rmw  beta1
C API providing a middleware abstraction layer which is used to implement the rest of ROS.
macros.hpp
Go to the documentation of this file.
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RMW__IMPL__CPP__MACROS_HPP_
16 #define RMW__IMPL__CPP__MACROS_HPP_
17 
18 #include <iostream>
19 #include <sstream>
20 #include <string>
21 
22 #include "rmw/error_handling.h"
23 #include "rmw/impl/config.h" // For RMW_AVOID_MEMORY_ALLOCATION
24 #include "rmw/impl/cpp/demangle.hpp" // For demangle.
25 
26 // *INDENT-OFF* (prevent uncrustify from using four space indention here)
27 #define RMW_TRY_PLACEMENT_NEW(Destination, BufferForNew, FailureAction, Type, ...) try { \
28  Destination = new(BufferForNew) Type(__VA_ARGS__); \
29 } catch(const std::exception & exception) { \
30  RMW_SET_ERROR_MSG(( \
31  std::string("caught C++ exception ") + rmw::impl::cpp::demangle(exception) + \
32  " constructing " #Type ": " + exception.what() \
33  ).c_str()); \
34  FailureAction; \
35 } catch(...) { \
36  RMW_SET_ERROR_MSG("caught unknown C++ exception constructing " #Type); \
37  FailureAction; \
38 }
39 
40 #define RMW_TRY_DESTRUCTOR(Statement, Type, FailureAction) try { \
41  Statement; \
42 } catch(const std::exception & exception) { \
43  RMW_SET_ERROR_MSG(( \
44  std::string("caught C++ exception in destructor of " #Type ": ") + \
45  rmw::impl::cpp::demangle(exception) + ": " + exception.what() \
46  ).c_str()); \
47  FailureAction; \
48 } catch(...) { \
49  RMW_SET_ERROR_MSG("caught unknown C++ exception in destructor of " #Type); \
50  FailureAction; \
51 }
52 
53 #define RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(Statement, Type) try { \
54  Statement; \
55 } catch(const std::exception & exception) { \
56  std::stringstream ss; \
57  ss << "caught C++ exception in destructor of " #Type " while handling a failure: " \
58  << rmw::impl::cpp::demangle(exception) << ": " << exception.what() \
59  << ", at: " << __FILE__ << ":" << __LINE__ << '\n'; \
60  (std::cerr << ss.str()).flush(); \
61 } catch(...) { \
62  std::stringstream ss; \
63  ss << "caught unknown C++ exception in destructor of " #Type \
64  << " while handling a failure at: " << __FILE__ << ":" << __LINE__ << '\n'; \
65  (std::cerr << ss.str()).flush(); \
66 }
67 
68 #if RMW_AVOID_MEMORY_ALLOCATION
69 #define RMW_CHECK_TYPE_IDENTIFIERS_MATCH(ElementName, ElementTypeID, ExpectedTypeID, OnFailure) { \
70  if (ElementTypeID != ExpectedTypeID) { \
71  char __msg[1024]; \
72  snprintf( \
73  __msg, 1024, \
74  #ElementName " implementation '%s'(%p) does not match rmw implementation '%s'(%p)", \
75  ElementTypeID, ElementTypeID, ExpectedTypeID, ExpectedTypeID); \
76  RMW_SET_ERROR_MSG(__msg); \
77  OnFailure; \
78  } \
79 }
80 #else // RMW_AVOID_MEMORY_ALLOCATION
81 #define RMW_CHECK_TYPE_IDENTIFIERS_MATCH(ElementName, ElementTypeID, ExpectedTypeID, OnFailure) { \
82  if (ElementTypeID != ExpectedTypeID) { \
83  size_t __bytes_that_would_have_been_written = snprintf( \
84  NULL, 0, \
85  #ElementName " implementation '%s'(%p) does not match rmw implementation '%s'(%p)", \
86  ElementTypeID, ElementTypeID, ExpectedTypeID, ExpectedTypeID); \
87  char * __msg = \
88  reinterpret_cast<char *>(rmw_allocate(__bytes_that_would_have_been_written + 1)); \
89  snprintf( \
90  __msg, __bytes_that_would_have_been_written + 1, \
91  #ElementName " implementation '%s'(%p) does not match rmw implementation '%s'(%p)", \
92  ElementTypeID, ElementTypeID, ExpectedTypeID, ExpectedTypeID); \
93  RMW_SET_ERROR_MSG(__msg); \
94  rmw_free(__msg); \
95  OnFailure; \
96  } \
97 }
98 #endif // RMW_AVOID_MEMORY_ALLOCATION
99 // *INDENT-ON*
100 
101 #endif // RMW__IMPL__CPP__MACROS_HPP_