1 // exception standard header for Microsoft
2 #pragma once
3 #ifndef _EXCEPTION_
4 #define _EXCEPTION_
5 #ifndef RC_INVOKED
6
7 #include <xstddef>
8
9 #ifdef _MSC_VER
10  #pragma pack(push,_CRT_PACKING)
11  #pragma warning(push,3)
12 #endif  /* _MSC_VER */
13
14 #ifdef _CRT_EXCEPTION_NO_MALLOC
15
16   #pragma push_macro("malloc")
17   #pragma push_macro("free")
18
19  #undef malloc
20  #undef free
21 #endif /* _CRT_EXCEPTION_NO_MALLOC */
22
23 _STD_BEGIN
24
25   #define _USE_EXCEPTION \
26     using _STDEXT exception;
27
28   #define _USE_BAD_EXCEPTION \
29     using _STDEXT bad_alloc; \
30     using _STDEXT bad_exception;
31
32   #define _USE_EX \
33     using ::set_terminate; using ::terminate_handler; using ::terminate; \
34     using ::set_unexpected; using ::unexpected_handler; using ::unexpected;
35
36 _STD_END
37
38  #if _HAS_EXCEPTIONS
39
40  #include <eh.h>
41  #include <malloc.h>
42
43  #if !defined(_WIN32)
44   #error ERROR: Only Win32 targets supported!
45  #endif /* !defined(_WIN32) */
46
47  #ifndef _CRTIMP
48
49   #ifdef    _DLL
50      #define _CRTIMP __declspec(dllimport)
51
52   #else    /* ndef _DLL */
53      #define _CRTIMP
54   #endif    /* _DLL */
55
56  #endif    /* _CRTIMP */
57
58 #ifndef _MCRTIMP
59 #ifdef  _DLL
60 #define _MCRTIMP __declspec(dllimport)
61 #else     /* ndef _DLL */
62 #define _MCRTIMP
63 #endif  /* _DLL */
64 #endif  /* _CRTIMP */
65
66 #ifndef _CRTIMP_PURE
67  #if defined(_M_CEE_PURE) || defined(_STATIC_CPPLIB)
68   #define _CRTIMP_PURE
69  #else
70   #define _CRTIMP_PURE _CRTIMP
71  #endif
72 #endif
73
74 #ifndef _ERRCODE_DEFINED
75 #define _ERRCODE_DEFINED
76 /* errcode is deprecated in favor or errno_t, which is part of the standard proposal */
77 _CRT_OBSOLETE(errno_t) typedef int errcode;
78 typedef int errno_t;
79 #endif
80
81
82 typedef const char *__exString;
83 extern "C" _Check_return_ size_t __cdecl strlen(_In_z_ const char *);
84 #if __STDC_WANT_SECURE_LIB__
85 extern "C" _CRTIMP_ALTERNATIVE errno_t __cdecl strcpy_s(_Out_z_cap_(_SizeInBytes) char * _Dst, _In_ size_t _SizeInBytes, _In_z_ const char * _Src);
86 #define _CRT_SECURE_STRCPY(dest, destsize, source) ::strcpy_s((dest), (destsize), (source))
87 #else
88 extern "C" char * __cdecl strcpy(_Pre_cap_for_(_Source) _Post_z_ char *_Dst, _In_z_ const char *_Source);
89 #define _CRT_SECURE_STRCPY(dest, destsize, source) ::strcpy((dest), (source))
90 #endif
91
92  _STD_BEGIN
93
94 class _CRTIMP_PURE exception
95     {    // base of all library exceptions
96 public:
97 #ifdef _M_CEE_PURE
98     __CLR_OR_THIS_CALL exception()
99              : _m_what(NULL), _m_doFree(0)
100       { }
101     __CLR_OR_THIS_CALL exception(const char *const& _What)
102       {
103              if (_What != NULL)
104              {
105                     size_t _Buf_size = strlen( _What ) + 1;
106 #pragma warning(push)
107 #pragma warning(disable:4996)
108                     _m_what = static_cast< char * >( malloc( _Buf_size ) );
109 #pragma warning(pop)
110                     if ( _m_what != NULL )
111                     {
112                           _CRT_SECURE_STRCPY( const_cast<char *>(_m_what), _Buf_size, _What );
113                     }
114              }
115              else
116              {
117                     _m_what = NULL;
118              }
119              _m_doFree = 1;
120       }
121     __CLR_OR_THIS_CALL exception(const char *const& _What, int)
122       {
123              _m_what = _What;
124              _m_doFree = 0;
125       }
126     __CLR_OR_THIS_CALL exception(const exception& _That)
127       {
128              _m_doFree = _That._m_doFree;
129              if (_m_doFree)
130              {
131                     if (_That._m_what != NULL)
132                     {
133                           size_t _Buf_size = strlen( _That._m_what ) + 1;
134 #pragma warning(push)
135 #pragma warning(disable:4996)
136                           _m_what = static_cast< char * >( malloc( _Buf_size ) );
137 #pragma warning(pop)
138                           if (_m_what != NULL)                          {
139                                  _CRT_SECURE_STRCPY( const_cast<char *>(_m_what), _Buf_size, _That._m_what );
140                           }
141                     }
142                     else
143                     {
144                           _m_what = NULL;
145                     }
146              }
147              else
148                   _m_what = _That._m_what;
149       }
150     exception& __CLR_OR_THIS_CALL operator=(const exception& _That)
151       {
152              if (this != &_That)
153              {
154                     _m_doFree = _That._m_doFree;
155                     if (_m_doFree)
156                     {
157                           if (_That._m_what != NULL)
158                           {
159                                  size_t _Buf_size = strlen( _That._m_what ) + 1;
160 #pragma warning(push)
161 #pragma warning(disable:4996)
162                                  _m_what = static_cast< char * >( malloc( _Buf_size ) );
163 #pragma warning(pop)
164                                  if (_m_what != NULL)                          {
165                                         _CRT_SECURE_STRCPY( const_cast<char *>(_m_what), 
166                                                         _Buf_size, _That._m_what );
167                                  }
168                           }
169                           else
170                           {
171                                  _m_what = NULL;
172                           }
173                     }
174                     else
175                          _m_what = _That._m_what;
176              }
177              return *this;
178       }
179     virtual __CLR_OR_THIS_CALL ~exception()
180       {
181              if (_m_doFree)
182 #pragma warning(push)
183 #pragma warning(disable:4996)
184                     free( const_cast< char * >( _m_what ) );
185 #pragma warning(pop)
186       }
187     virtual const char* __CLR_OR_THIS_CALL what() const
188       {
189              if ( _m_what != NULL )
190                     return _m_what;
191              else
192                     return "Unknown exception";
193       }
194 #else /* _M_CEE_PURE */
195       __CLR_OR_THIS_CALL exception();
196       __CLR_OR_THIS_CALL exception(const char *const&);
197       __CLR_OR_THIS_CALL exception(const char *const&, int);
198       __CLR_OR_THIS_CALL exception(const exception&);
199       exception& __CLR_OR_THIS_CALL operator=(const exception&);
200       virtual __CLR_OR_THIS_CALL ~exception();
201       virtual const char * __CLR_OR_THIS_CALL what() const;
202 #endif /* _M_CEE_PURE */
203 private:
204     const char *_m_what;
205     int _m_doFree;
206     };
207
208 _USE_EX
209
210 typedef void (__cdecl *_Prhand)(const exception&);
211
212 #if defined(_M_CEE_PURE) || defined(MRTDLL)
213 _MRTIMP bool __cdecl _uncaught_exception_m();
214 inline bool __clrcall uncaught_exception() { return _uncaught_exception_m(); }
215 #else
216 _CRTIMP2 bool __cdecl uncaught_exception();
217 #endif
218
219 _STD_END
220
221  #else /* _HAS_EXCEPTIONS */
222
223         // CLASS exception
224 _STDEXT_BEGIN
225 class exception;
226 _STDEXT_END
227
228 _STD_BEGIN
229
230 _USE_EXCEPTION
231
232 typedef void (__cdecl *_Prhand)(const exception&);
233 extern _CRTIMP2_NCEEPURE _Prhand _Raise_handler;    // pointer to raise handler
234
235 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Throw(const exception&);    // throw the exception
236
237 _STD_END
238
239 _STDEXT_BEGIN
240 class exception
241     {    // base of all library exceptions
242 public:
243
244     static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew)
245         {    // register a handler for _Raise calls
246         const _STD _Prhand _Pold = _STD _Raise_handler;
247         _STD _Raise_handler = _Pnew;
248         return (_Pold);
249         }
250
251     // this constructor is necessary to compile 
252     // successfully header new for _HAS_EXCEPTIONS==0 scenario
253     explicit __CLR_OR_THIS_CALL exception(const char *_Message = _MESG("unknown"), int x=1)
254         _THROW0()
255         : _Ptr(_Message)
256         {    // construct from message string
257                           (void)x;
258         }
259
260     __CLR_OR_THIS_CALL exception(const exception& _Right) _THROW0()
261         : _Ptr(_Right._Ptr)
262         {    // construct by copying _Right
263         }
264
265     exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) _THROW0()
266         {    // assign _Right
Lines 267 ... 276 are skipped.
277         return (_Ptr != 0 ? _Ptr : "unknown exception");
278         }
279
280     void __CLR_OR_THIS_CALL _Raise() const
281         {    // raise the exception
282         if (_STD _Raise_handler != 0)
283             (*_STD _Raise_handler)(*this);    // call raise handler if present
284
285         _Doraise();    // call the protected virtual
286         _RAISE(*this);    // raise this exception
287         }
288
289 protected:
290     virtual void __CLR_OR_THIS_CALL _Doraise() const
291         {    // perform class-specific exception handling
292         }
293
294 protected:
295     const char *_Ptr;    // the message pointer
296     };
297 _STDEXT_END
298
299 _STD_BEGIN
300
301         // TYPES
302 typedef void (__cdecl *terminate_handler)();
303 typedef void (__cdecl *unexpected_handler)();
304
305         // DUMMY FUNCTION DECLARATIONS
306 inline terminate_handler __CRTDECL set_terminate(terminate_handler)
307     _THROW0()
308     {    // register a terminate handler
309     return 0;
310     }
311
312 inline unexpected_handler __CRTDECL set_unexpected(unexpected_handler)
313     _THROW0()
314     {    // register an unexpected handler
315     return 0;
316     }
317
318 inline void __CRTDECL terminate()
319     {    // handle exception termination
320     }
321
322 inline void __CRTDECL unexpected()
323     {    // handle unexpected exception
324     }
325
326 _CRTIMP2 bool __cdecl uncaught_exception();    // handle uncaught exception
327
328 _STD_END
329
330  #endif /* _HAS_EXCEPTIONS */
331
332 #if _HAS_EXCEPTIONS
333 _STD_BEGIN
334 #else
335 _STDEXT_BEGIN
336 #endif
337
338         // CLASS bad_exception
339 class bad_exception : public exception
340     {    // base of all bad exceptions
341 public:
342     __CLR_OR_THIS_CALL bad_exception(const char *_Message = _MESG("bad exception"))
343         _THROW0()
344         : exception(_Message)
345         {    // construct from message string
346         }
347
348     virtual __CLR_OR_THIS_CALL ~bad_exception() _THROW0()
349         {    // destroy the object
350         }
351
352  #if !_HAS_EXCEPTIONS
353 protected:
354     virtual void __CLR_OR_THIS_CALL _Doraise() const
355         {    // raise this exception
356         _RAISE(*this);
357         }
358  #endif /* _HAS_EXCEPTIONS */
359
360     };
361
362
363 static const char * _bad_alloc_Message = _MESG("bad allocation");
364
365         // CLASS bad_alloc
366 class bad_alloc : public exception
367     {    // base of all bad allocation exceptions
368 public:
369     __CLR_OR_THIS_CALL bad_alloc(const char *_Message) _THROW0()
370         : exception(_Message)
371         {    // construct from message string
372         }
373
374     __CLR_OR_THIS_CALL bad_alloc() _THROW0()
375         : exception(_bad_alloc_Message, 1)
376         {    // construct from message string with no memory allocation
377         }
378
379     virtual __CLR_OR_THIS_CALL ~bad_alloc() _THROW0()
380         {    // destroy the object
381         }
382
383  #if !_HAS_EXCEPTIONS
384 protected:
385     virtual void __CLR_OR_THIS_CALL _Doraise() const
386         {    // perform class-specific exception handling
387         _RAISE(*this);
388         }
389 #endif  /* _HAS_EXCEPTIONS */
390
391     };
392
393 #if _HAS_EXCEPTIONS
394 _STD_END
395 #else
396 _STDEXT_END
397
398 _STD_BEGIN
399
400 _USE_BAD_EXCEPTION
401
402 _STD_END
403
404 #endif
405
406 #ifdef _CRT_EXCEPTION_NO_MALLOC
407
408   #pragma pop_macro("free")
409   #pragma pop_macro("malloc")
410
411 #endif /* _CRT_EXCEPTION_NO_MALLOC */
412 #ifdef _MSC_VER
413  #pragma warning(pop)
414  #pragma pack(pop)
415 #endif
416
417 #endif /* RC_INVOKED */
418 #endif /* _EXCEPTION_ */
419
420 /*
421  * Copyright (c) 1992-2007 by P.J. Plauger.  ALL RIGHTS RESERVED.
422  * Consult your license regarding permissions and restrictions.
423  V5.03:0009 */
424