SMIL  0.9.1
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'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
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 {
49  enum RES_T
50  {
51  RES_OK = 1,
52  RES_ERR = -100,
53  RES_ERR_BAD_ALLOCATION,
54  RES_ERR_BAD_SIZE,
55  RES_ERR_IO,
56  RES_ERR_NOT_IMPLEMENTED,
57  RES_ERR_UNKNOWN
58  };
59 
60  inline const char *getErrorMessage(const RES_T &res)
61  {
62  switch(res)
63  {
64  case RES_OK:
65  return "ok";
66  case RES_ERR_BAD_ALLOCATION:
67  return "Bad allocation";
68  default:
69  return "Unknown error";
70  }
71  }
72 
73  class Error
74 #ifndef SWIG
75  : public exception
76 #endif // SWIG
77  {
78  public:
79  Error() throw()
80  {}
81  Error(const string &descr) throw()
82  : description(cleanDescr(descr))
83  {
84  buildMessage();
85  }
86  Error(const string &descr, const char *func, const char *_file, const int _line, const char *expr) throw()
87  : description(cleanDescr(descr)),
88  function(func),
89  file(_file),
90  line(_line),
91  expression(expr)
92  {
93  buildMessage();
94  }
95  Error(const string &descr, const char *func, const char *_file, const int _line) throw()
96  : description(cleanDescr(descr)),
97  function(func),
98  file(_file),
99  line(_line)
100  {
101  buildMessage();
102  }
103  Error(const char *func, const char *_file, const int _line, const char *expr) throw()
104  : function(func),
105  file(_file),
106  line(_line),
107  expression(expr)
108  {
109  buildMessage();
110  }
111  Error(const string &descr, const char *func, const char *expr) throw()
112  : description(cleanDescr(descr)),
113  function(func),
114  expression(expr)
115  {
116  buildMessage();
117  }
118  Error(const char *func, const char *expr) throw()
119  : function(func),
120  expression(expr)
121  {
122  buildMessage();
123  }
124  virtual ~Error() throw()
125  {}
126  virtual const char* what() const throw()
127  {
128  return this->message.c_str();
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  {
145  if (description.empty())
146  buf << "\n error: assert " << expression;
147  else
148  buf << " ( assert " << expression << " )";
149  }
150  if (!file.empty())
151  buf << "\n file: " << file << ":" << line;
152  #endif // NDEBUG
153  this->message = buf.str();
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 
165  string description;
166  string function;
167  string file;
168 #if defined NDEBUG && defined __clang__
169  SMIL_UNUSED
170 #endif // NDEBUG && __clang__
171  int line;
172  string expression;
173  string message;
174  };
175 
176  #define ASSERT_1_ARG(func, file, line, expr) \
177  if(!(expr)) { Error(func, file, line, #expr).show(); return RES_ERR; }
178  #define ASSERT_2_ARGS(func, file, line, expr, errCode) \
179  if(!(expr)) { Error(#errCode, func, file, line, #expr).show(); return errCode; }
180  #define ASSERT_3_ARGS(func, file, line, expr, errCode, retVal) \
181  if(!(expr)) { Error(#errCode, func, file, line, #expr).show(); return retVal; }
182 
183  #define ERR_MSG(msg) Error(msg, __FUNC__, __FILE__, __LINE__).show()
184 
185  #define ASSERT_NARGS_CHOOSER(...) \
186  GET_4TH_ARG(__VA_ARGS__, ASSERT_3_ARGS, ASSERT_2_ARGS, ASSERT_1_ARG, ...)
187 
188  #ifdef _MSC_VER
189  #define ASSERT(...) EXPAND( ASSERT_NARGS_CHOOSER(__VA_ARGS__)(__FUNC__, __FILE__, __LINE__, __VA_ARGS__) )
190  #else // _MSC_VER
191  #define ASSERT(...) ASSERT_NARGS_CHOOSER(__VA_ARGS__)(__FUNC__, __FILE__, __LINE__, __VA_ARGS__)
192  #endif // _MSC_VER
193 
196 } // namespace smil
197 
198 
199 #endif // _DERRORS_H
200 
Definition: DColorConvert.h:38
Definition: DErrors.h:73