Point Cloud Library (PCL)  1.10.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 <cstdarg>
63 #include <cstdio>
64 #include <cstdlib>
65 #include <cstdint>
66 #include <iostream>
67 
68 #include <pcl/pcl_config.h>
69 
70 // It seems that __has_cpp_attribute doesn't work correctly
71 // when compiling with some versions of nvcc so we
72 // additionally check if nvcc is used before setting the
73 // PCL_DEPRECATED macro to [[deprecated]].
74 #if defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated) && !defined(__CUDACC__)
75  #define PCL_DEPRECATED(message) [[deprecated(message)]]
76 #elif defined(__GNUC__) || defined(__clang__)
77  #define PCL_DEPRECATED(message) __attribute__((deprecated(message)))
78 #elif defined(_MSC_VER)
79  // Until Visual Studio 2013 you had to use __declspec(deprecated).
80  // However, we decided to ignore the deprecation for these version because
81  // of simplicity reasons. See PR #3634 for the details.
82  #define PCL_DEPRECATED(message)
83 #else
84  #warning "You need to implement PCL_DEPRECATED for this compiler"
85  #define PCL_DEPRECATED(message)
86 #endif
87 
88 #if defined _WIN32
89 // Define math constants, without including math.h, to prevent polluting global namespace with old math methods
90 // Copied from math.h
91 // Check for M_2_SQRTPI since the cmath header on mingw-w64 doesn't seem to define
92 // _MATH_DEFINES_DEFINED when included with _USE_MATH_DEFINES
93 #if !defined _MATH_DEFINES_DEFINED && !defined M_2_SQRTPI
94  #define _MATH_DEFINES_DEFINED
95 
96  #define M_E 2.71828182845904523536 // e
97  #define M_LOG2E 1.44269504088896340736 // log2(e)
98  #define M_LOG10E 0.434294481903251827651 // log10(e)
99  #define M_LN2 0.693147180559945309417 // ln(2)
100  #define M_LN10 2.30258509299404568402 // ln(10)
101  #define M_PI 3.14159265358979323846 // pi
102  #define M_PI_2 1.57079632679489661923 // pi/2
103  #define M_PI_4 0.785398163397448309616 // pi/4
104  #define M_1_PI 0.318309886183790671538 // 1/pi
105  #define M_2_PI 0.636619772367581343076 // 2/pi
106  #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
107  #define M_SQRT2 1.41421356237309504880 // sqrt(2)
108  #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)
109 #endif
110 
111 #if defined _MSC_VER
112  // Stupid. This should be removed when all the PCL dependencies have min/max fixed.
113  #ifndef NOMINMAX
114  #define NOMINMAX
115  #endif
116 
117  #define __PRETTY_FUNCTION__ __FUNCTION__
118  #define __func__ __FUNCTION__
119 #endif
120 #endif // defined _WIN32
121 
122 
123 template<typename T>
124 PCL_DEPRECATED("use std::isnan instead of pcl_isnan")
125 bool pcl_isnan (T&& x) { return std::isnan (std::forward<T> (x)); }
126 
127 template<typename T>
128 PCL_DEPRECATED("use std::isfinite instead of pcl_isfinite")
129 bool pcl_isfinite (T&& x) { return std::isfinite (std::forward<T> (x)); }
130 
131 template<typename T>
132 PCL_DEPRECATED("use std::isinf instead of pcl_isinf")
133 bool pcl_isinf (T&& x) { return std::isinf (std::forward<T> (x)); }
134 
135 
136 #ifndef DEG2RAD
137 #define DEG2RAD(x) ((x)*0.017453293)
138 #endif
139 
140 #ifndef RAD2DEG
141 #define RAD2DEG(x) ((x)*57.29578)
142 #endif
143 
144 /** \brief Macro that maps version information given by major.minor.patch to a linear integer value to enable easy comparison
145  */
146 #define PCL_LINEAR_VERSION(major,minor,patch) ((major)<<16|(minor)<<8|(patch))
147 
148 /** Win32 doesn't seem to have rounding functions.
149  * Therefore implement our own versions of these functions here.
150  */
151 
152 __inline double
153 pcl_round (double number)
154 {
155  return (number < 0.0 ? std::ceil (number - 0.5) : std::floor (number + 0.5));
156 }
157 __inline float
158 pcl_round (float number)
159 {
160  return (number < 0.0f ? std::ceil (number - 0.5f) : std::floor (number + 0.5f));
161 }
162 
163 #ifdef __GNUC__
164 #define pcl_lrint(x) (lrint(static_cast<double> (x)))
165 #define pcl_lrintf(x) (lrintf(static_cast<float> (x)))
166 #else
167 #define pcl_lrint(x) (static_cast<long int>(pcl_round(x)))
168 #define pcl_lrintf(x) (static_cast<long int>(pcl_round(x)))
169 #endif
170 
171 #ifdef WIN32
172 #define pcl_sleep(x) Sleep(1000*(x))
173 #else
174 #define pcl_sleep(x) sleep(x)
175 #endif
176 
177 #ifndef PVAR
178  #define PVAR(s) \
179  #s << " = " << (s) << std::flush
180 #endif
181 #ifndef PVARN
182 #define PVARN(s) \
183  #s << " = " << (s) << "\n"
184 #endif
185 #ifndef PVARC
186 #define PVARC(s) \
187  #s << " = " << (s) << ", " << std::flush
188 #endif
189 #ifndef PVARS
190 #define PVARS(s) \
191  #s << " = " << (s) << " " << std::flush
192 #endif
193 #ifndef PVARA
194 #define PVARA(s) \
195  #s << " = " << RAD2DEG(s) << "deg" << std::flush
196 #endif
197 #ifndef PVARAN
198 #define PVARAN(s) \
199  #s << " = " << RAD2DEG(s) << "deg\n"
200 #endif
201 #ifndef PVARAC
202 #define PVARAC(s) \
203  #s << " = " << RAD2DEG(s) << "deg, " << std::flush
204 #endif
205 #ifndef PVARAS
206 #define PVARAS(s) \
207  #s << " = " << RAD2DEG(s) << "deg " << std::flush
208 #endif
209 
210 #define FIXED(s) \
211  std::fixed << s << std::resetiosflags(std::ios_base::fixed)
212 
213 #ifndef ERASE_STRUCT
214 #define ERASE_STRUCT(var) memset(&var, 0, sizeof(var))
215 #endif
216 
217 #ifndef ERASE_ARRAY
218 #define ERASE_ARRAY(var, size) memset(var, 0, size*sizeof(*var))
219 #endif
220 
221 #ifndef SET_ARRAY
222 #define SET_ARRAY(var, value, size) { for (decltype(size) i = 0; i < size; ++i) var[i]=value; }
223 #endif
224 
225 #ifndef PCL_EXTERN_C
226  #ifdef __cplusplus
227  #define PCL_EXTERN_C extern "C"
228  #else
229  #define PCL_EXTERN_C
230  #endif
231 #endif
232 
233 #if defined WIN32 || defined _WIN32 || defined WINCE || defined __MINGW32__
234  #ifdef PCLAPI_EXPORTS
235  #define PCL_EXPORTS __declspec(dllexport)
236  #else
237  #define PCL_EXPORTS
238  #endif
239 #else
240  #define PCL_EXPORTS
241 #endif
242 
243 #if defined WIN32 || defined _WIN32
244  #define PCL_CDECL __cdecl
245  #define PCL_STDCALL __stdcall
246 #else
247  #define PCL_CDECL
248  #define PCL_STDCALL
249 #endif
250 
251 #ifndef PCLAPI
252  #define PCLAPI(rettype) PCL_EXTERN_C PCL_EXPORTS rettype PCL_CDECL
253 #endif
254 
255 // Macro for pragma operator
256 #if (defined (__GNUC__) || defined(__clang__))
257  #define PCL_PRAGMA(x) _Pragma (#x)
258 #elif _MSC_VER
259  #define PCL_PRAGMA(x) __pragma (#x)
260 #else
261  #define PCL_PRAGMA
262 #endif
263 
264 // Macro for emitting pragma warning
265 #if (defined (__GNUC__) || defined(__clang__))
266  #define PCL_PRAGMA_WARNING(x) PCL_PRAGMA (GCC warning x)
267 #elif _MSC_VER
268  #define PCL_PRAGMA_WARNING(x) PCL_PRAGMA (warning (x))
269 #else
270  #define PCL_PRAGMA_WARNING
271 #endif
272 
273 //for clang cf. http://clang.llvm.org/docs/LanguageExtensions.html
274 #ifndef __has_extension
275  #define __has_extension(x) 0 // Compatibility with pre-3.0 compilers.
276 #endif
277 
278 #if defined (__GNUC__) || defined (__PGI) || defined (__IBMCPP__) || defined (__SUNPRO_CC)
279  #define PCL_ALIGN(alignment) __attribute__((aligned(alignment)))
280 #elif defined (_MSC_VER)
281  #define PCL_ALIGN(alignment) __declspec(align(alignment))
282 #else
283  #error Alignment not supported on your platform
284 #endif
285 
286 #if defined(__GLIBC__) && PCL_LINEAR_VERSION(__GLIBC__,__GLIBC_MINOR__,0)>PCL_LINEAR_VERSION(2,8,0)
287  #define GLIBC_MALLOC_ALIGNED 1
288 #else
289  #define GLIBC_MALLOC_ALIGNED 0
290 #endif
291 
292 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
293  #define FREEBSD_MALLOC_ALIGNED 1
294 #else
295  #define FREEBSD_MALLOC_ALIGNED 0
296 #endif
297 
298 #if defined(__APPLE__) || defined(_WIN64) || GLIBC_MALLOC_ALIGNED || FREEBSD_MALLOC_ALIGNED
299  #define MALLOC_ALIGNED 1
300 #endif
301 
302 #if defined (HAVE_MM_MALLOC)
303  // Intel compiler defines an incompatible _mm_malloc signature
304  #if defined(__INTEL_COMPILER)
305  #include <malloc.h>
306  #else
307  #include <mm_malloc.h>
308  #endif
309 #endif
310 
311 inline void*
312 aligned_malloc (std::size_t size)
313 {
314  void *ptr;
315 #if defined (MALLOC_ALIGNED)
316  ptr = std::malloc (size);
317 #elif defined (HAVE_POSIX_MEMALIGN)
318  if (posix_memalign (&ptr, 16, size))
319  ptr = 0;
320 #elif defined (HAVE_MM_MALLOC)
321  ptr = _mm_malloc (size, 16);
322 #elif defined (_MSC_VER)
323  ptr = _aligned_malloc (size, 16);
324 #elif defined (ANDROID)
325  ptr = memalign (16, size);
326 #else
327  #error aligned_malloc not supported on your platform
328  ptr = 0;
329 #endif
330  return (ptr);
331 }
332 
333 inline void
334 aligned_free (void* ptr)
335 {
336 #if defined (MALLOC_ALIGNED) || defined (HAVE_POSIX_MEMALIGN)
337  std::free (ptr);
338 #elif defined (HAVE_MM_MALLOC)
339  _mm_free (ptr);
340 #elif defined (_MSC_VER)
341  _aligned_free (ptr);
342 #elif defined (ANDROID)
343  free (ptr);
344 #else
345  #error aligned_free not supported on your platform
346 #endif
347 }
348 
349 /**
350  * \brief Macro to add a no-op or a fallthrough attribute based on compiler feature
351  *
352  * \ingroup common
353  */
354 #if (__cplusplus >= 201703L) || (defined(_MSC_VER) && (_MSC_VER >= 1910) && (_MSVC_LANG >= 201703L))
355  #define PCL_FALLTHROUGH [[fallthrough]];
356 #elif defined(__clang__)
357  #define PCL_FALLTHROUGH [[clang::fallthrough]];
358 #elif defined(__GNUC__) && (__GNUC__ >= 7)
359  #define PCL_FALLTHROUGH [[gnu::fallthrough]];
360 #else
361  #define PCL_FALLTHROUGH
362 #endif
363 
364 #if (__cplusplus >= 201703L) || (defined(_MSC_VER) && (_MSC_VER >= 1911) && (_MSVC_LANG >= 201703L))
365  #define PCL_NODISCARD [[nodiscard]]
366 #elif defined(__clang__) && (PCL_LINEAR_VERSION(__clang_major__, __clang_minor__, 0) >= PCL_LINEAR_VERSION(3, 9, 0))
367  #define PCL_NODISCARD [[clang::warn_unused_result]]
368 #elif defined(__GNUC__)
369  #define PCL_NODISCARD [[gnu::warn_unused_result]]
370 #else
371  #define PCL_NODISCARD
372 #endif
373 
bool pcl_isinf(T &&x)
Definition: pcl_macros.h:133
__inline double pcl_round(double number)
Win32 doesn&#39;t seem to have rounding functions.
Definition: pcl_macros.h:153
bool pcl_isfinite(T &&x)
Definition: pcl_macros.h:129
#define PCL_DEPRECATED(message)
Definition: pcl_macros.h:85
bool pcl_isnan(T &&x)
Definition: pcl_macros.h:125
void aligned_free(void *ptr)
Definition: pcl_macros.h:334
void * aligned_malloc(std::size_t size)
Definition: pcl_macros.h:312