casacore
Loading...
Searching...
No Matches
Assert.h
Go to the documentation of this file.
1//# Assert.h: Throw exceptions when Assertions fail.
2//# Copyright (C) 1993,1994,1995,1999,2000,2002
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_ASSERT_H
27#define CASA_ASSERT_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Exceptions/Error.h>
31
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35// <summary>Utility function for Assert macros.</summary>
36// <use visibility=export>
37// <reviewed reviewer="Friso Olnon" date="1995/03/13" tests="" demos="">
38// </reviewed>
39
40// <prerequisite>
41// <li> module <linkto module=Exceptions>Exceptions</linkto>
42// </prerequisite>
43
44// <etymology>
45// Templated function <src>assert_</src> is the basis for the macros
46// <src>DebugAssertExit</src>, <src>DebugAssert</src>,
47// <src>AlwaysAssertExit</src>, and <src>AlwaysAssert</src> which
48// form the "public interface" to the Assertion mechanism.
49// </etymology>
50
51// <synopsis>
52// The present Assertion mechanism uses the exception
53// handling mechanism to throw the errors when an Assertion
54// fails. It can be used in two ways:
55// <dl>
56// <dt> <src>DebugAssertExit(expr)</src>
57// <dt> <src>AlwaysAssertExit(expr)</src>
58// <dd> cause the program to abort if <src>expr</src> evaluates to false.
59// This form is intended for the <em>end users</em>
60// because presumabily at their level there is no way to recover
61// from errors.
62// <dt> <src>DebugAssert(expr, exception)</src>
63// <dt> <src>AlwaysAssert(expr, exception)</src>
64// <dd> throw the specified exception if the <src>expr</src> is false.
65// This form is designed to be used by <em>library
66// elements</em> because it actually raises an exception which
67// can be later caught in the regular way.
68// </dl>
69//
70// <note role=tip> <src>DebugAssertExit</src> and
71// <src>DebugAssert</src> are only invoked in
72// debug mode (i.e. when <src>AIPS_DEBUG</src> is defined); otherwise
73// they preprocess to null statements. <src>AlwaysAssertExit</src>
74// and <src>AlwaysAssert</src> are always invoked.
75// </note>
76//
77// </synopsis>
78
79// <example>
80// The implementation of the <linkto module=Arrays>Array classes</linkto>
81// contains many examples of the Assertion mechanism. The following
82// application of the Assertion mechanism is taken from the archive of
83// the aips2-workers@nrao.edu mail group (Brian Glendenning, 1994/03/23):
84//
85// I thought I'd readvertise a technique I use that helps me find
86// problems in the classes I write. I have found this to be an
87// EXTREMELY useful way of discovering bugs automatically (so the users
88// of your class don't have to manually).
89//
90// In your class, write an <src>ok()</src> member function that
91// returns a <src>Bool</src>. Allow for inheritance and make it a
92// virtual function (in fact, the derived class's <src>ok()</src> would
93// probably call the <src>ok()</src> from its parent, as well as doing
94// specific stuff for the derived class).
95//
96// Then in every member function, place a call to <src>ok()</src> in
97// an Assertion. Like this:
98// <srcblock>
99// DebugAssert(ok(), AipsError); // include aips/Assert.h in your .cc file
100// </srcblock>
101//
102// The second argument is the exception you want to throw.
103// <src>AipsError</src> will always do, although you can throw a
104// more particular one if you want to. This Assertion will not be in
105// production code -- i.e. if <src>AIPS_DEBUG</src> is not defined, the
106// above line will be a null statement. I place these lines at the entry
107// to all member functions (except I place them at the <em>end</em> of a
108// constructor!). (I normally don't put an Assertion in an inline
109// function).
110//
111// In the <src>ok()</src> function you should Assert a class's
112// invariants. This is more or less the same as Asserting that an
113// object's private and protected data are <em>consistent</em>. For
114// example, one of the simple tests I do in the array classes is Assert
115// that the number of elements (which I cache) is indeed equal to the
116// product of its shape (I do ~15 tests in the <src>ok()</src> for the
117// new <src>Array<T></src> class).
118// </example>
119
120// this templated function is called from Assert macros
121template<typename t>
122void assert_(bool expr, const char *msg, const char* file, int line);
123
124// These marcos are provided for use instead of simply using the
125// <src>assert_</src> function directly.
126//
127// <src>DebugAssert</src> and <src>AlwaysAssert</src> are designed to
128// be used by library elements because they actually raise an exception
129// which can later be later caught.
130// <src>DebugAssertExit</src> and <src>AlwaysAssertExit</src> are
131// intended to be used by the applications writer, because they cause an
132// <src>exit(0)</src>.
133
134#define AlwaysAssert(expr, exception) \
135 {casacore::assert_<exception >(static_cast<bool>(expr), "Failed AlwaysAssert " #expr,__FILE__,static_cast<int>(__LINE__)); }
136#define AlwaysAssertExit(expr) \
137 {casacore::assert_<casacore::AbortError>(static_cast<bool>(expr), "Unrecoverable AlwaysAssertExit: " #expr,__FILE__,static_cast<int>(__LINE__));}
138
139#if defined(AIPS_DEBUG)
140
141#define DebugAssert(expr, exception) \
142 {casacore::assert_<exception >(static_cast<bool>(expr), "Failed Assertion: " #expr,__FILE__,static_cast<int>(__LINE__));}
143#define DebugAssertExit(expr) \
144 {casacore::assert_<casacore::AbortError>(static_cast<bool>(expr), "Unrecoverable Assertion: " #expr,__FILE__,static_cast<int>(__LINE__));}
145
146#else
147
148#define DebugAssert(expr, exception)
149#define DebugAssertExit(expr)
150
151#endif
152
153
154} //# NAMESPACE CASACORE - END
155
156#ifndef CASACORE_NO_AUTO_TEMPLATES
157#include <casacore/casa/Utilities/Assert.tcc>
158#endif //# CASACORE_NO_AUTO_TEMPLATES
159#endif
void assert_(bool expr, const char *msg, const char *file, int line)
this file contains all the compiler specific defines
Definition mainpage.dox:28