1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (c) 2019, Nefelus Inc
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // * Redistributions of source code must retain the above copyright notice, this
11 //   list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above copyright notice,
14 //   this list of conditions and the following disclaimer in the documentation
15 //   and/or other materials provided with the distribution.
16 //
17 // * Neither the name of the copyright holder nor the names of its
18 //   contributors may be used to endorse or promote products derived from
19 //   this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 #pragma once
34 
35 #include "odb.h"
36 
37 #ifdef __GNUC__
38 #define ADS_FORMAT_PRINTF(F, A) __attribute__((format(printf, F, A)))
39 #else
40 #define ADS_FORMAT_PRINTF(F, A)
41 #endif
42 
43 namespace odb {
44 
45 /////////////////////////////////
46 // Base exception class
47 /////////////////////////////////
48 class ZException
49 {
50  public:
51   const char* _msg;
52   bool _free_msg;
53 
54   ZException();
55   ZException(const char* fmt, ...) ADS_FORMAT_PRINTF(2, 3);
56   ZException(const ZException& ex);
57   ~ZException();
58 };
59 
60 class ZOutOfMemory : public ZException
61 {
62  public:
ZOutOfMemory()63   ZOutOfMemory()
64   {
65     _free_msg = false;
66     _msg = "Out of memory";
67   }
68 };
69 
70 class ZIOError : public ZException
71 {
72  public:
73   ZIOError(int err);
74   ZIOError(int err, const char* msg);
75 };
76 
77 class ZAssert : public ZException
78 {
79  public:
80   ZAssert(const char* expr, const char* file, int line);
81 };
82 
83 // See http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert
84 // Avoids unused variable warnings.
85 #ifdef NDEBUG
86 #define ZASSERT(x)    \
87   do {                \
88     (void) sizeof(x); \
89   } while (0)
90 #else
91 #define ZASSERT(x) assert(x)
92 #endif
93 
94 #define ZALLOCATED(expr)    \
95   do {                      \
96     if ((expr) == NULL)     \
97       throw ZOutOfMemory(); \
98   } while (0);
99 
100 }  // namespace odb
101