GL Studio C++ Runtime API
disti_assert.h
Go to the documentation of this file.
1 /*! \file
2  \brief Contains the DistiAssert macro.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd., Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef INCLUDED_DISTI_ASSERT_H
41 #define INCLUDED_DISTI_ASSERT_H
42 
43 #include "disti_include.h"
44 #include "gls_cpp_lang_support.h"
45 #include <stddef.h>
46 
47 namespace disti
48 {
49 class DistiException;
50 DISTI_EXPORT void swap( DistiException& first, DistiException& second );
51 
52 /** DistiException just contains a string error message */
54 {
55 public:
56  explicit DISTI_EXPORT DistiException( const char* what );
57  DISTI_EXPORT DistiException( const DistiException& other );
58  DISTI_EXPORT DistiException& operator=( DistiException other );
59 #ifdef DISTI_HAS_RVAL_REFS
60  DISTI_EXPORT DistiException( DistiException&& other );
61 #endif
62  virtual DISTI_EXPORT ~DistiException();
63  virtual DISTI_EXPORT const char* what() const; // never returns NULL
64  friend DISTI_EXPORT void swap( DistiException& first, DistiException& second );
65 
66 private:
67  char* _what;
68  size_t _len;
69 };
70 
71 /** The callback type. It receives an error message string */
72 typedef void ( *DistiAssertHandler )( const char* errMessage );
73 
74 /* This function enables users to override the default behavior of DistiAssert(), which is used
75  * throughout the GL Studio code base to test prerequisites etc. The user can set a function to
76  * be called when DistiAssert fires. Only one callback can be registered, so calling the set
77  * function multiple times overwrites the previous handler. If the given handler is null,
78  * DistiAssert() reverts to its default behavior.
79  * \note This function is deprecated; Use SetGlsVerifyHandler or SetGlsAssertHandler instead.
80  */
81 DISTI_EXPORT void SetDistiAssertHandler( DistiAssertHandler );
82 
83 /* This function enables users to override the default behavior of GLS_VERIFY(), which is used
84  * throughout the GL Studio code base to test prerequisites etc. The user can set a function to
85  * be called when GLS_VERIFY fires. Only one callback can be registered, so calling the set
86  * function multiple times overwrites the previous handler. If the given handler is null,
87  * GLS_VERIFY() reverts to its default behavior.
88  */
89 DISTI_EXPORT void SetGlsVerifyHandler( DistiAssertHandler );
90 
91 /* This function enables users to override the default behavior of GLS_ASSERT(), which is used
92  * throughout the GL Studio code base to test prerequisites etc. The user can set a function to
93  * be called when GLS_ASSERT fires. Only one callback can be registered, so calling the set
94  * function multiple times overwrites the previous handler. If the given handler is null,
95  * GLS_ASSERT() reverts to its default behavior.
96  */
97 DISTI_EXPORT void SetGlsAssertHandler( DistiAssertHandler );
98 
99 /* DistiAssert
100  * \note This function is deprecated; Use GLS_ASSERT or GLS_VERIFY instead.
101  * If a callback is registered, that function is invoked when an assertion fails (exp is false).
102  * If the callback is null, when the assertion fails, DistiAssert will popup a messagebox allowing
103  * the user to Abort, Retry(Debug) or Ignore the error. Retry will cause a User breakpoint on the
104  * error, Abort will throw a DistiException describing the error, and Ignore does nothing.
105  *
106  * At present, the function does the same thing in debug and release modes.
107  */
108 #if defined( GLS_DEBUG )
109 # define DistiAssert( exp ) (void)( ( exp ) || ( ::disti::DistiAssertDebug( # exp, __FILE__, __LINE__ ), 0 ) )
110 #else
111 # define DistiAssert( exp ) (void)( ( exp ) || ( ::disti::DistiAssertRelease( # exp, __FILE__, __LINE__ ), 0 ) )
112 #endif
113 
114 #define GLS_UNUSED( exp ) \
115  do \
116  { \
117  (void)sizeof( exp ); \
118  } while( 0 )
119 
120 /** In builds where GLS_DEBUG is defined OR where NDEBUG is not defined, this macro has the following behavior:
121  *
122  * 1. If a callback is registered with SetGlsAssertHandler(), that handler is invoked when an assertion
123  * fails (\a exp is false).
124  *
125  * 2. If no handler is registered and the assertion fails, on Windows GLS_ASSERT will popup a message box
126  * allowing the user to Abort, Retry (Debug), or Ignore the error. Retry will cause a User breakpoint
127  * on the error, Abort will throw a DistiException describing the error, and Ignore does nothing.
128  *
129  * If none of the aforementioned symbols are defined, this function does nothing, and does not evaluate \a exp.
130  *
131  * \note Use this function instead of the deprecated DistiAssert() macro for checks that should be tested in
132  * Debug builds only.
133  */
134 #if defined( GLS_DEBUG ) || !defined( NDEBUG )
135 # define GLS_ASSERT( exp ) \
136  do \
137  { \
138  (void)( ( exp ) || ( ::disti::GlsAssert( #exp, __FILE__, __LINE__ ), 0 ) ); \
139  } while( 0 )
140 #else
141 # define GLS_ASSERT( exp ) GLS_UNUSED( exp )
142 #endif
143 
144 /** The GLS_VERIFY() macro has the following behavior:
145  *
146  * 1. If a callback is registered with SetGlsVerifyHandler(), that handler is invoked when \a exp is false.
147  *
148  * 2. If no handler is registered and the \a exp is false, on Windows GLS_VERIFY() will popup a message box
149  * allowing the user to Abort, Retry (Debug), or Ignore the error. Retry will cause a User breakpoint
150  * on the error, Abort will throw a DistiException describing the error, and Ignore does nothing.
151  *
152  * \note Use this function instead of the deprecated DistiAssert() macro for checks that should be tested in
153  * both Debug and Release builds.
154  */
155 #define GLS_VERIFY( exp ) (void)( ( exp ) || ( ::disti::GlsVerify( #exp, __FILE__, __LINE__ ), 0 ) )
156 
157 /** Do not call DistiAssertDebug directly, instead use the DistiAssert macro
158  * \note This function is deprecated; Use GlsAssert or GlsVerify instead.
159  */
160 DISTI_EXPORT void DistiAssertDebug( const char* expression, const char* filename, unsigned int lineNumber );
161 /** Do not call DistiAssertRelease directly, instead use the DistiAssert macro
162  * \note This function is deprecated; Use GlsAssert or GlsVerify instead.
163  */
164 DISTI_EXPORT void DistiAssertRelease( const char* expression, const char* filename, unsigned int lineNumber );
165 
166 /** Do not call GlsVerify directly, instead use the GLS_VERIFY macro */
167 DISTI_EXPORT void GlsVerify( const char* expression, const char* filename, unsigned int lineNumber );
168 
169 /** Do not call GlsAssert directly, instead use the GLS_ASSERT macro */
170 DISTI_EXPORT void GlsAssert( const char* expression, const char* filename, unsigned int lineNumber );
171 
172 /** Assert the pointer is not null and return it */
173 template<class T>
174 T* ValidatePointer( const T* const p )
175 {
176  DistiAssert( p );
177  return p;
178 }
179 
180 } // namespace disti
181 
182 #endif
T * ValidatePointer(const T *const p)
Definition: disti_assert.h:174
void DistiAssertDebug(const char *expression, const char *filename, unsigned int lineNumber)
A file for all GL Studio files to include.
void(* DistiAssertHandler)(const char *errMessage)
Definition: disti_assert.h:72
Definition: disti_assert.h:53
void DistiAssertRelease(const char *expression, const char *filename, unsigned int lineNumber)
void GlsVerify(const char *expression, const char *filename, unsigned int lineNumber)
Macros and helper code to determine what subset of C++11/14/17 is available.
void GlsAssert(const char *expression, const char *filename, unsigned int lineNumber)
Definition: bmpimage.h:46