rcutils  master
C API providing common utilities and data structures.
macros.h
Go to the documentation of this file.
1 // Copyright 2017 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 
16 
17 #ifndef RCUTILS__MACROS_H_
18 #define RCUTILS__MACROS_H_
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 #ifndef _WIN32
26 #define RCUTILS_WARN_UNUSED __attribute__((warn_unused_result))
28 #else
29 #define RCUTILS_WARN_UNUSED _Check_return_
31 #endif
32 
34 // This block either sets RCUTILS_THREAD_LOCAL or RCUTILS_THREAD_LOCAL_PTHREAD.
35 #if defined _WIN32 || defined __CYGWIN__
36 // Windows or Cygwin
37  #define RCUTILS_THREAD_LOCAL __declspec(thread)
38 #elif defined __APPLE__
39 // Apple OS's
40  #include <TargetConditionals.h>
41  #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
42 // iOS Simulator or iOS device
43  #include <Availability.h>
44  #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
45  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
46 // iOS >= 10, thread local storage was added in iOS 10
47  #define RCUTILS_THREAD_LOCAL _Thread_local
48  #else
49 // iOS < 10, no thread local storage, so use pthread instead
50  #define RCUTILS_THREAD_LOCAL_PTHREAD 1
51  #undef RCUTILS_THREAD_LOCAL
52  #endif
53  #else
54  #error "Unknown iOS version"
55  #endif
56  #elif TARGET_OS_MAC
57 // macOS
58  #define RCUTILS_THREAD_LOCAL _Thread_local
59  #else
60  #error "Unknown Apple platform"
61  #endif
62 #else
63 // Some other non-Windows, non-cygwin, non-apple OS
64  #define RCUTILS_THREAD_LOCAL _Thread_local
65 #endif
66 
67 #define RCUTILS_STRINGIFY_IMPL(x) #x
68 #define RCUTILS_STRINGIFY(x) RCUTILS_STRINGIFY_IMPL(x)
69 
71 #define RCUTILS_UNUSED(x) (void)(x)
72 
73 #define RCUTILS_JOIN_IMPL(arg1, arg2) arg1 ## arg2
74 #define RCUTILS_JOIN(arg1, arg2) RCUTILS_JOIN_IMPL(arg1, arg2)
75 
77 #if defined _WIN32 || defined __CYGWIN__
78 #define RCUTILS_ATTRIBUTE_PRINTF_FORMAT(format_string_index, first_to_check_index)
80 #else
81 
102 #define RCUTILS_ATTRIBUTE_PRINTF_FORMAT(format_string_index, first_to_check_index) \
103  __attribute__ ((format(printf, format_string_index, first_to_check_index)))
104 #endif // !defined _WIN32 || defined __CYGWIN__
105 
107 #ifndef _WIN32
108 # define RCUTILS_DEPRECATED __attribute__((deprecated))
109 #else
110 # define RCUTILS_DEPRECATED __declspec(deprecated)
111 #endif
112 
114 #ifndef _WIN32
115 # define RCUTILS_DEPRECATED_WITH_MSG(msg) __attribute__((deprecated(msg)))
116 #else
117 # define RCUTILS_DEPRECATED_WITH_MSG(msg) __declspec(deprecated(msg))
118 #endif
119 
120 // Provide the compiler with branch prediction information
121 #ifndef _WIN32
122 
126 # define RCUTILS_LIKELY(x) __builtin_expect((x), 1)
127 
131 # define RCUTILS_UNLIKELY(x) __builtin_expect((x), 0)
132 #else
133 
137 # define RCUTILS_LIKELY(x) (x)
138 
142 # define RCUTILS_UNLIKELY(x) (x)
143 #endif // _WIN32
144 
145 #if defined RCUTILS_ENABLE_FAULT_INJECTION
147 
189 # define RCUTILS_CAN_RETURN_WITH_ERROR_OF(error_return_value) \
190  RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR(error_return_value);
191 
206 # define RCUTILS_CAN_FAIL_WITH(failure_code) \
207  RCUTILS_FAULT_INJECTION_MAYBE_FAIL(failure_code);
208 
209 #else
210 # define RCUTILS_CAN_RETURN_WITH_ERROR_OF(error_return_value)
211 # define RCUTILS_CAN_FAIL_WITH(failure_code)
212 #endif // defined RCUTILS_ENABLE_FAULT_INJECTION
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif // RCUTILS__MACROS_H_
fault_injection.h