HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IexMacros.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 
7 #ifndef INCLUDED_IEXMACROS_H
8 #define INCLUDED_IEXMACROS_H
9 
10 //--------------------------------------------------------------------
11 //
12 // Macros which make throwing exceptions more convenient
13 //
14 //--------------------------------------------------------------------
15 
16 #include <sstream>
17 
18 
19 //----------------------------------------------------------------------------
20 // A macro to throw exceptions whose text is assembled using stringstreams.
21 //
22 // Example:
23 //
24 // THROW (InputExc, "Syntax error in line " << line ", " << file << ".");
25 //
26 //----------------------------------------------------------------------------
27 
28 #include "IexExport.h"
29 #include "IexForward.h"
30 
32 
33 #define THROW(type, text) \
34  do \
35  { \
36  iex_debugTrap(); \
37  std::stringstream _iex_throw_s; \
38  _iex_throw_s << text; \
39  throw type (_iex_throw_s); \
40  } \
41  while (0)
42 
43 
44 //----------------------------------------------------------------------------
45 // Macros to add to or to replace the text of an exception.
46 // The new text is assembled using stringstreams.
47 //
48 // Examples:
49 //
50 // Append to end of an exception's text:
51 //
52 // catch (BaseExc &e)
53 // {
54 // APPEND_EXC (e, " Directory " << name << " does not exist.");
55 // throw;
56 // }
57 //
58 // Replace an exception's text:
59 //
60 // catch (BaseExc &e)
61 // {
62 // REPLACE_EXC (e, "Directory " << name << " does not exist. " << e);
63 // throw;
64 // }
65 //----------------------------------------------------------------------------
66 
67 #define APPEND_EXC(exc, text) \
68  do \
69  { \
70  std::stringstream _iex_append_s; \
71  _iex_append_s << text; \
72  exc.append (_iex_append_s); \
73  } \
74  while (0)
75 
76 #define REPLACE_EXC(exc, text) \
77  do \
78  { \
79  std::stringstream _iex_replace_s; \
80  _iex_replace_s << text; \
81  exc.assign (_iex_replace_s); \
82  } \
83  while (0)
84 
85 
86 //-------------------------------------------------------------
87 // A macro to throw ErrnoExc exceptions whose text is assembled
88 // using stringstreams:
89 //
90 // Example:
91 //
92 // THROW_ERRNO ("Cannot open file " << name << " (%T).");
93 //
94 //-------------------------------------------------------------
95 
96 #define THROW_ERRNO(text) \
97  do \
98  { \
99  std::stringstream _iex_throw_errno_s; \
100  _iex_throw_errno_s << text; \
101  ::IEX_NAMESPACE::throwErrnoExc (_iex_throw_errno_s.str()); \
102  } \
103  while (0)
104 
105 
106 //-------------------------------------------------------------
107 // A macro to throw exceptions if an assertion is false.
108 //
109 // Example:
110 //
111 // ASSERT (ptr != 0, NullExc, "Null pointer" );
112 //
113 //-------------------------------------------------------------
114 
115 #define ASSERT(assertion, type, text) \
116  do \
117  { \
118  if( bool(assertion) == false ) \
119  { \
120  THROW( type, text ); \
121  } \
122  } \
123  while (0)
124 
125 //-------------------------------------------------------------
126 // A macro to throw an IEX_NAMESPACE::LogicExc if an assertion is false,
127 // with the text composed from the source code file, line number,
128 // and assertion argument text.
129 //
130 // Example:
131 //
132 // LOGIC_ASSERT (i < n);
133 //
134 //-------------------------------------------------------------
135 #define LOGIC_ASSERT(assertion) \
136  ASSERT(assertion, \
137  IEX_NAMESPACE::LogicExc, \
138  __FILE__ << "(" << __LINE__ << "): logical assertion failed: " << #assertion )
139 
140 #endif
#define IEX_EXPORT
Definition: IexExport.h:30
IEX_EXPORT void iex_debugTrap()