CppAD: A C++ Algorithmic Differentiation Package  20130918
check_simple_vector.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_CHECK_SIMPLE_VECTOR_INCLUDED
00003 # define CPPAD_CHECK_SIMPLE_VECTOR_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 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 CheckSimpleVector$$
00017 $spell
00018      alloc
00019      const
00020      cppad.hpp
00021      CppAD
00022 $$
00023 
00024 $section Check Simple Vector Concept$$
00025 
00026 $index simple, vector check$$
00027 $index vector, simple check$$
00028 $index check, simple vector$$
00029 $index concept, check simple vector$$
00030 
00031 $head Syntax$$
00032 $code # include <cppad/check_simple_vector.hpp>$$
00033 $pre
00034 $$
00035 $codei%CheckSimpleVector<%Scalar%, %Vector%>()%$$
00036 $pre
00037 $$
00038 $codei%CheckSimpleVector<%Scalar%, %Vector%>(%x%, %y%)%$$
00039 
00040 
00041 $head Purpose$$
00042 Preforms compile and run time checks that the type specified
00043 by $icode Vector$$ satisfies all the requirements for 
00044 a $cref SimpleVector$$ class with 
00045 $cref/elements of type/SimpleVector/Elements of Specified Type/$$ 
00046 $icode Scalar$$.
00047 If a requirement is not satisfied,
00048 a an error message makes it clear what condition is not satisfied.
00049 
00050 $head x, y$$
00051 If the arguments $icode x$$ and $icode y$$ are present,
00052 they have prototype
00053 $codei%
00054      const %Scalar%& %x%
00055      const %Scalar%& %y%
00056 %$$
00057 In addition, the check
00058 $codei%
00059      %x% == %x%
00060 %$$
00061 will return the boolean value $code true$$, and 
00062 $codei%
00063      %x% == %y%
00064 %$$
00065 will return $code false$$.
00066 
00067 $head Restrictions$$
00068 If the arguments $icode x$$ and $icode y$$ are not present,
00069 the following extra assumption is made by $code CheckSimpleVector$$:
00070 If $icode x$$ is a $icode Scalar$$ object
00071 $codei%
00072      %x% = 0
00073      %y% = 1
00074 %$$
00075 assigns values to the objects $icode x$$ and $icode y$$.
00076 In addition, 
00077 $icode%x% == %x%$$ would return the boolean value $code true$$ and
00078 $icode%x% == %y%$$ would return $code false$$.
00079 
00080 $head Include$$
00081 The file $code cppad/check_simple_vector.hpp$$ is included by $code cppad/cppad.hpp$$
00082 but it can also be included separately with out the rest
00083 if the CppAD include files.
00084 
00085 $head Parallel Mode$$
00086 $index parallel, CheckSimpleVector$$
00087 $index CheckSimpleVector, parallel$$
00088 The routine $cref/thread_alloc::parallel_setup/ta_parallel_setup/$$
00089 must be called before it
00090 can be used in $cref/parallel/ta_in_parallel/$$ mode.
00091 
00092 $head Example$$
00093 $children%
00094      example/check_simple_vector.cpp
00095 %$$
00096 The file $cref check_simple_vector.cpp$$
00097 contains an example and test of this function where $icode S$$
00098 is the same as $icode T$$.
00099 It returns true, if it succeeds an false otherwise.
00100 The comments in this example suggest a way to change the example
00101 so $icode S$$ is not the same as $icode T$$.
00102 
00103 $end
00104 ---------------------------------------------------------------------------
00105 */
00106 
00107 # include <cstddef>
00108 # include <cppad/local/cppad_assert.hpp>
00109 # include <cppad/local/define.hpp>
00110 # include <cppad/thread_alloc.hpp>
00111 
00112 namespace CppAD {
00113 
00114 # ifdef NDEBUG
00115      template <class Scalar, class Vector>
00116      inline void CheckSimpleVector(const Scalar& x, const Scalar& y)
00117      { }
00118      template <class Scalar, class Vector>
00119      inline void CheckSimpleVector(void)
00120      { }
00121 # else
00122      template <class S, class T>
00123      struct ok_if_S_same_as_T { };
00124 
00125      template <class T>
00126      struct ok_if_S_same_as_T<T,T> { T value; };
00127 
00128      template <class Scalar, class Vector>
00129      void CheckSimpleVector(const Scalar& x, const Scalar& y)
00130      {    CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
00131           static size_t count;
00132           if( count > 0  )
00133                return;
00134           count++;
00135 
00136           // value_type must be type of elements of Vector
00137           typedef typename Vector::value_type value_type;
00138 
00139           // check that elements of Vector have type Scalar
00140           struct ok_if_S_same_as_T<Scalar, value_type> x_copy;
00141           x_copy.value = x;
00142 
00143           // check default constructor
00144           Vector d;
00145 
00146           // size member function
00147           CPPAD_ASSERT_KNOWN(
00148                d.size() == 0,
00149                "default construtor result does not have size zero"
00150           );
00151 
00152           // resize to same size as other vectors in test
00153           d.resize(1);
00154 
00155           // check sizing constructor
00156           Vector s(1);
00157 
00158           // check element assignment
00159           s[0] = y;
00160           CPPAD_ASSERT_KNOWN(
00161                s[0] == y,
00162                "element assignment failed"
00163           );
00164 
00165           // check copy constructor
00166           s[0] = x_copy.value;
00167           const Vector c(s);
00168           s[0] = y;
00169           CPPAD_ASSERT_KNOWN(
00170                c[0] == x,
00171                "copy constructor is shallow"
00172           );
00173 
00174           // vector assignment operator
00175           d[0] = x;
00176           s    = d;
00177           s[0] = y;
00178           CPPAD_ASSERT_KNOWN(
00179                d[0] == x,
00180                "assignment operator is shallow"
00181           );
00182 
00183           // element access, right side const
00184           // element assignment, left side not const
00185           d[0] = c[0];
00186           CPPAD_ASSERT_KNOWN(
00187                d[0] == x,
00188                "element assignment from const failed" 
00189           );
00190      }
00191      template <class Scalar, class Vector>
00192      void CheckSimpleVector(void)
00193      {    Scalar x;
00194           Scalar y;
00195 
00196           // use assignment and not constructor
00197           x = 0;
00198           y = 1;
00199 
00200           CheckSimpleVector<Scalar, Vector>(x, y);
00201      }
00202 
00203 # endif
00204 
00205 } // end namespace CppAD
00206 
00207 # endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines