SMIL  1.0.4
DErrors.h
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _DERRORS_H
31 #define _DERRORS_H
32 
33 #include <exception>
34 #include <iostream>
35 #include <sstream>
36 
37 #include "DCommon.h"
38 
39 using namespace std;
40 
41 namespace smil
42 {
48  enum RES_T {
49  RES_OK = 1,
50  RES_ERR = -100,
51  RES_ERR_BAD_ALLOCATION,
52  RES_ERR_BAD_SIZE,
53  RES_ERR_IO,
54  RES_ERR_NOT_IMPLEMENTED,
55  RES_ERR_UNKNOWN
56  };
57 
58  inline const char *getErrorMessage(const RES_T &res)
59  {
60  switch (res) {
61  case RES_OK:
62  return "ok";
63  case RES_ERR_BAD_ALLOCATION:
64  return "Bad allocation";
65  default:
66  return "Unknown error";
67  }
68  }
69 
70  class Error
71 #ifndef SWIG
72  : public exception
73 #endif // SWIG
74  {
75  public:
76  Error() noexcept(true) : line(0)
77  {
78  }
79 
80  Error(const string &descr) noexcept(true) : description(cleanDescr(descr))
81  {
82  buildMessage();
83  }
84 
85  Error(const string &descr, const char *func, const char *_file,
86  const int _line, const char *expr) noexcept(true)
87  : description(cleanDescr(descr)), function(func), file(_file),
88  line(_line), expression(expr)
89  {
90  buildMessage();
91  }
92 
93  Error(const string &descr, const char *func, const char *_file,
94  const int _line) noexcept(true)
95  : description(cleanDescr(descr)), function(func), file(_file),
96  line(_line)
97  {
98  buildMessage();
99  }
100 
101  Error(const char *func, const char *_file, const int _line,
102  const char *expr) noexcept(true)
103  : function(func), file(_file), line(_line), expression(expr)
104  {
105  buildMessage();
106  }
107 
108  Error(const string &descr, const char *func,
109  const char *expr) noexcept(true)
110  : description(cleanDescr(descr)), function(func), expression(expr)
111  {
112  buildMessage();
113  }
114 
115  Error(const char *func, const char *expr) noexcept(true)
116  : function(func), expression(expr)
117  {
118  buildMessage();
119  }
120 
121  virtual ~Error() noexcept(true)
122  {
123  }
124 
125  virtual const char *what() const noexcept(true)
126  {
127  return this->message.c_str();
128  }
129 
130  void show()
131  {
132  cout << "Error:" << this->what() << endl;
133  }
134 
135  void buildMessage()
136  {
137  ostringstream buf;
138  if (!function.empty())
139  buf << "\n in function: " << function;
140  if (!description.empty())
141  buf << "\n error: " << description;
142 #ifndef NDEBUG
143  if (!expression.empty()) {
144  if (description.empty())
145  buf << "\n error: assert " << expression;
146  else
147  buf << " ( assert " << expression << " )";
148  }
149  if (!file.empty())
150  buf << "\n file: " << file << ":" << line;
151 #endif // NDEBUG
152  this->message = buf.str();
153  }
154 
155  private:
156  inline string cleanDescr(const string descr)
157  {
158  if (descr[0] != '"')
159  return descr;
160  else
161  return descr.substr(1, descr.length() - 2);
162  }
163 
164  string description;
165  string function;
166  string file;
167 #if defined NDEBUG && defined __clang__
168  SMIL_UNUSED
169 #endif // NDEBUG && __clang__
170  int line;
171  string expression;
172  string message;
173  };
174 
175 #define ASSERT_1_ARG(func, file, line, expr) \
176  if (!(expr)) { \
177  Error(func, file, line, #expr).show(); \
178  return RES_ERR; \
179  }
180 #define ASSERT_2_ARGS(func, file, line, expr, errCode) \
181  if (!(expr)) { \
182  Error(#errCode, func, file, line, #expr).show(); \
183  return errCode; \
184  }
185 #define ASSERT_3_ARGS(func, file, line, expr, errCode, retVal) \
186  if (!(expr)) { \
187  Error(#errCode, func, file, line, #expr).show(); \
188  return retVal; \
189  }
190 
191 #define ERR_MSG(msg) Error(msg, __FUNC__, __FILE__, __LINE__).show()
192 
193 #define ASSERT_NARGS_CHOOSER(...) \
194  GET_4TH_ARG(__VA_ARGS__, ASSERT_3_ARGS, ASSERT_2_ARGS, ASSERT_1_ARG, ...)
195 
196 #ifdef _MSC_VER
197 #define ASSERT(...) \
198  EXPAND(ASSERT_NARGS_CHOOSER(__VA_ARGS__)(__FUNC__, __FILE__, __LINE__, \
199  __VA_ARGS__))
200 #else // _MSC_VER
201 #define ASSERT(...) \
202  ASSERT_NARGS_CHOOSER(__VA_ARGS__)(__FUNC__, __FILE__, __LINE__, __VA_ARGS__)
203 #endif // _MSC_VER
204 
207 } // namespace smil
208 
209 #endif // _DERRORS_H
Definition: DErrors.h:74