Point Cloud Library (PCL)  1.11.1-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 <cstdlib>
63 #include <iostream>
64 
65 // We need to check for GCC version, because GCC releases before 9 were implementing an
66 // OpenMP 3.1 data sharing rule, even OpenMP 4 is supported, so a plain OpenMP version 4 check
67 // isn't enough (see https://www.gnu.org/software/gcc/gcc-9/porting_to.html#ompdatasharing)
68 #if (defined _OPENMP && (_OPENMP <= 201307)) || (defined __GNUC__ && (__GNUC__ >= 6 && __GNUC__ < 9))
69  #define OPENMP_LEGACY_CONST_DATA_SHARING_RULE 1
70 #else
71  #define OPENMP_LEGACY_CONST_DATA_SHARING_RULE 0
72 #endif
73 
74 #include <pcl/pcl_config.h>
75 
76 #include <boost/preprocessor/comparison/equal.hpp>
77 #include <boost/preprocessor/comparison/less.hpp>
78 #include <boost/preprocessor/control/if.hpp>
79 #include <boost/preprocessor/stringize.hpp>
80 
81 // MSVC < 2019 have issues:
82 // * can't short-circuiting logic in macros
83 // * don't define standard macros
84 // => this leads to annyoing C4067 warnings (see https://developercommunity.visualstudio.com/content/problem/327796/-has-cpp-attribute-emits-warning-is-wrong-highligh.html)
85 #if defined(_MSC_VER)
86  // nvcc on msvc can't work with [[deprecated]]
87  #if !defined(__CUDACC__)
88  #define _PCL_DEPRECATED_IMPL(Message) [[deprecated(Message)]]
89  #else
90  #define _PCL_DEPRECATED_IMPL(Message)
91  #endif
92 #elif __has_cpp_attribute(deprecated)
93  #define _PCL_DEPRECATED_IMPL(Message) [[deprecated(Message)]]
94 #else
95  #warning "You need to implement _PCL_DEPRECATED_IMPL for this compiler"
96  #define _PCL_DEPRECATED_IMPL(Message)
97 #endif
98 
99 // Macro for pragma operator
100 #if (defined (__GNUC__) || defined(__clang__))
101  #define PCL_PRAGMA(x) _Pragma (#x)
102 #elif _MSC_VER
103  #define PCL_PRAGMA(x) __pragma (#x)
104 #else
105  #define PCL_PRAGMA
106 #endif
107 
108 // Macro for emitting pragma warning for deprecated headers
109 #if (defined (__GNUC__) || defined(__clang__))
110  #define _PCL_DEPRECATED_HEADER_IMPL(Message) PCL_PRAGMA (GCC warning Message)
111 #elif _MSC_VER
112  #define _PCL_DEPRECATED_HEADER_IMPL(Message) PCL_PRAGMA (warning (Message))
113 #else
114  #warning "You need to implement _PCL_DEPRECATED_HEADER_IMPL for this compiler"
115  #define _PCL_DEPRECATED_HEADER_IMPL(Message)
116 #endif
117 
118 /**
119  * \brief A handy way to inform the user of the removal deadline
120  */
121 #define _PCL_PREPARE_REMOVAL_MESSAGE(Major, Minor, Msg) \
122  Msg " (It will be removed in PCL " BOOST_PP_STRINGIZE(Major.Minor) ")"
123 
124 /**
125  * \brief Tests for Minor < PCL_MINOR_VERSION
126  */
127 #define _PCL_COMPAT_MINOR_VERSION(Minor, IfPass, IfFail) \
128  BOOST_PP_IF(BOOST_PP_LESS(PCL_MINOR_VERSION, Minor), IfPass, IfFail)
129 
130 /**
131  * \brief Tests for Major == PCL_MAJOR_VERSION
132  */
133 #define _PCL_COMPAT_MAJOR_VERSION(Major, IfPass, IfFail) \
134  BOOST_PP_IF(BOOST_PP_EQUAL(PCL_MAJOR_VERSION, Major), IfPass, IfFail)
135 
136 /**
137  * \brief macro for compatibility across compilers and help remove old deprecated
138  * items for the Major.Minor release
139  *
140  * \details compiler errors of `unneeded_deprecation` and `major_version_mismatch`
141  * are hints to the developer that those items can be safely removed.
142  * Behavior of PCL_DEPRECATED(1, 99, "Not needed anymore")
143  * * till PCL 1.98: "Not needed anymore (It will be removed in PCL 1.99)"
144  * * PCL 1.99 onwards: compiler error with "unneeded_deprecation"
145  * * PCL 2.0 onwards: compiler error with "major_version_mismatch"
146  */
147 #define PCL_DEPRECATED(Major, Minor, Message) \
148  _PCL_COMPAT_MAJOR_VERSION( \
149  Major, \
150  _PCL_COMPAT_MINOR_VERSION( \
151  Minor, \
152  _PCL_DEPRECATED_IMPL(_PCL_PREPARE_REMOVAL_MESSAGE(Major, Minor, Message)), \
153  unneeded_deprecation), \
154  major_version_mismatch)
155 
156 /**
157  * \brief macro for compatibility across compilers and help remove old deprecated
158  * headers for the Major.Minor release
159  *
160  * \details compiler errors of `unneeded_header` and `major_version_mismatch`
161  * are hints to the developer that those items can be safely removed.
162  * Behavior of PCL_DEPRECATED_HEADER(1, 99, "Use file <newfile.h> instead.")
163  * * till PCL 1.98: "This header is deprecated. Use file <newfile.h> instead. (It will be removed in PCL 1.99)"
164  * * PCL 1.99 onwards: compiler error with "unneeded_header"
165  * * PCL 2.0 onwards: compiler error with "major_version_mismatch"
166  */
167 #define PCL_DEPRECATED_HEADER(Major, Minor, Message) \
168  _PCL_COMPAT_MAJOR_VERSION( \
169  Major, \
170  _PCL_COMPAT_MINOR_VERSION( \
171  Minor, \
172  _PCL_DEPRECATED_HEADER_IMPL(_PCL_PREPARE_REMOVAL_MESSAGE( \
173  Major, \
174  Minor, \
175  "This header is deprecated. " Message)), \
176  unneeded_header), \
177  major_version_mismatch)
178 
179 #if defined _WIN32
180 // Define math constants, without including math.h, to prevent polluting global namespace with old math methods
181 // Copied from math.h
182 // Check for M_2_SQRTPI since the cmath header on mingw-w64 doesn't seem to define
183 // _MATH_DEFINES_DEFINED when included with _USE_MATH_DEFINES
184 #if !defined _MATH_DEFINES_DEFINED && !defined M_2_SQRTPI
185  #define _MATH_DEFINES_DEFINED
186 
187  #define M_E 2.71828182845904523536 // e
188  #define M_LOG2E 1.44269504088896340736 // log2(e)
189  #define M_LOG10E 0.434294481903251827651 // log10(e)
190  #define M_LN2 0.693147180559945309417 // ln(2)
191  #define M_LN10 2.30258509299404568402 // ln(10)
192  #define M_PI 3.14159265358979323846 // pi
193  #define M_PI_2 1.57079632679489661923 // pi/2
194  #define M_PI_4 0.785398163397448309616 // pi/4
195  #define M_1_PI 0.318309886183790671538 // 1/pi
196  #define M_2_PI 0.636619772367581343076 // 2/pi
197  #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
198  #define M_SQRT2 1.41421356237309504880 // sqrt(2)
199  #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)
200 #endif
201 
202 #if defined _MSC_VER
203  // Stupid. This should be removed when all the PCL dependencies have min/max fixed.
204  #ifndef NOMINMAX
205  #define NOMINMAX
206  #endif
207 
208  #define __PRETTY_FUNCTION__ __FUNCTION__
209  #define __func__ __FUNCTION__
210 #endif
211 #endif // defined _WIN32
212 
213 
214 template<typename T>
215 PCL_DEPRECATED(1, 12, "use std::isnan instead of pcl_isnan")
216 bool pcl_isnan (T&& x) { return std::isnan (std::forward<T> (x)); }
217 
218 template<typename T>
219 PCL_DEPRECATED(1, 12, "use std::isfinite instead of pcl_isfinite")
220 bool pcl_isfinite (T&& x) { return std::isfinite (std::forward<T> (x)); }
221 
222 template<typename T>
223 PCL_DEPRECATED(1, 12, "use std::isinf instead of pcl_isinf")
224 bool pcl_isinf (T&& x) { return std::isinf (std::forward<T> (x)); }
225 
226 
227 #ifndef DEG2RAD
228 #define DEG2RAD(x) ((x)*0.017453293)
229 #endif
230 
231 #ifndef RAD2DEG
232 #define RAD2DEG(x) ((x)*57.29578)
233 #endif
234 
235 /** \brief Macro that maps version information given by major.minor.patch to a linear integer value to enable easy comparison
236  */
237 #define PCL_LINEAR_VERSION(major,minor,patch) ((major)<<16|(minor)<<8|(patch))
238 
239 /** Win32 doesn't seem to have rounding functions.
240  * Therefore implement our own versions of these functions here.
241  */
242 
243 __inline double
244 pcl_round (double number)
245 {
246  return (number < 0.0 ? std::ceil (number - 0.5) : std::floor (number + 0.5));
247 }
248 __inline float
249 pcl_round (float number)
250 {
251  return (number < 0.0f ? std::ceil (number - 0.5f) : std::floor (number + 0.5f));
252 }
253 
254 #ifdef __GNUC__
255 #define pcl_lrint(x) (lrint(static_cast<double> (x)))
256 #define pcl_lrintf(x) (lrintf(static_cast<float> (x)))
257 #else
258 #define pcl_lrint(x) (static_cast<long int>(pcl_round(x)))
259 #define pcl_lrintf(x) (static_cast<long int>(pcl_round(x)))
260 #endif
261 
262 #ifdef WIN32
263 #define pcl_sleep(x) Sleep(1000*(x))
264 #else
265 #define pcl_sleep(x) sleep(x)
266 #endif
267 
268 #ifndef PVAR
269  #define PVAR(s) \
270  #s << " = " << (s) << std::flush
271 #endif
272 #ifndef PVARN
273 #define PVARN(s) \
274  #s << " = " << (s) << "\n"
275 #endif
276 #ifndef PVARC
277 #define PVARC(s) \
278  #s << " = " << (s) << ", " << std::flush
279 #endif
280 #ifndef PVARS
281 #define PVARS(s) \
282  #s << " = " << (s) << " " << std::flush
283 #endif
284 #ifndef PVARA
285 #define PVARA(s) \
286  #s << " = " << RAD2DEG(s) << "deg" << std::flush
287 #endif
288 #ifndef PVARAN
289 #define PVARAN(s) \
290  #s << " = " << RAD2DEG(s) << "deg\n"
291 #endif
292 #ifndef PVARAC
293 #define PVARAC(s) \
294  #s << " = " << RAD2DEG(s) << "deg, " << std::flush
295 #endif
296 #ifndef PVARAS
297 #define PVARAS(s) \
298  #s << " = " << RAD2DEG(s) << "deg " << std::flush
299 #endif
300 
301 #define FIXED(s) \
302  std::fixed << s << std::resetiosflags(std::ios_base::fixed)
303 
304 #ifndef ERASE_STRUCT
305 #define ERASE_STRUCT(var) memset(&var, 0, sizeof(var))
306 #endif
307 
308 #ifndef ERASE_ARRAY
309 #define ERASE_ARRAY(var, size) memset(var, 0, size*sizeof(*var))
310 #endif
311 
312 #ifndef SET_ARRAY
313 #define SET_ARRAY(var, value, size) { for (decltype(size) i = 0; i < size; ++i) var[i]=value; }
314 #endif
315 
316 #ifndef PCL_EXTERN_C
317  #ifdef __cplusplus
318  #define PCL_EXTERN_C extern "C"
319  #else
320  #define PCL_EXTERN_C
321  #endif
322 #endif
323 
324 #if defined WIN32 || defined _WIN32 || defined WINCE || defined __MINGW32__
325  #ifdef PCLAPI_EXPORTS
326  #define PCL_EXPORTS __declspec(dllexport)
327  #else
328  #define PCL_EXPORTS
329  #endif
330 #else
331  #define PCL_EXPORTS
332 #endif
333 
334 #if defined WIN32 || defined _WIN32
335  #define PCL_CDECL __cdecl
336  #define PCL_STDCALL __stdcall
337 #else
338  #define PCL_CDECL
339  #define PCL_STDCALL
340 #endif
341 
342 #ifndef PCLAPI
343  #define PCLAPI(rettype) PCL_EXTERN_C PCL_EXPORTS rettype PCL_CDECL
344 #endif
345 
346 //for clang cf. http://clang.llvm.org/docs/LanguageExtensions.html
347 #ifndef __has_extension
348  #define __has_extension(x) 0 // Compatibility with pre-3.0 compilers.
349 #endif
350 
351 #if defined (__GNUC__) || defined (__PGI) || defined (__IBMCPP__) || defined (__SUNPRO_CC)
352  #define PCL_ALIGN(alignment) __attribute__((aligned(alignment)))
353 #elif defined (_MSC_VER)
354  #define PCL_ALIGN(alignment) __declspec(align(alignment))
355 #else
356  #error Alignment not supported on your platform
357 #endif
358 
359 #if defined(__GLIBC__) && PCL_LINEAR_VERSION(__GLIBC__,__GLIBC_MINOR__,0)>PCL_LINEAR_VERSION(2,8,0)
360  #define GLIBC_MALLOC_ALIGNED 1
361 #else
362  #define GLIBC_MALLOC_ALIGNED 0
363 #endif
364 
365 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
366  #define FREEBSD_MALLOC_ALIGNED 1
367 #else
368  #define FREEBSD_MALLOC_ALIGNED 0
369 #endif
370 
371 #if defined(__APPLE__) || defined(_WIN64) || GLIBC_MALLOC_ALIGNED || FREEBSD_MALLOC_ALIGNED
372  #define MALLOC_ALIGNED 1
373 #endif
374 
375 #if defined (HAVE_MM_MALLOC)
376  // Intel compiler defines an incompatible _mm_malloc signature
377  #if defined(__INTEL_COMPILER)
378  #include <malloc.h>
379  #else
380  #include <mm_malloc.h>
381  #endif
382 #endif
383 
384 inline void*
385 aligned_malloc (std::size_t size)
386 {
387  void *ptr;
388 #if defined (MALLOC_ALIGNED)
389  ptr = std::malloc (size);
390 #elif defined (HAVE_POSIX_MEMALIGN)
391  if (posix_memalign (&ptr, 16, size))
392  ptr = 0;
393 #elif defined (HAVE_MM_MALLOC)
394  ptr = _mm_malloc (size, 16);
395 #elif defined (_MSC_VER)
396  ptr = _aligned_malloc (size, 16);
397 #elif defined (ANDROID)
398  ptr = memalign (16, size);
399 #else
400  #error aligned_malloc not supported on your platform
401  ptr = 0;
402 #endif
403  return (ptr);
404 }
405 
406 inline void
407 aligned_free (void* ptr)
408 {
409 #if defined (MALLOC_ALIGNED) || defined (HAVE_POSIX_MEMALIGN)
410  std::free (ptr);
411 #elif defined (HAVE_MM_MALLOC)
412  _mm_free (ptr);
413 #elif defined (_MSC_VER)
414  _aligned_free (ptr);
415 #elif defined (ANDROID)
416  free (ptr);
417 #else
418  #error aligned_free not supported on your platform
419 #endif
420 }
421 
422 /**
423  * \brief Macro to add a no-op or a fallthrough attribute based on compiler feature
424  *
425  * \ingroup common
426  */
427 #if (__cplusplus >= 201703L) || (defined(_MSC_VER) && (_MSC_VER >= 1910) && (_MSVC_LANG >= 201703L))
428  #define PCL_FALLTHROUGH [[fallthrough]];
429 #elif defined(__clang__)
430  #define PCL_FALLTHROUGH [[clang::fallthrough]];
431 #elif defined(__GNUC__) && (__GNUC__ >= 7)
432  #define PCL_FALLTHROUGH [[gnu::fallthrough]];
433 #else
434  #define PCL_FALLTHROUGH
435 #endif
436 
437 #if (__cplusplus >= 201703L) || (defined(_MSC_VER) && (_MSC_VER >= 1911) && (_MSVC_LANG >= 201703L))
438  #define PCL_NODISCARD [[nodiscard]]
439 #elif defined(__clang__) && (PCL_LINEAR_VERSION(__clang_major__, __clang_minor__, 0) >= PCL_LINEAR_VERSION(3, 9, 0))
440  #define PCL_NODISCARD [[clang::warn_unused_result]]
441 #elif defined(__GNUC__)
442  #define PCL_NODISCARD [[gnu::warn_unused_result]]
443 #else
444  #define PCL_NODISCARD
445 #endif
pcl_isnan
bool pcl_isnan(T &&x)
Definition: pcl_macros.h:216
aligned_free
void aligned_free(void *ptr)
Definition: pcl_macros.h:407
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:147
pcl_isfinite
bool pcl_isfinite(T &&x)
Definition: pcl_macros.h:220
aligned_malloc
void * aligned_malloc(std::size_t size)
Definition: pcl_macros.h:385
pcl_isinf
bool pcl_isinf(T &&x)
Definition: pcl_macros.h:224
pcl_round
__inline double pcl_round(double number)
Win32 doesn't seem to have rounding functions.
Definition: pcl_macros.h:244