CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_CHECK_NUMERIC_TYPE_INCLUDED 00003 # define CPPAD_CHECK_NUMERIC_TYPE_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 /* 00016 $begin CheckNumericType$$ 00017 $spell 00018 alloc 00019 cppad.hpp 00020 CppAD 00021 $$ 00022 00023 $section Check NumericType Class Concept$$ 00024 00025 $index numeric, check$$ 00026 $index check, numeric$$ 00027 $index concept, check numeric$$ 00028 00029 $head Syntax$$ 00030 $code # include <cppad/check_numeric_type.hpp>$$ 00031 $pre 00032 $$ 00033 $codei%CheckNumericType<%NumericType%>()%$$ 00034 00035 00036 $head Purpose$$ 00037 The syntax 00038 $codei% 00039 CheckNumericType<%NumericType%>() 00040 %$$ 00041 preforms compile and run time checks that the type specified 00042 by $icode NumericType$$ satisfies all the requirements for 00043 a $cref NumericType$$ class. 00044 If a requirement is not satisfied, 00045 a an error message makes it clear what condition is not satisfied. 00046 00047 $head Include$$ 00048 The file $code cppad/check_numeric_type.hpp$$ is included by $code cppad/cppad.hpp$$ 00049 but it can also be included separately with out the rest 00050 if the CppAD include files. 00051 00052 $head Parallel Mode$$ 00053 $index parallel, CheckNumericType$$ 00054 $index CheckNumericType, parallel$$ 00055 The routine $cref/thread_alloc::parallel_setup/ta_parallel_setup/$$ 00056 must be called before it 00057 can be used in $cref/parallel/ta_in_parallel/$$ mode. 00058 00059 $head Example$$ 00060 $children% 00061 example/check_numeric_type.cpp 00062 %$$ 00063 The file $cref check_numeric_type.cpp$$ 00064 contains an example and test of this function. 00065 It returns true, if it succeeds an false otherwise. 00066 The comments in this example suggest a way to change the example 00067 so an error message occurs. 00068 00069 $end 00070 --------------------------------------------------------------------------- 00071 */ 00072 00073 # include <cstddef> 00074 # include <cppad/thread_alloc.hpp> 00075 00076 namespace CppAD { 00077 00078 # ifdef NDEBUG 00079 template <class NumericType> 00080 void CheckNumericType(void) 00081 { } 00082 # else 00083 template <class NumericType> 00084 NumericType CheckNumericType(void) 00085 { // Section 3.6.2 of ISO/IEC 14882:1998(E) states: "The storage for 00086 // objects with static storage duration (3.7.1) shall be zero- 00087 // initialized (8.5) before any other initialization takes place." 00088 static size_t count[CPPAD_MAX_NUM_THREADS]; 00089 size_t thread = thread_alloc::thread_num(); 00090 if( count[thread] > 0 ) 00091 return NumericType(0); 00092 count[thread]++; 00093 /* 00094 contructors 00095 */ 00096 NumericType check_NumericType_default_constructor; 00097 NumericType check_NumericType_constructor_from_int(1); 00098 00099 const NumericType x(1); 00100 00101 NumericType check_NumericType_copy_constructor(x); 00102 00103 // assignment 00104 NumericType check_NumericType_assignment; 00105 check_NumericType_assignment = x; 00106 00107 /* 00108 unary operators 00109 */ 00110 const NumericType check_NumericType_unary_plus(1); 00111 NumericType check_NumericType_unary_plus_result = 00112 + check_NumericType_unary_plus; 00113 00114 const NumericType check_NumericType_unary_minus(1); 00115 NumericType check_NumericType_unary_minus_result = 00116 - check_NumericType_unary_minus; 00117 00118 /* 00119 binary operators 00120 */ 00121 const NumericType check_NumericType_binary_addition(1); 00122 NumericType check_NumericType_binary_addition_result = 00123 check_NumericType_binary_addition + x; 00124 00125 const NumericType check_NumericType_binary_subtraction(1); 00126 NumericType check_NumericType_binary_subtraction_result = 00127 check_NumericType_binary_subtraction - x; 00128 00129 const NumericType check_NumericType_binary_multiplication(1); 00130 NumericType check_NumericType_binary_multiplication_result = 00131 check_NumericType_binary_multiplication * x; 00132 00133 const NumericType check_NumericType_binary_division(1); 00134 NumericType check_NumericType_binary_division_result = 00135 check_NumericType_binary_division / x; 00136 00137 /* 00138 computed assignment operators 00139 */ 00140 NumericType 00141 check_NumericType_computed_assignment_addition(1); 00142 check_NumericType_computed_assignment_addition += x; 00143 00144 NumericType 00145 check_NumericType_computed_assignment_subtraction(1); 00146 check_NumericType_computed_assignment_subtraction -= x; 00147 00148 NumericType 00149 check_NumericType_computed_assignment_multiplication(1); 00150 check_NumericType_computed_assignment_multiplication *= x; 00151 00152 NumericType 00153 check_NumericType_computed_assignment_division(1); 00154 check_NumericType_computed_assignment_division /= x; 00155 00156 /* 00157 use all values so as to avoid warnings 00158 */ 00159 check_NumericType_default_constructor = x; 00160 return 00161 + check_NumericType_default_constructor 00162 + check_NumericType_constructor_from_int 00163 + check_NumericType_copy_constructor 00164 + check_NumericType_assignment 00165 + check_NumericType_unary_plus_result 00166 + check_NumericType_unary_minus_result 00167 + check_NumericType_binary_addition_result 00168 + check_NumericType_binary_subtraction_result 00169 + check_NumericType_binary_multiplication_result 00170 + check_NumericType_binary_division_result 00171 + check_NumericType_computed_assignment_addition 00172 + check_NumericType_computed_assignment_subtraction 00173 + check_NumericType_computed_assignment_multiplication 00174 + check_NumericType_computed_assignment_division 00175 ; 00176 } 00177 # endif 00178 00179 } // end namespace CppAD 00180 00181 # endif