Point Cloud Library (PCL)  1.11.0-dev
pcl_macros.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #pragma once
38 
39 /**
40  * \file pcl/pcl_macros.h
41  *
42  * \brief Defines all the PCL and non-PCL macros used
43  * \ingroup common
44  */
45 
46 #if defined __INTEL_COMPILER
47  #pragma warning disable 2196 2536 279
48 #endif
49 
50 #if defined _MSC_VER
51  // 4244 : conversion from 'type1' to 'type2', possible loss of data
52  // 4661 : no suitable definition provided for explicit template instantiation reques
53  // 4503 : decorated name length exceeded, name was truncated
54  // 4146 : unary minus operator applied to unsigned type, result still unsigned
55  #pragma warning (disable: 4018 4244 4267 4521 4251 4661 4305 4503 4146)
56 #endif
57 
58 #ifndef _USE_MATH_DEFINES
59  #define _USE_MATH_DEFINES
60 #endif
61 #include <cmath>
62 #include <cstdarg>
63 #include <cstdio>
64 #include <cstdlib>
65 #include <cstdint>
66 #include <iostream>
67 
68 // We need to check for GCC version, because GCC releases before 9 were implementing an
69 // OpenMP 3.1 data sharing rule, even OpenMP 4 is supported, so a plain OpenMP version 4 check
70 // isn't enough (see https://www.gnu.org/software/gcc/gcc-9/porting_to.html#ompdatasharing)
71 #if (defined _OPENMP && (_OPENMP <= 201307)) || (defined __GNUC__ && (__GNUC__ >= 6 && __GNUC__ < 9))
72  #define OPENMP_LEGACY_CONST_DATA_SHARING_RULE 1
73 #else
74  #define OPENMP_LEGACY_CONST_DATA_SHARING_RULE 0
75 #endif
76 
77 #include <pcl/pcl_config.h>
78 
79 #include <boost/preprocessor/comparison/equal.hpp>
80 #include <boost/preprocessor/comparison/less.hpp>
81 #include <boost/preprocessor/control/if.hpp>
82 #include <boost/preprocessor/stringize.hpp>
83 
84 // MSVC < 2019 have issues:
85 // * can't short-circuiting logic in macros
86 // * don't define standard macros
87 // => this leads to annyoing C4067 warnings (see https://developercommunity.visualstudio.com/content/problem/327796/-has-cpp-attribute-emits-warning-is-wrong-highligh.html)
88 #if defined(_MSC_VER)
89  // nvcc on msvc can't work with [[deprecated]]
90  #if !defined(__CUDACC__)
91  #define _PCL_DEPRECATED_IMPL(Message) [[deprecated(Message)]]
92  #else
93  #define _PCL_DEPRECATED_IMPL(Message)
94  #endif
95 #elif __has_cpp_attribute(deprecated)
96  #define _PCL_DEPRECATED_IMPL(Message) [[deprecated(Message)]]
97 #else
98  #warning "You need to implement _PCL_DEPRECATED_IMPL for this compiler"
99  #define _PCL_DEPRECATED_IMPL(Message)
100 #endif
101 
102 // Macro for pragma operator
103 #if (defined (__GNUC__) || defined(__clang__))
104  #define PCL_PRAGMA(x) _Pragma (#x)
105 #elif _MSC_VER
106  #define PCL_PRAGMA(x) __pragma (#x)
107 #else
108  #define PCL_PRAGMA
109 #endif
110 
111 // Macro for emitting pragma warning for deprecated headers
112 #if (defined (__GNUC__) || defined(__clang__))
113  #define _PCL_DEPRECATED_HEADER_IMPL(Message) PCL_PRAGMA (GCC warning Message)
114 #elif _MSC_VER
115  #define _PCL_DEPRECATED_HEADER_IMPL(Message) PCL_PRAGMA (warning (Message))
116 #else
117  #warning "You need to implement _PCL_DEPRECATED_HEADER_IMPL for this compiler"
118  #define _PCL_DEPRECATED_HEADER_IMPL(Message)
119 #endif
120 
121 /**
122  * \brief A handy way to inform the user of the removal deadline
123  */
124 #define _PCL_PREPARE_REMOVAL_MESSAGE(Major, Minor, Msg) \
125  Msg " (It will be removed in PCL " BOOST_PP_STRINGIZE(Major.Minor) ")"
126 
127 /**
128  * \brief Tests for Minor < PCL_MINOR_VERSION
129  */
130 #define _PCL_COMPAT_MINOR_VERSION(Minor, IfPass, IfFail) \
131  BOOST_PP_IF(BOOST_PP_LESS(PCL_MINOR_VERSION, Minor), IfPass, IfFail)
132 
133 /**
134  * \brief Tests for Major == PCL_MAJOR_VERSION
135  */
136 #define _PCL_COMPAT_MAJOR_VERSION(Major, IfPass, IfFail) \
137  BOOST_PP_IF(BOOST_PP_EQUAL(PCL_MAJOR_VERSION, Major), IfPass, IfFail)
138 
139 /**
140  * \brief macro for compatibility across compilers and help remove old deprecated
141  * items for the Major.Minor release
142  *
143  * \details compiler errors of `unneeded_deprecation` and `major_version_mismatch`
144  * are hints to the developer that those items can be safely removed.
145  * Behavior of PCL_DEPRECATED(1, 99, "Not needed anymore")
146  * * till PCL 1.98: "Not needed anymore (It will be removed in PCL 1.99)"
147  * * PCL 1.99 onwards: compiler error with "unneeded_deprecation"
148  * * PCL 2.0 onwards: compiler error with "major_version_mismatch"
149  */
150 #define PCL_DEPRECATED(Major, Minor, Message) \
151  _PCL_COMPAT_MAJOR_VERSION( \
152  Major, \
153  _PCL_COMPAT_MINOR_VERSION( \
154  Minor, \
155  _PCL_DEPRECATED_IMPL(_PCL_PREPARE_REMOVAL_MESSAGE(Major, Minor, Message)), \
156  unneeded_deprecation), \
157  major_version_mismatch)
158 
159 /**
160  * \brief macro for compatibility across compilers and help remove old deprecated
161  * headers for the Major.Minor release
162  *
163  * \details compiler errors of `unneeded_header` and `major_version_mismatch`
164  * are hints to the developer that those items can be safely removed.
165  * Behavior of PCL_DEPRECATED_HEADER(1, 99, "Use file <newfile.h> instead.")
166  * * till PCL 1.98: "This header is deprecated. Use file <newfile.h> instead. (It will be removed in PCL 1.99)"
167  * * PCL 1.99 onwards: compiler error with "unneeded_header"
168  * * PCL 2.0 onwards: compiler error with "major_version_mismatch"
169  */
170 #define PCL_DEPRECATED_HEADER(Major, Minor, Message) \
171  _PCL_COMPAT_MAJOR_VERSION( \
172  Major, \
173  _PCL_COMPAT_MINOR_VERSION( \
174  Minor, \
175  _PCL_DEPRECATED_HEADER_IMPL(_PCL_PREPARE_REMOVAL_MESSAGE( \
176  Major, \
177  Minor, \
178  "This header is deprecated. " Message)), \
179  unneeded_header), \
180  major_version_mismatch)
181 
182 #if defined _WIN32
183 // Define math constants, without including math.h, to prevent polluting global namespace with old math methods
184 // Copied from math.h
185 // Check for M_2_SQRTPI since the cmath header on mingw-w64 doesn't seem to define
186 // _MATH_DEFINES_DEFINED when included with _USE_MATH_DEFINES
187 #if !defined _MATH_DEFINES_DEFINED && !defined M_2_SQRTPI
188  #define _MATH_DEFINES_DEFINED
189 
190  #define M_E 2.71828182845904523536 // e
191  #define M_LOG2E 1.44269504088896340736 // log2(e)
192  #define M_LOG10E 0.434294481903251827651 // log10(e)
193  #define M_LN2 0.693147180559945309417 // ln(2)
194  #define M_LN10 2.30258509299404568402 // ln(10)
195  #define M_PI 3.14159265358979323846 // pi
196  #define M_PI_2 1.57079632679489661923 // pi/2
197  #define M_PI_4 0.785398163397448309616 // pi/4
198  #define M_1_PI 0.318309886183790671538 // 1/pi
199  #define M_2_PI 0.636619772367581343076 // 2/pi
200  #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
201  #define M_SQRT2 1.41421356237309504880 // sqrt(2)
202  #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)
203 #endif
204 
205 #if defined _MSC_VER
206  // Stupid. This should be removed when all the PCL dependencies have min/max fixed.
207  #ifndef NOMINMAX
208  #define NOMINMAX
209  #endif
210 
211  #define __PRETTY_FUNCTION__ __FUNCTION__
212  #define __func__ __FUNCTION__
213 #endif
214 #endif // defined _WIN32
215 
216 
217 template<typename T>
218 PCL_DEPRECATED(1, 12, "use std::isnan instead of pcl_isnan")
219 bool pcl_isnan (T&& x) { return std::isnan (std::forward<T> (x)); }
220 
221 template<typename T>
222 PCL_DEPRECATED(1, 12, "use std::isfinite instead of pcl_isfinite")
223 bool pcl_isfinite (T&& x) { return std::isfinite (std::forward<T> (x)); }
224 
225 template<typename T>
226 PCL_DEPRECATED(1, 12, "use std::isinf instead of pcl_isinf")
227 bool pcl_isinf (T&& x) { return std::isinf (std::forward<T> (x)); }
228 
229 
230 #ifndef DEG2RAD
231 #define DEG2RAD(x) ((x)*0.017453293)
232 #endif
233 
234 #ifndef RAD2DEG
235 #define RAD2DEG(x) ((x)*57.29578)
236 #endif
237 
238 /** \brief Macro that maps version information given by major.minor.patch to a linear integer value to enable easy comparison
239  */
240 #define PCL_LINEAR_VERSION(major,minor,patch) ((major)<<16|(minor)<<8|(patch))
241 
242 /** Win32 doesn't seem to have rounding functions.
243  * Therefore implement our own versions of these functions here.
244  */
245 
246 __inline double
247 pcl_round (double number)
248 {
249  return (number < 0.0 ? std::ceil (number - 0.5) : std::floor (number + 0.5));
250 }
251 __inline float
252 pcl_round (float number)
253 {
254  return (number < 0.0f ? std::ceil (number - 0.5f) : std::floor (number + 0.5f));
255 }
256 
257 #ifdef __GNUC__
258 #define pcl_lrint(x) (lrint(static_cast<double> (x)))
259 #define pcl_lrintf(x) (lrintf(static_cast<float> (x)))
260 #else
261 #define pcl_lrint(x) (static_cast<long int>(pcl_round(x)))
262 #define pcl_lrintf(x) (static_cast<long int>(pcl_round(x)))
263 #endif
264 
265 #ifdef WIN32
266 #define pcl_sleep(x) Sleep(1000*(x))
267 #else
268 #define pcl_sleep(x) sleep(x)
269 #endif
270 
271 #ifndef PVAR
272  #define PVAR(s) \
273  #s << " = " << (s) << std::flush
274 #endif
275 #ifndef PVARN
276 #define PVARN(s) \
277  #s << " = " << (s) << "\n"
278 #endif
279 #ifndef PVARC
280 #define PVARC(s) \
281  #s << " = " << (s) << ", " << std::flush
282 #endif
283 #ifndef PVARS
284 #define PVARS(s) \
285  #s << " = " << (s) << " " << std::flush
286 #endif
287 #ifndef PVARA
288 #define PVARA(s) \
289  #s << " = " << RAD2DEG(s) << "deg" << std::flush
290 #endif
291 #ifndef PVARAN
292 #define PVARAN(s) \
293  #s << " = " << RAD2DEG(s) << "deg\n"
294 #endif
295 #ifndef PVARAC
296 #define PVARAC(s) \
297  #s << " = " << RAD2DEG(s) << "deg, " << std::flush
298 #endif
299 #ifndef PVARAS
300 #define PVARAS(s) \
301  #s << " = " << RAD2DEG(s) << "deg " << std::flush
302 #endif
303 
304 #define FIXED(s) \
305  std::fixed << s << std::resetiosflags(std::ios_base::fixed)
306 
307 #ifndef ERASE_STRUCT
308 #define ERASE_STRUCT(var) memset(&var, 0, sizeof(var))
309 #endif
310 
311 #ifndef ERASE_ARRAY
312 #define ERASE_ARRAY(var, size) memset(var, 0, size*sizeof(*var))
313 #endif
314 
315 #ifndef SET_ARRAY
316 #define SET_ARRAY(var, value, size) { for (decltype(size) i = 0; i < size; ++i) var[i]=value; }
317 #endif
318 
319 #ifndef PCL_EXTERN_C
320  #ifdef __cplusplus
321  #define PCL_EXTERN_C extern "C"
322  #else
323  #define PCL_EXTERN_C
324  #endif
325 #endif
326 
327 #if defined WIN32 || defined _WIN32 || defined WINCE || defined __MINGW32__
328  #ifdef PCLAPI_EXPORTS
329  #define PCL_EXPORTS __declspec(dllexport)
330  #else
331  #define PCL_EXPORTS
332  #endif
333 #else
334  #define PCL_EXPORTS
335 #endif
336 
337 #if defined WIN32 || defined _WIN32
338  #define PCL_CDECL __cdecl
339  #define PCL_STDCALL __stdcall
340 #else
341  #define PCL_CDECL
342  #define PCL_STDCALL
343 #endif
344 
345 #ifndef PCLAPI
346  #define PCLAPI(rettype) PCL_EXTERN_C PCL_EXPORTS rettype PCL_CDECL
347 #endif
348 
349 //for clang cf. http://clang.llvm.org/docs/LanguageExtensions.html
350 #ifndef __has_extension
351  #define __has_extension(x) 0 // Compatibility with pre-3.0 compilers.
352 #endif
353 
354 #if defined (__GNUC__) || defined (__PGI) || defined (__IBMCPP__) || defined (__SUNPRO_CC)
355  #define PCL_ALIGN(alignment) __attribute__((aligned(alignment)))
356 #elif defined (_MSC_VER)
357  #define PCL_ALIGN(alignment) __declspec(align(alignment))
358 #else
359  #error Alignment not supported on your platform
360 #endif
361 
362 #if defined(__GLIBC__) && PCL_LINEAR_VERSION(__GLIBC__,__GLIBC_MINOR__,0)>PCL_LINEAR_VERSION(2,8,0)
363  #define GLIBC_MALLOC_ALIGNED 1
364 #else
365  #define GLIBC_MALLOC_ALIGNED 0
366 #endif
367 
368 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
369  #define FREEBSD_MALLOC_ALIGNED 1
370 #else
371  #define FREEBSD_MALLOC_ALIGNED 0
372 #endif
373 
374 #if defined(__APPLE__) || defined(_WIN64) || GLIBC_MALLOC_ALIGNED || FREEBSD_MALLOC_ALIGNED
375  #define MALLOC_ALIGNED 1
376 #endif
377 
378 #if defined (HAVE_MM_MALLOC)
379  // Intel compiler defines an incompatible _mm_malloc signature
380  #if defined(__INTEL_COMPILER)
381  #include <malloc.h>
382  #else
383  #include <mm_malloc.h>
384  #endif
385 #endif
386 
387 inline void*
388 aligned_malloc (std::size_t size)
389 {
390  void *ptr;
391 #if defined (MALLOC_ALIGNED)
392  ptr = std::malloc (size);
393 #elif defined (HAVE_POSIX_MEMALIGN)
394  if (posix_memalign (&ptr, 16, size))
395  ptr = 0;
396 #elif defined (HAVE_MM_MALLOC)
397  ptr = _mm_malloc (size, 16);
398 #elif defined (_MSC_VER)
399  ptr = _aligned_malloc (size, 16);
400 #elif defined (ANDROID)
401  ptr = memalign (16, size);
402 #else
403  #error aligned_malloc not supported on your platform
404  ptr = 0;
405 #endif
406  return (ptr);
407 }
408 
409 inline void
410 aligned_free (void* ptr)
411 {
412 #if defined (MALLOC_ALIGNED) || defined (HAVE_POSIX_MEMALIGN)
413  std::free (ptr);
414 #elif defined (HAVE_MM_MALLOC)
415  _mm_free (ptr);
416 #elif defined (_MSC_VER)
417  _aligned_free (ptr);
418 #elif defined (ANDROID)
419  free (ptr);
420 #else
421  #error aligned_free not supported on your platform
422 #endif
423 }
424 
425 /**
426  * \brief Macro to add a no-op or a fallthrough attribute based on compiler feature
427  *
428  * \ingroup common
429  */
430 #if (__cplusplus >= 201703L) || (defined(_MSC_VER) && (_MSC_VER >= 1910) && (_MSVC_LANG >= 201703L))
431  #define PCL_FALLTHROUGH [[fallthrough]];
432 #elif defined(__clang__)
433  #define PCL_FALLTHROUGH [[clang::fallthrough]];
434 #elif defined(__GNUC__) && (__GNUC__ >= 7)
435  #define PCL_FALLTHROUGH [[gnu::fallthrough]];
436 #else
437  #define PCL_FALLTHROUGH
438 #endif
439 
440 #if (__cplusplus >= 201703L) || (defined(_MSC_VER) && (_MSC_VER >= 1911) && (_MSVC_LANG >= 201703L))
441  #define PCL_NODISCARD [[nodiscard]]
442 #elif defined(__clang__) && (PCL_LINEAR_VERSION(__clang_major__, __clang_minor__, 0) >= PCL_LINEAR_VERSION(3, 9, 0))
443  #define PCL_NODISCARD [[clang::warn_unused_result]]
444 #elif defined(__GNUC__)
445  #define PCL_NODISCARD [[gnu::warn_unused_result]]
446 #else
447  #define PCL_NODISCARD
448 #endif
pcl_isnan
bool pcl_isnan(T &&x)
Definition: pcl_macros.h:219
aligned_free
void aligned_free(void *ptr)
Definition: pcl_macros.h:410
PCL_DEPRECATED
#define PCL_DEPRECATED(Major, Minor, Message)
macro for compatibility across compilers and help remove old deprecated items for the Major....
Definition: pcl_macros.h:150
pcl_isfinite
bool pcl_isfinite(T &&x)
Definition: pcl_macros.h:223
aligned_malloc
void * aligned_malloc(std::size_t size)
Definition: pcl_macros.h:388
pcl_isinf
bool pcl_isinf(T &&x)
Definition: pcl_macros.h:227
pcl_round
__inline double pcl_round(double number)
Win32 doesn't seem to have rounding functions.
Definition: pcl_macros.h:247