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