RTI Connext Traditional C++ API  Version 7.0.0
ExceptionHelper.hpp
1 /*
2 (c) Copyright, Real-Time Innovations, 2018.
3 All rights reserved.
4 
5 No duplications, whole or partial, manual or electronic, may be made
6 without express written permission. Any such copies, or
7 revisions thereof, must display this notice unaltered.
8 This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10 
11 
12 #ifndef RTI_DDS_FLAT_EXCEPTIONHELPER_HPP_
13 #define RTI_DDS_FLAT_EXCEPTIONHELPER_HPP_
14 
15 //
16 // Enable debug assertions (RTI_FLAT_ASSERT) in debug mode
17 //
18 #if !defined(NDEBUG)
19 #define RTI_FLAT_DATA_ENABLE_DEBUG_ASSERTIONS
20 #endif
21 
22 //
23 // Error handling without C++ exceptions (traditional C++ and Micro C++ APIs)
24 //
25 #ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
26  // TODO: use MSG
27  #ifdef RTI_FLAT_DATA_ENABLE_DEBUG_ASSERTIONS
28  #define RTI_FLAT_ASSERT(COND, ACTION) \
29  if (!(COND)) { \
30  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "Assertion failure: " #COND); \
31  ACTION; \
32  }
33  #else
34  #define RTI_FLAT_ASSERT(COND, ACTION)
35  #endif
36 
37  // CR decission: Log error messages. Export function for each log kind (serialize, deserialize, precondition, skip)
38  // pass type/field (if possible) to serialization errors
39  #define RTI_FLAT_OFFSET_CHECK_NOT_NULL(ACTION) \
40  if (this->is_null()) { \
41  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "null offset"); \
42  ACTION; \
43  }
44 
45  #define RTI_FLAT_OFFSET_PRECONDITION_ERROR(MSG, ACTION) \
46  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, MSG); \
47  ACTION
48 
49  #define RTI_FLAT_CHECK_PRECONDITION(COND, ACTION) \
50  if (!(COND)) { \
51  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "Precondition failure: " #COND); \
52  ACTION; \
53  }
54 
55  #define RTI_FLAT_BUILDER_CHECK_NOT_BOUND(ACTION) \
56  if (bind_position_) { \
57  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "Builder is currently bound - call finish() first"); \
58  ACTION; \
59  }
60 
61  #define RTI_FLAT_BUILDER_CHECK_CAN_FINISH(ACTION) \
62  if (!is_nested()) { \
63  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "This is not a member builder; call finish_sample() instead"); \
64  ACTION; \
65  }
66 
67  #define RTI_FLAT_BUILDER_CHECK_CAN_FINISH_SAMPLE(ACTION) \
68  if (is_nested()) { \
69  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "This is a member builder; call finish() instead"); \
70  ACTION; \
71  }
72 
73  #define RTI_FLAT_BUILDER_CHECK_VALID(ACTION) \
74  if (!is_valid()) { \
75  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, "This Builder is not valid"); \
76  ACTION; \
77  }
78 
79  #define RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(BUILDER, ACTION) \
80  if (!BUILDER.is_valid()) { \
81  set_failure(); \
82  RTIXCdrFlatData_logCreationError(__FILE__, __LINE__, "Builder"); \
83  ACTION; \
84  }
85 
86  #define RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(ACTION) \
87  set_failure(); \
88  RTIXCdrFlatData_logBuilderOutOfResources(__FILE__, __LINE__); \
89  ACTION
90 
91  #define RTI_FLAT_BUILDER_PRECONDITION_ERROR(MSG, ACTION) \
92  set_failure(); \
93  RTIXCdrFlatData_logPreconditionError(__FILE__, __LINE__, MSG); \
94  ACTION
95 
96 //
97 // Error handling with C++ exceptions (modern C++ API)
98 //
99 #else // !RTI_FLAT_DATA_NO_EXCEPTIONS
100 
101  #ifdef RTI_FLAT_DATA_ENABLE_DEBUG_ASSERTIONS
102  #define RTI_FLAT_ASSERT(COND, ACTION) \
103  if (!(COND)) throw dds::core::PreconditionNotMetError("Assertion failure: " #COND)
104  #else
105  #define RTI_FLAT_ASSERT(COND, ACTION)
106  #endif
107 
108  #define RTI_FLAT_OFFSET_CHECK_NOT_NULL(ACTION) \
109  if (this->is_null()) throw dds::core::NullReferenceError("null offset")
110 
111  #define RTI_FLAT_OFFSET_PRECONDITION_ERROR(MSG, ACTION) \
112  throw dds::core::PreconditionNotMetError(MSG)
113 
114  #define RTI_FLAT_CHECK_PRECONDITION(COND, ACTION) \
115  if (!(COND)) throw dds::core::PreconditionNotMetError("Precondition failure: " #COND)
116 
117  #define RTI_FLAT_BUILDER_CHECK_NOT_BOUND(ACTION) \
118  if (bind_position_) \
119  throw dds::core::PreconditionNotMetError( \
120  "Builder is currently bound - call finish() first")
121 
122  #define RTI_FLAT_BUILDER_CHECK_CAN_FINISH(ACTION) \
123  if (!is_nested()) \
124  throw dds::core::PreconditionNotMetError( \
125  "This is not a member builder; call finish_sample() instead")
126 
127  #define RTI_FLAT_BUILDER_CHECK_CAN_FINISH_SAMPLE(ACTION) \
128  if (is_nested()) \
129  throw dds::core::PreconditionNotMetError( \
130  "This is a member builder; call finish() instead")
131 
132  #define RTI_FLAT_BUILDER_CHECK_VALID(ACTION) \
133  if (!is_valid()) \
134  throw dds::core::PreconditionNotMetError("This Builder is not valid")
135 
136  // With exceptions, nothing to check (exception would have been thrown)
137  #define RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(BUILDER, ACTION)
138 
139  #define RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(ACTION) \
140  throw dds::core::OutOfResourcesError("builder reached end of buffer")
141 
142  #define RTI_FLAT_BUILDER_PRECONDITION_ERROR(MSG, ACTION) \
143  throw dds::core::PreconditionNotMetError(MSG)
144 
145 
146 #endif // RTI_FLAT_DATA_NO_EXCEPTIONS
147 
148 //
149 // Error checking (other than assertions) in release mode is enabled unless
150 // the sources are compiled with -DRTI_FLAT_DATA_DISABLE_ERROR_CHECKING
151 //
152 // In that case, most error checks that deal with preconditions due to incorrect
153 // usage of the API are disabled in release mode. This would lead to undefined
154 // behavior in case of bad usage, but can improve performance.
155 //
156 #if defined(RTI_FLAT_DATA_ENABLE_DEBUG_ASSERTIONS) \
157  || !defined(RTI_FLAT_DATA_DISABLE_ERROR_CHECKING)
158 #define RTI_FLAT_DATA_ENABLE_ERROR_CHECKING
159 #endif
160 
161 #ifndef RTI_FLAT_DATA_ENABLE_ERROR_CHECKING
162 
163  #define RTI_FLAT_OFFSET_CHECK_NOT_NULL(ACTION)
164  #define RTI_FLAT_ASSERT(COND, ACTION)
165  #define RTI_FLAT_CHECK_PRECONDITION(COND, ACTION)
166  #define RTI_FLAT_BUILDER_CHECK_NOT_BOUND(ACTION)
167  #define RTI_FLAT_BUILDER_CHECK_CAN_FINISH(ACTION)
168  #define RTI_FLAT_BUILDER_CHECK_CAN_FINISH_SAMPLE(ACTION)
169  #define RTI_FLAT_BUILDER_CHECK_VALID(ACTION)
170  #define RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(BUILDER, ACTION)
171 
172 #endif // RTI_FLAT_DATA_ENABLE_ERROR_CHECKING
173 
174 namespace rti { namespace flat { namespace detail {
175 
176 } } }
177 
178 #endif // RTI_DDS_FLAT_EXCEPTIONHELPER_HPP_
179 
Definition: AggregationBuilders.hpp:17