source: trunk/cppad/local/cppad_assert.hpp @ 3320

Last change on this file since 3320 was 3320, checked in by bradbell, 6 years ago
  1. g++ 4.8.2 has shadow warnings by default, but eigen and fadbad do not

these warnings, so supress then in these cases.

  1. Move check that arguments come before result into on place,

CPPAD_ASSERT_ARG_BEFORE_RESULT (only one argument case so far).

main.cpp: fix shadowing of index variable.
CMakeLists.txt: adapt to change in teuchos library name.
sparse_jacobian.cpp: fix a shadowed variable.
check_svn_id.sh: ignore svn_commit.sh.
gpl_license.sh: ignore svn_commit.sh.

  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: cppad_assert.hpp 3320 2014-09-11 23:06:21Z bradbell $ */
2# ifndef CPPAD_CPPAD_ASSERT_INCLUDED
3# define CPPAD_CPPAD_ASSERT_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15
16/*!
17\file cppad_assert.hpp
18Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
19*/
20
21/*
22-------------------------------------------------------------------------------
23$begin cppad_assert$$
24$spell
25        CppAD
26        exp
27        const
28        bool
29$$
30
31$index assert, error macro $$
32$index error, assert macro$$
33$index macro, error assert$$
34
35$section CppAD Assertions During Execution$$
36
37$head Syntax$$
38$codei%CPPAD_ASSERT_KNOWN(%exp%, %msg%)
39%$$
40$codei%CPPAD_ASSERT_UNKNOWN(%exp%)%$$
41
42
43$head Purpose$$
44These CppAD macros are used to detect and report errors.
45They are documented here because they correspond to the C++
46source code that the error is reported at.
47
48$head NDEBUG$$
49$index NDEBUG$$
50If the preprocessor symbol
51$cref/NDEBUG/Faq/Speed/NDEBUG/$$ is defined,
52these macros do nothing; i.e., they are optimized out.
53
54$head Restriction$$
55The CppAD user should not uses these macros.
56You can however write your own macros that do not begin with $code CPPAD$$
57and that call the $cref/CppAD error handler/ErrorHandler/$$.
58
59$subhead Known$$
60$index CPPAD_ASSERT_KNOWN$$
61The $code CPPAD_ASSERT_KNOWN$$ macro is used to check for an error
62with a known cause.
63For example, many CppAD routines uses these macros
64to make sure their arguments conform to their specifications.
65
66$subhead Unknown$$
67$index CPPAD_ASSERT_UNKNOWN$$
68The $code CPPAD_ASSERT_UNKNOWN$$ macro is used to check that the
69CppAD internal data structures conform as expected.
70If this is not the case, CppAD does not know why the error has
71occurred; for example, the user may have written past the end
72of an allocated array.
73
74$head Exp$$
75The argument $icode exp$$ is a C++ source code expression
76that results in a $code bool$$ value that should be true.
77If it is false, an error has occurred.
78This expression may be execute any number of times
79(including zero times) so it must have not side effects.
80
81$head Msg$$
82The argument $icode msg$$ has prototype
83$codei%
84        const char *%msg%
85%$$
86and contains a $code '\0'$$ terminated character string.
87This string is a description of the error
88corresponding to $icode exp$$ being false.
89
90$head Error Handler$$
91These macros use the
92$cref/CppAD error handler/ErrorHandler/$$ to report errors.
93This error handler can be replaced by the user.
94
95$end
96------------------------------------------------------------------------------
97*/
98
99# include <cassert>
100# include <iostream>
101# include <cppad/error_handler.hpp>
102
103# ifdef _OPENMP
104# include <omp.h>
105# endif
106
107
108/*!
109\def CPPAD_ASSERT_KNOWN(exp, msg)
110Check that \a exp is true, if not print \a msg and terminate execution.
111
112The C++ expression \a exp is expected to be true.
113If it is false,
114the CppAD use has made an error that is described by \a msg.
115If the preprocessor symbol \a NDEBUG is not defined,
116and \a exp is false,
117this macro will report the source code line number at
118which this expected result occurred.
119In addition, it will print the specified error message \a msg.
120*/
121# ifdef NDEBUG
122# define CPPAD_ASSERT_KNOWN(exp, msg)  // do nothing
123# else
124# define CPPAD_ASSERT_KNOWN(exp, msg)           \
125{       if( ! ( exp ) )                         \
126        CppAD::ErrorHandler::Call(              \
127                true       ,                    \
128                __LINE__   ,                    \
129                __FILE__   ,                    \
130                #exp       ,                    \
131                msg        );                   \
132}
133# endif
134
135/*!
136\def CPPAD_ASSERT_UNKNOWN(exp)
137Check that \a exp is true, if not terminate execution.
138
139The C++ expression \a exp is expected to be true.
140If it is false,
141CppAD has detected an error but does not know the cause of the error.
142If the preprocessor symbol \a NDEBUG is not defined,
143and \a exp is false,
144this macro will report the source code line number at
145which this expected result occurred.
146*/
147# ifdef NDEBUG
148# define CPPAD_ASSERT_UNKNOWN(exp)      // do nothing
149# else
150# define CPPAD_ASSERT_UNKNOWN(exp)              \
151{       if( ! ( exp ) )                         \
152        CppAD::ErrorHandler::Call(              \
153                false      ,                    \
154                __LINE__   ,                    \
155                __FILE__   ,                    \
156                #exp       ,                    \
157                ""         );                   \
158}
159# endif
160
161/*!
162\def CPPAD_ASSERT_NARG_NRES(op, n_arg, n_res)
163Check that operator \a op has the specified number of of arguments and results.
164
165If \a NDEBUG is not defined and either the number of arguments
166or the number of results are not as expected,
167execution is terminated and the source code line number is reported.
168*/
169# define CPPAD_ASSERT_NARG_NRES(op, n_arg, n_res)   \
170        CPPAD_ASSERT_UNKNOWN( NumArg(op) == n_arg ) \
171        CPPAD_ASSERT_UNKNOWN( NumRes(op) == n_res )
172
173/*!
174\def CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
175Check that the first call to a routine is not during parallel execution mode.
176
177If \c NDEBUG is defined, this macro has no effect
178(not even the definition of (\c assert_first_call).
179Otherwise, the variable
180\code
181        static bool assert_first_call
182\endcode
183is defined and if the first call is executed in parallel mode,
184execution is terminated and the source code line number is reported.
185*/
186# ifdef NDEBUG
187# define CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
188# else
189# define CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL                           \
190        static bool assert_first_call = true;                              \
191        if( assert_first_call )                                            \
192        {       CPPAD_ASSERT_KNOWN(                                           \
193                ! (CppAD::thread_alloc::in_parallel() ),                      \
194                "In parallel mode and parallel_setup has not been called."    \
195                );                                                            \
196                assert_first_call = false;                                    \
197        }
198# endif
199
200/*!
201\def CPPAD_ASSERT_ARG_BEFORE_RESULT
202Check that operator arguments come before result.
203
204If \c NDEBUG is defined, this macro has no effect,
205otherwise it calls the function assert_arg_before_result.
206*/
207# ifdef NDEBUG
208# define CPPAD_ASSERT_ARG_BEFORE_RESULT(op, arg, result)
209# else
210# define CPPAD_ASSERT_ARG_BEFORE_RESULT(op, arg, result) \
211        assert_arg_before_result(op, arg, result)
212       
213# endif
214
215# endif
Note: See TracBrowser for help on using the repository browser.