HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IexMacros.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IEXMACROS_H
38 #define INCLUDED_IEXMACROS_H
39 
40 //--------------------------------------------------------------------
41 //
42 // Macros which make throwing exceptions more convenient
43 //
44 //--------------------------------------------------------------------
45 
46 #include <sstream>
47 
48 
49 //----------------------------------------------------------------------------
50 // A macro to throw exceptions whose text is assembled using stringstreams.
51 //
52 // Example:
53 //
54 // THROW (InputExc, "Syntax error in line " << line ", " << file << ".");
55 //
56 //----------------------------------------------------------------------------
57 
58 #include "IexExport.h"
59 #include "IexForward.h"
60 
62 
63 #define THROW(type, text) \
64  do \
65  { \
66  iex_debugTrap(); \
67  std::stringstream s; \
68  s << text; \
69  throw type (s); \
70  } \
71  while (0)
72 
73 
74 //----------------------------------------------------------------------------
75 // Macros to add to or to replace the text of an exception.
76 // The new text is assembled using stringstreams.
77 //
78 // Examples:
79 //
80 // Append to end of an exception's text:
81 //
82 // catch (BaseExc &e)
83 // {
84 // APPEND_EXC (e, " Directory " << name << " does not exist.");
85 // throw;
86 // }
87 //
88 // Replace an exception's text:
89 //
90 // catch (BaseExc &e)
91 // {
92 // REPLACE_EXC (e, "Directory " << name << " does not exist. " << e);
93 // throw;
94 // }
95 //----------------------------------------------------------------------------
96 
97 #define APPEND_EXC(exc, text) \
98  do \
99  { \
100  std::stringstream s; \
101  s << text; \
102  exc.append (s); \
103  } \
104  while (0)
105 
106 #define REPLACE_EXC(exc, text) \
107  do \
108  { \
109  std::stringstream s; \
110  s << text; \
111  exc.assign (s); \
112  } \
113  while (0)
114 
115 
116 //-------------------------------------------------------------
117 // A macro to throw ErrnoExc exceptions whose text is assembled
118 // using stringstreams:
119 //
120 // Example:
121 //
122 // THROW_ERRNO ("Cannot open file " << name << " (%T).");
123 //
124 //-------------------------------------------------------------
125 
126 #define THROW_ERRNO(text) \
127  do \
128  { \
129  std::stringstream s; \
130  s << text; \
131  ::IEX_NAMESPACE::throwErrnoExc (s.str()); \
132  } \
133  while (0)
134 
135 
136 //-------------------------------------------------------------
137 // A macro to throw exceptions if an assertion is false.
138 //
139 // Example:
140 //
141 // ASSERT (ptr != 0, NullExc, "Null pointer" );
142 //
143 //-------------------------------------------------------------
144 
145 #define ASSERT(assertion, type, text) \
146  do \
147  { \
148  if( (assertion) == false ) \
149  { \
150  THROW( type, text ); \
151  } \
152  } \
153  while (0)
154 
155 //-------------------------------------------------------------
156 // A macro to throw an IEX_NAMESPACE::LogicExc if an assertion is false,
157 // with the text composed from the source code file, line number,
158 // and assertion argument text.
159 //
160 // Example:
161 //
162 // LOGIC_ASSERT (i < n);
163 //
164 //-------------------------------------------------------------
165 #define LOGIC_ASSERT(assertion) \
166  ASSERT(assertion, \
167  IEX_NAMESPACE::LogicExc, \
168  __FILE__ << "(" << __LINE__ << "): logical assertion failed: " << #assertion )
169 
170 #endif
#define IEX_EXPORT
Definition: IexExport.h:60
IEX_EXPORT void iex_debugTrap()