CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
check_simple_vector.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_CHECK_SIMPLE_VECTOR_HPP
2 # define CPPAD_UTILITY_CHECK_SIMPLE_VECTOR_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 CheckSimpleVector$$
16 $spell
17  alloc
18  const
19  cppad.hpp
20  CppAD
21 $$
22 
23 $section Check Simple Vector Concept$$
24 $mindex CheckSimpleVector$$
25 
26 
27 $head Syntax$$
28 $codei%# include <cppad/utility/check_simple_vector.hpp>
29 %$$
30 $codei%CheckSimpleVector<%Scalar%, %Vector%>()%$$
31 $pre
32 $$
33 $codei%CheckSimpleVector<%Scalar%, %Vector%>(%x%, %y%)%$$
34 
35 
36 $head Purpose$$
37 Preforms compile and run time checks that the type specified
38 by $icode Vector$$ satisfies all the requirements for
39 a $cref SimpleVector$$ class with
40 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
41 $icode Scalar$$.
42 If a requirement is not satisfied,
43 a an error message makes it clear what condition is not satisfied.
44 
45 $head x, y$$
46 If the arguments $icode x$$ and $icode y$$ are present,
47 they have prototype
48 $codei%
49  const %Scalar%& %x%
50  const %Scalar%& %y%
51 %$$
52 In addition, the check
53 $codei%
54  %x% == %x%
55 %$$
56 will return the boolean value $code true$$, and
57 $codei%
58  %x% == %y%
59 %$$
60 will return $code false$$.
61 
62 $head Restrictions$$
63 If the arguments $icode x$$ and $icode y$$ are not present,
64 the following extra assumption is made by $code CheckSimpleVector$$:
65 If $icode x$$ is a $icode Scalar$$ object
66 $codei%
67  %x% = 0
68  %y% = 1
69 %$$
70 assigns values to the objects $icode x$$ and $icode y$$.
71 In addition,
72 $icode%x% == %x%$$ would return the boolean value $code true$$ and
73 $icode%x% == %y%$$ would return $code false$$.
74 
75 $head Include$$
76 The file $code cppad/check_simple_vector.hpp$$ is included by $code cppad/cppad.hpp$$
77 but it can also be included separately with out the rest
78 if the CppAD include files.
79 
80 $head Parallel Mode$$
81 The routine $cref/thread_alloc::parallel_setup/ta_parallel_setup/$$
82 must be called before it
83 can be used in $cref/parallel/ta_in_parallel/$$ mode.
84 
85 $head Example$$
86 $children%
87  example/utility/check_simple_vector.cpp
88 %$$
89 The file $cref check_simple_vector.cpp$$
90 contains an example and test of this function where $icode S$$
91 is the same as $icode T$$.
92 It returns true, if it succeeds an false otherwise.
93 The comments in this example suggest a way to change the example
94 so $icode S$$ is not the same as $icode T$$.
95 
96 $end
97 ---------------------------------------------------------------------------
98 */
99 
100 # include <cstddef>
101 # include <cppad/core/cppad_assert.hpp>
102 # include <cppad/core/define.hpp>
104 
105 namespace CppAD {
106 
107 # ifdef NDEBUG
108  template <class Scalar, class Vector>
109  inline void CheckSimpleVector(const Scalar& x, const Scalar& y)
110  { }
111  template <class Scalar, class Vector>
112  inline void CheckSimpleVector(void)
113  { }
114 # else
115  template <class S, class T>
116  struct ok_if_S_same_as_T { };
117 
118  template <class T>
119  struct ok_if_S_same_as_T<T,T> { T value; };
120 
121  template <class Scalar, class Vector>
122  void CheckSimpleVector(const Scalar& x, const Scalar& y)
124  static size_t count;
125  if( count > 0 )
126  return;
127  count++;
128 
129  // value_type must be type of elements of Vector
130  typedef typename Vector::value_type value_type;
131 
132  // check that elements of Vector have type Scalar
133  struct ok_if_S_same_as_T<Scalar, value_type> x_copy;
134  x_copy.value = x;
135 
136  // check default constructor
137  Vector d;
138 
139  // size member function
141  d.size() == 0,
142  "default construtor result does not have size zero"
143  );
144 
145  // resize to same size as other vectors in test
146  d.resize(1);
147 
148  // check sizing constructor
149  Vector s(1);
150 
151  // check element assignment
152  s[0] = y;
154  s[0] == y,
155  "element assignment failed"
156  );
157 
158  // check copy constructor
159  s[0] = x_copy.value;
160  const Vector c(s);
161  s[0] = y;
163  c[0] == x,
164  "copy constructor is shallow"
165  );
166 
167  // vector assignment operator
168  d[0] = x;
169  s = d;
170  s[0] = y;
172  d[0] == x,
173  "assignment operator is shallow"
174  );
175 
176  // element access, right side const
177  // element assignment, left side not const
178  d[0] = c[0];
180  d[0] == x,
181  "element assignment from const failed"
182  );
183  }
184  template <class Scalar, class Vector>
185  void CheckSimpleVector(void)
186  { Scalar x;
187  Scalar y;
188 
189  // use assignment and not constructor
190  x = 0;
191  y = 1;
192 
193  CheckSimpleVector<Scalar, Vector>(x, y);
194  }
195 
196 # endif
197 
198 } // end namespace CppAD
199 
200 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
Define processor symbols and macros that are used by CppAD.
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
#define CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
Check that the first call to a routine is not during parallel execution mode.
File used to define the CppAD multi-threading allocator class.
Scalar value_type
void CheckSimpleVector(const Scalar &x, const Scalar &y)