/home/coin/SVN-release/CoinAll-1.1.0/cppad/cppad/check_simple_vector.hpp

Go to the documentation of this file.
00001 # ifndef CPPAD_CHECK_SIMPLE_VECTOR_INCLUDED
00002 # define CPPAD_CHECK_SIMPLE_VECTOR_INCLUDED
00003 
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
00006 
00007 CppAD is distributed under multiple licenses. This distribution is under
00008 the terms of the 
00009                     Common Public License Version 1.0.
00010 
00011 A copy of this license is included in the COPYING file of this distribution.
00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00013 -------------------------------------------------------------------------- */
00014 /*
00015 $begin CheckSimpleVector$$
00016 $spell
00017         cppad.hpp
00018         CppAD
00019 $$
00020 
00021 $section Check Simple Vector Concept$$
00022 
00023 $index simple, vector check$$
00024 $index vector, simple check$$
00025 $index check, simple vector$$
00026 $index concept, check simple vector$$
00027 
00028 $head Syntax$$
00029 $code # include <cppad/check_simple_vector.hpp>$$
00030 $pre
00031 $$
00032 $syntax%CheckSimpleVector<%Scalar%, %Vector%>()%$$
00033 
00034 
00035 $head Purpose$$
00036 The syntax 
00037 $syntax%
00038         CheckSimpleVector<%Scalar%, %Vector%>()
00039 %$$
00040 preforms compile and run time checks that the type specified
00041 by $italic Vector$$ satisfies all the requirements for 
00042 a $xref/SimpleVector/$$ class with 
00043 $xref/SimpleVector/Elements of Specified Type/elements of type Scalar/$$.
00044 If a requirement is not satisfied,
00045 a an error message makes it clear what condition is not satisfied.
00046 
00047 $head Restrictions$$
00048 The following extra assumption is made by $code CheckSimpleVector$$:
00049 The type $italic Scalar$$ supports the syntax
00050 $syntax%
00051         %Scalar%(%i%) == %Scalar%(%j%)%
00052 %$$
00053 where $italic i$$ and $italic j$$ are either $code 0$$ or $code 1$$.
00054 In addition, this syntax returns the value true (false) if and only if
00055 $italic i$$ equals $italic j$$.
00056 ($italic i$$ does not equal $italic j$$).
00057 
00058 
00059 $head Include$$
00060 The file $code cppad/check_simple_vector.hpp$$ is included by $code cppad/cppad.hpp$$
00061 but it can also be included separately with out the rest
00062 if the CppAD include files.
00063 
00064 $head Example$$
00065 $children%
00066         example/check_simple_vector.cpp
00067 %$$
00068 The file $xref/CheckSimpleVector.cpp/$$
00069 contains an example and test of this function where $italic S$$
00070 is the same as $italic T$$.
00071 It returns true, if it succeeds an false otherwise.
00072 The comments in this example suggest a way to change the example
00073 so $italic S$$ is not the same as $italic T$$.
00074 
00075 $end
00076 ---------------------------------------------------------------------------
00077 */
00078 
00079 # include <cppad/local/cppad_assert.hpp>
00080 
00081 namespace CppAD {
00082 
00083 # ifdef NDEBUG
00084         template <class Scalar, class Vector>
00085         inline void CheckSimpleVector(void)
00086         { }
00087 # else
00088         template <class S, class T>
00089         struct ok_if_S_same_as_T { };
00090 
00091         template <class T>
00092         struct ok_if_S_same_as_T<T,T> { typedef T ok; };
00093 
00094         template <class Scalar, class Vector>
00095         void CheckSimpleVector(void)
00096         {       Scalar zero = Scalar(0);
00097                 Scalar one  = Scalar(1);
00098 
00099                 // only need execute once per value Scalar, Vector pair
00100                 static bool runOnce = false;
00101                 if( runOnce )
00102                         return;
00103                 runOnce = true;
00104 
00105                 // value_type must be type of elements of Vector
00106                 typedef typename Vector::value_type value_type;
00107 
00108                 // check that elements of Vector have type Scalar
00109                 typedef typename ok_if_S_same_as_T<Scalar, value_type>::ok ok;
00110 
00111                 // check default constructor
00112                 Vector d;
00113 
00114                 // size member function
00115                 CPPAD_ASSERT_KNOWN(
00116                         d.size() == 0,
00117                         "default construtor result does not have size zero"
00118                 );
00119 
00120                 // resize to same size as other vectors in test
00121                 d.resize(1);
00122 
00123                 // check sizing constructor
00124                 Vector s(1);
00125 
00126                 // check element assignment
00127                 s[0] = one;
00128                 CPPAD_ASSERT_KNOWN(
00129                         s[0] == one,
00130                         "element assignment failed"
00131                 );
00132 
00133                 // check copy constructor
00134                 s[0] = zero;
00135                 const Vector c(s);
00136                 s[0] = one;
00137                 CPPAD_ASSERT_KNOWN(
00138                         c[0] == zero,
00139                         "copy constructor is shallow"
00140                 );
00141 
00142                 // vector assignment operator
00143                 s = c;
00144                 s[0] = one;
00145                 CPPAD_ASSERT_KNOWN(
00146                         c[0] == zero,
00147                         "assignment operator is shallow"
00148                 );
00149 
00150                 // element access, right side const
00151                 // element assignment, left side not const
00152                 d[0] = s[0];
00153         }
00154 # endif
00155 
00156 } // end namespace CppAD
00157 
00158 # endif

Generated on Sun Nov 14 14:06:32 2010 for Coin-All by  doxygen 1.4.7