nec2++  1.7.0
nec_exception.h
1 /***************************************************************************
2  * Copyright (C) 2004-2008 by Tim Molteno *
3  * tim@physics.otago.ac.nz *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 #ifndef __nec_exception__
21 #define __nec_exception__
22 
23 #include <string>
24 #include <sstream>
25 
26 //#include "common.h"
27 
29 {
30 public:
32  {
33  }
34 
35  nec_exception(const char* message)
36  {
37  m_message << message;
38  }
39 
40  nec_exception(const char* message, int code)
41  {
42  m_message << message << code;
43  }
44 
45  template <class T> void append(const T& message)
46  {
47  m_message << message;
48  }
49 
50  std::string get_message()
51  {
52  std::string ret = m_message.str();
53  return ret;
54  }
55 
56  static std::string string_printf(const char* fmt, ...);
57 
58 protected:
59  std::stringstream m_message;
60 };
61 
62 #ifdef _MSC_VER
63 /*
64  Visual C++ does not allow macros with variable argument lists. Therefore error messages
65  will be meaningless when this is compiled using VC++, however at least it will compile!
66 */
67 inline void nec_stop(const char* __fmt, ...)
68 {
69  nec_exception* __nex = new nec_exception("Undefined Error");
70  // __nex->os_printf(__fmt, __VA_ARGS__);
71  throw __nex;
72 }
73 #else
74 #define nec_stop(__fmt, ...)\
75 { nec_exception* __nex = new nec_exception();\
76  std::string _mess = nec_exception::string_printf(__fmt, __VA_ARGS__); \
77  __nex->append(_mess.c_str()); \
78  throw __nex; \
79 }
80 #endif
81 
82 
83 #endif /* __nec_exception__ */
Definition: nec_exception.h:28