CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
check_numeric_type.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_CHECK_NUMERIC_TYPE_HPP
2 # define CPPAD_UTILITY_CHECK_NUMERIC_TYPE_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin CheckNumericType$$
16 $spell
17  alloc
18  cppad.hpp
19  CppAD
20 $$
21 
22 $section Check NumericType Class Concept$$
23 $mindex numeric CheckNumericType$$
24 
25 
26 $head Syntax$$
27 $codei%# include <cppad/utility/check_numeric_type.hpp>
28 %$$
29 $codei%CheckNumericType<%NumericType%>()%$$
30 
31 
32 $head Purpose$$
33 The syntax
34 $codei%
35  CheckNumericType<%NumericType%>()
36 %$$
37 preforms compile and run time checks that the type specified
38 by $icode NumericType$$ satisfies all the requirements for
39 a $cref NumericType$$ class.
40 If a requirement is not satisfied,
41 a an error message makes it clear what condition is not satisfied.
42 
43 $head Include$$
44 The file $code cppad/check_numeric_type.hpp$$ is included by $code cppad/cppad.hpp$$
45 but it can also be included separately with out the rest
46 if the CppAD include files.
47 
48 $head Parallel Mode$$
49 The routine $cref/thread_alloc::parallel_setup/ta_parallel_setup/$$
50 must be called before it
51 can be used in $cref/parallel/ta_in_parallel/$$ mode.
52 
53 $head Example$$
54 $children%
55  example/utility/check_numeric_type.cpp
56 %$$
57 The file $cref check_numeric_type.cpp$$
58 contains an example and test of this function.
59 It returns true, if it succeeds an false otherwise.
60 The comments in this example suggest a way to change the example
61 so an error message occurs.
62 
63 $end
64 ---------------------------------------------------------------------------
65 */
66 
67 # include <cstddef>
69 
70 namespace CppAD {
71 
72 # ifdef NDEBUG
73  template <class NumericType>
74  void CheckNumericType(void)
75  { }
76 # else
77  template <class NumericType>
78  NumericType CheckNumericType(void)
79  { // Section 3.6.2 of ISO/IEC 14882:1998(E) states: "The storage for
80  // objects with static storage duration (3.7.1) shall be zero-
81  // initialized (8.5) before any other initialization takes place."
82  static size_t count[CPPAD_MAX_NUM_THREADS];
83  size_t thread = thread_alloc::thread_num();
84  if( count[thread] > 0 )
85  return NumericType(0);
86  count[thread]++;
87  /*
88  contructors
89  */
90  NumericType check_NumericType_default_constructor;
91  NumericType check_NumericType_constructor_from_int(1);
92 
93  const NumericType x(1);
94 
95  NumericType check_NumericType_copy_constructor(x);
96 
97  // assignment
98  NumericType check_NumericType_assignment;
99  check_NumericType_assignment = x;
100 
101  /*
102  unary operators
103  */
104  const NumericType check_NumericType_unary_plus(1);
105  NumericType check_NumericType_unary_plus_result =
106  + check_NumericType_unary_plus;
107 
108  const NumericType check_NumericType_unary_minus(1);
109  NumericType check_NumericType_unary_minus_result =
110  - check_NumericType_unary_minus;
111 
112  /*
113  binary operators
114  */
115  const NumericType check_NumericType_binary_addition(1);
116  NumericType check_NumericType_binary_addition_result =
117  check_NumericType_binary_addition + x;
118 
119  const NumericType check_NumericType_binary_subtraction(1);
120  NumericType check_NumericType_binary_subtraction_result =
121  check_NumericType_binary_subtraction - x;
122 
123  const NumericType check_NumericType_binary_multiplication(1);
124  NumericType check_NumericType_binary_multiplication_result =
125  check_NumericType_binary_multiplication * x;
126 
127  const NumericType check_NumericType_binary_division(1);
128  NumericType check_NumericType_binary_division_result =
129  check_NumericType_binary_division / x;
130 
131  /*
132  compound assignment operators
133  */
134  NumericType
135  check_NumericType_computed_assignment_addition(1);
136  check_NumericType_computed_assignment_addition += x;
137 
138  NumericType
139  check_NumericType_computed_assignment_subtraction(1);
140  check_NumericType_computed_assignment_subtraction -= x;
141 
142  NumericType
143  check_NumericType_computed_assignment_multiplication(1);
144  check_NumericType_computed_assignment_multiplication *= x;
145 
146  NumericType
147  check_NumericType_computed_assignment_division(1);
148  check_NumericType_computed_assignment_division /= x;
149 
150  /*
151  use all values so as to avoid warnings
152  */
153  check_NumericType_default_constructor = x;
154  return
155  + check_NumericType_default_constructor
156  + check_NumericType_constructor_from_int
157  + check_NumericType_copy_constructor
158  + check_NumericType_assignment
159  + check_NumericType_unary_plus_result
160  + check_NumericType_unary_minus_result
161  + check_NumericType_binary_addition_result
162  + check_NumericType_binary_subtraction_result
163  + check_NumericType_binary_multiplication_result
164  + check_NumericType_binary_division_result
165  + check_NumericType_computed_assignment_addition
166  + check_NumericType_computed_assignment_subtraction
167  + check_NumericType_computed_assignment_multiplication
168  + check_NumericType_computed_assignment_division
169  ;
170  }
171 # endif
172 
173 } // end namespace CppAD
174 
175 # endif
NumericType CheckNumericType(void)
static size_t thread_num(void)
Get current thread number.
File used to define the CppAD multi-threading allocator class.