GL Studio SCECpp Runtime Library
gls_assert.h
Go to the documentation of this file.
1 /*! \file gls_assert.h
2 
3 \brief This header defines the runtime assert checking macros for
4  the GL Studio DO-178B Runtime Library.
5 
6 \par Copyright Information
7 Copyright (C) 1999-2012 The DiSTI Corporation<br>
8 Orlando, FL USA<br>
9 All rights reserved.<br>
10 
11  This file is copyrighted software and contains proprietary trade secrets of
12 DiSTI, and embodies substantial creative efforts as well as confidential
13 information, ideas, and expressions.
14 
15  Permission to use, and copy this software and its documentation for any
16 purpose is hereby granted per the Distribution Agreement and/or the Licensing
17 Agreement signed with DiSTI. This permission is granted provided that:
18  1. The above copyright notice appears in all copies.
19  2. That both the copyright notice and this permission notice appear in
20  the supporting documentation.
21  3. That the names DiSTI and GL Studio not be used in advertising or
22  publicity pertaining to distribution of the software without specific,
23  written prior permission of DiSTI.
24 
25  Permission to modify the software is granted, but not the right to
26 distribute the source code whether modified, or non-modified. Modifying the
27 software might invalidate the DO-178B certification package.
28 
29  Permission to distribute binaries produced by compiling source code, or
30 modified source code is granted, provided you:
31  1. Provide your name and address as the primary contact for the support
32  of your modified version.
33  2. Retain our contact information in regard to use of the base software.
34 
35  DiSTI does not provide warranty for this software or guarantee that it
36 satisfies any specification or requirement unless otherwise stated in a
37 specific contractual arrangement between the customer and DiSTI.
38 
39 */
40 #ifndef _GLS_ASSERT_H
41 #define _GLS_ASSERT_H
42 
43 #include "gls_include.h"
44 
45 #if defined( GLS_DEBUG ) && defined( GLS_UNIT_TEST )
46 #pragma BullseyeCoverage save off
47 #include <exception>
48 #include <sstream>
49 /** This class is used to report library assertion failures when running unit tests
50  * in debug mode. ( GLS_DEBUG && GLS_UNIT_TEST )
51  */
52 class GlsAssertionFailure : public std::exception
53 {
54 public:
55  /** Constructor - create an instance with the given attributes
56  * \param msg string message describing the assertion failure
57  * \pre none
58  * \post GlsAssert macro is disabled
59  */
60  GlsAssertionFailure( const char* const msg ) :
61  std::exception( msg )
62  {
63  // disable further GlsAssert s from throwing
64  _glsAssertIsEnabled = false;
65  }
66 
67  /** Dtor
68  * \pre none
69  * \post the GlsAssert macro is reenabled
70  */
71  ~GlsAssertionFailure() { _glsAssertIsEnabled = true; }
72 
73  /** Determine if the GlsAssert macro should throw a GlsAssertionFailure in the debug unit test
74  * build. GlsAssert is disabled after the first assertion failure so that further potential
75  * assertion failures are not thrown as the stack unwinds. One assertion failure stops the
76  * unit test immediately so further assertion failures do not need to be thrown. The GlsAssert
77  * macro is reenabled by the destructor for this class which is called once the assertion has
78  * been caught and handled by the unit test runner.
79  * \pre none
80  * \post none
81  * \return true if the GlsAssert macro should throw a GlsAssertionFailure else false
82  */
83  static bool GlsAssertIsEnabled( void ) { return( _glsAssertIsEnabled ); }
84 
85 protected:
86  static bool _glsAssertIsEnabled; /**< true if the GlsAssert macro should throw
87  * a GlsAssertionFailure else false */
88 };
89 #pragma BullseyeCoverage restore
90 #endif // ( GLS_DEBUG && GLS_UNIT_TEST )
91 
92 #if defined( GLS_DEBUG )
93  #if defined( GLS_UNIT_TEST )
94  // define GlsAssert macro to throw a GlsAssertionFailure when running unit tests in debug mode
95  #define GlsAssert( _exp ) if( !( _exp ) && GlsAssertionFailure::GlsAssertIsEnabled() ) \
96  { \
97  std::ostringstream msg; \
98  msg << "( " << #_exp << " ) at " << __FILE__ << " (" << __LINE__ << ")"; \
99  throw( GlsAssertionFailure( msg.str().c_str() ) ); \
100  }
101  #else
102  // define GlsAssert macro to use assert() if in debug mode and not unit test mode
103  #include <assert.h>
104  #define GlsAssert( _exp ) assert( _exp )
105  #endif // GLS_UNIT_TEST
106 #else
107  // define GlsAssert macro as empty if not in debug mode
108  #define GlsAssert( _exp )
109 #endif // defined( GLS_DEBUG )
110 
111 #define GlsError() GlsAssert( false )
112 
113 #endif // _GLS_ASSERT_H
This header defines any preprocessor defines needed to configure the GL Studio DO-178B Runtime Librar...