GL Studio 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) 2015 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 
50 class DistiException;
51 DISTI_EXPORT void swap( DistiException& first, DistiException& second );
52 
53 /** DistiException just contains a string error message */
55 {
56 public:
57  explicit DISTI_EXPORT DistiException(const char* what);
58  DISTI_EXPORT DistiException(const DistiException& other);
59  DISTI_EXPORT DistiException& operator=(DistiException other);
60 #ifdef DISTI_HAS_RVAL_REFS
61  DISTI_EXPORT DistiException(DistiException&& other);
62 #endif
63  virtual DISTI_EXPORT ~DistiException();
64  virtual DISTI_EXPORT const char *what() const; // never returns NULL
65  friend DISTI_EXPORT void swap( DistiException& first, DistiException& second );
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 
100 /* DistiAssert
101  * \note This function is deprecated; Use GLS_ASSERT or GLS_VERIFY instead.
102  * If a callback is registered, that function is invoked when an assertion fails (exp is false).
103  * If the callback is null, when the assertion fails, DistiAssert will popup a messagebox allowing
104  * the user to Abort, Retry(Debug) or Ignore the error. Retry will cause a User breakpoint on the
105  * error, Abort will throw a DistiException describing the error, and Ignore does nothing.
106  *
107  * At present, the function does the same thing in debug and release modes.
108  */
109 #if defined(GLS_DEBUG)
110 #define DistiAssert(exp) (void)( (exp) || (::disti::DistiAssertDebug(#exp, __FILE__, __LINE__),0) )
111 #else
112 #define DistiAssert(exp) (void)( (exp) || (::disti::DistiAssertRelease(#exp, __FILE__, __LINE__),0) )
113 #endif
114 
115 #define GLS_UNUSED(exp) do { (void)sizeof(exp); } while(0)
116 
117 /** In builds where GLS_DEBUG is defined OR where NDEBUG is not defined, this macro has the following behavior:
118  *
119  * 1. If a callback is registered with SetGlsAssertHandler(), that handler is invoked when an assertion
120  * fails (\a exp is false).
121  *
122  * 2. If no handler is registered and the assertion fails, on Windows GLS_ASSERT will popup a message box
123  * allowing the user to Abort, Retry (Debug), or Ignore the error. Retry will cause a User breakpoint
124  * on the error, Abort will throw a DistiException describing the error, and Ignore does nothing.
125  *
126  * If none of the aforementioned symbols are defined, this function does nothing, and does not evaluate \a exp.
127  *
128  * \note Use this function instead of the deprecated DistiAssert() macro for checks that should be tested in
129  * Debug builds only.
130  */
131 #if defined(GLS_DEBUG) || !defined(NDEBUG)
132 # define GLS_ASSERT(exp) do { (void)( (exp) || (::disti::GlsAssert(#exp, __FILE__, __LINE__),0) ); } while(0)
133 #else
134 # define GLS_ASSERT(exp) GLS_UNUSED(exp)
135 #endif
136 
137 /** The GLS_VERIFY() macro has the following behavior:
138  *
139  * 1. If a callback is registered with SetGlsVerifyHandler(), that handler is invoked when \a exp is false.
140  *
141  * 2. If no handler is registered and the \a exp is false, on Windows GLS_VERIFY() will popup a message box
142  * allowing the user to Abort, Retry (Debug), or Ignore the error. Retry will cause a User breakpoint
143  * on the error, Abort will throw a DistiException describing the error, and Ignore does nothing.
144  *
145  * \note Use this function instead of the deprecated DistiAssert() macro for checks that should be tested in
146  * both Debug and Release builds.
147  */
148 #define GLS_VERIFY(exp) (void)( (exp) || (::disti::GlsVerify(#exp, __FILE__, __LINE__),0) )
149 
150 /** Do not call DistiAssertDebug directly, instead use the DistiAssert macro
151  * \note This function is deprecated; Use GlsAssert or GlsVerify instead.
152  */
153 DISTI_EXPORT void DistiAssertDebug(const char *expression, const char *filename, unsigned int lineNumber);
154 /** Do not call DistiAssertRelease directly, instead use the DistiAssert macro
155  * \note This function is deprecated; Use GlsAssert or GlsVerify instead.
156  */
157 DISTI_EXPORT void DistiAssertRelease(const char *expression, const char *filename, unsigned int lineNumber);
158 
159 /** Do not call GlsVerify directly, instead use the GLS_VERIFY macro */
160 DISTI_EXPORT void GlsVerify(const char *expression, const char *filename, unsigned int lineNumber);
161 
162 /** Do not call GlsAssert directly, instead use the GLS_ASSERT macro */
163 DISTI_EXPORT void GlsAssert(const char *expression, const char *filename, unsigned int lineNumber);
164 
165 /** Assert the pointer is not null and return it */
166 template< class T >
167 T* ValidatePointer( const T* const p )
168 {
169  DistiAssert( p );
170  return p;
171 }
172 
173 } // namespace disti
174 
175 #endif
T * ValidatePointer(const T *const p)
Definition: disti_assert.h:167
void DistiAssertDebug(const char *expression, const char *filename, unsigned int lineNumber)
void(* DistiAssertHandler)(const char *errMessage)
Definition: disti_assert.h:72
A file for all GL Studio files to include.
Definition: disti_assert.h:54
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