00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef UTILSDEFS_H
00029 #define UTILSDEFS_H
00030
00031
00032
00033
00034 #if defined(_MSC_VER)
00035
00036 #pragma warning(disable:4786)
00037
00038 #pragma warning(disable:4702)
00039
00040 #pragma warning (disable:4244)
00041 #pragma warning (disable:4305)
00042 #pragma warning (disable:4267)
00043
00044 #pragma warning( disable : 4290 )
00045
00046 #pragma warning(disable: 4251)
00047 #pragma warning(disable: 4275)
00048 #endif
00049
00050 #if defined(__BORLANDC__)
00051
00052 #pragma warn -8027
00053
00054 #pragma warn -8012
00055 #pragma warn -8022
00056 #endif
00057
00058
00059 #if defined(_MSC_VER) && (_MSC_VER>=1400)
00060 #if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
00061 #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
00062 #endif
00063 #endif
00064
00065
00066 #include <mrpt/config.h>
00067 #include <mrpt/system/os.h>
00068
00069
00070 #if defined(MRPT_BUILT_AS_DLL) && defined(mrpt_core_EXPORTS)
00071 # define MRPT_SKIP_AUTOINCLUDE_LIBS
00072 #endif
00073
00074 #define _IAMINUTILSDEFS_H
00075 #include <mrpt/utils/utils_impexp.h>
00076 #undef _IAMINUTILSDEFS_H
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 #ifndef BOOST_JOIN
00089 #define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
00090 #define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
00091 #define BOOST_DO_JOIN2( X, Y ) X##Y
00092 #endif
00093
00094
00095
00096
00097
00098
00099
00100 #if (defined(_MSC_VER) || defined(__BORLANDC__)) && !defined(MRPT_SKIP_AUTOINCLUDE_LIBS)
00101 # if defined(_DEBUG)
00102 # pragma comment (lib, BOOST_JOIN( BOOST_JOIN("libmrpt-core",MRPT_VERSION_POSTFIX),"-dbg.lib"))
00103 # if !defined(MRPT_BUILT_AS_DLL) // If built as DLL, user apps only need to link against mrpt-core
00104 # if MRPT_HAS_SIFT_HESS
00105 # pragma comment (lib, BOOST_JOIN( BOOST_JOIN("libmrpt-sift-hess",MRPT_VERSION_POSTFIX),"-dbg.lib"))
00106 # endif
00107 # if !MRPT_HAS_WXWIDGETS && MRPT_HAS_ZLIB && !MRPT_HAS_ZLIB_SYSTEM
00108 # pragma comment (lib, BOOST_JOIN( BOOST_JOIN("libmrpt-zlib",MRPT_VERSION_POSTFIX),"-dbg.lib"))
00109 # endif
00110 # endif
00111 # else
00112 # pragma comment (lib, BOOST_JOIN( BOOST_JOIN("libmrpt-core",MRPT_VERSION_POSTFIX),".lib"))
00113 # if !defined(MRPT_BUILT_AS_DLL) // If built as DLL, user apps only need to link against mrpt-core
00114 # if MRPT_HAS_SIFT_HESS
00115 # pragma comment (lib, BOOST_JOIN( BOOST_JOIN("libmrpt-sift-hess",MRPT_VERSION_POSTFIX),".lib"))
00116 # endif
00117 # if !MRPT_HAS_WXWIDGETS && MRPT_HAS_ZLIB && !MRPT_HAS_ZLIB_SYSTEM
00118 # pragma comment (lib, BOOST_JOIN( BOOST_JOIN("libmrpt-zlib",MRPT_VERSION_POSTFIX),".lib"))
00119 # endif
00120 # endif
00121 # endif
00122 #endif
00123
00124
00125
00126 #if defined(__GNUC__) && (__GNUC__ - 0 > 3 || (__GNUC__ - 0 == 3 && __GNUC_MINOR__ - 0 >= 2))
00127
00128 # define MRPT_DEPRECATED_PRE(_MSG)
00129
00130
00131 # define MRPT_DEPRECATED_POST(_MSG) __attribute__ ((deprecated))
00132 # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
00133
00134 # define MRPT_DEPRECATED_PRE(_MSG) __declspec(deprecated (_MSG))
00135 # define MRPT_DEPRECATED_POST(_MSG)
00136 # else
00137 # define MRPT_DEPRECATED_PRE(_MSG)
00138 # define MRPT_DEPRECATED_POST(_MSG)
00139 # endif
00140
00141
00142 #define MRPT_DECLARE_DEPRECATED_FUNCTION(__MSG, __FUNC) MRPT_DEPRECATED_PRE(__MSG) __FUNC MRPT_DEPRECATED_POST(__MSG)
00143
00144
00145
00146 #if defined(_MSC_VER)
00147 #define MRPT_ALIGN16 __declspec(align(16))
00148 #define MRPT_ALIGN32 __declspec(align(32))
00149 #elif defined(__GNUC__)
00150 #define MRPT_ALIGN16 __attribute__((aligned(16)))
00151 #define MRPT_ALIGN32 __attribute__((aligned(32)))
00152 #else
00153 #define MRPT_ALIGN16
00154 #define MRPT_ALIGN32
00155 #endif
00156
00157
00158
00159 #if defined(_DEBUG) && defined(_MSC_VER) && (_MSC_VER>=1400)
00160 #define _CRTDBG_MAP_ALLOC
00161 #include <stdlib.h>
00162 #include <crtdbg.h>
00163 #endif
00164
00165
00166 #include <stdlib.h>
00167 #include <cstdlib>
00168 #include <cmath>
00169
00170 #if HAVE_ALLOCA_H
00171 #include <alloca.h>
00172 #endif
00173
00174
00175 #include <algorithm>
00176 #include <iostream>
00177 #include <iomanip>
00178 #include <fstream>
00179 #include <cstring>
00180 #include <exception>
00181 #include <stdexcept>
00182 #include <limits>
00183 #include <sstream>
00184
00185
00186 #include <mrpt/otherlibs/stlplus/smart_ptr.hpp>
00187
00188
00189 #include <mrpt/utils/types.h>
00190
00191
00192
00193
00194 namespace mrpt
00195 {
00196
00197
00198
00199
00200
00201 std::string MRPTDLLIMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
00202
00203 namespace utils
00204 {
00205 class CFileStream;
00206 }
00207 namespace system
00208 {
00209
00210 std::string MRPTDLLIMPEXP extractFileName(const std::string &filePath);
00211 std::string MRPTDLLIMPEXP stack_trace(bool calling_from_exception);
00212 }
00213
00214
00215 namespace math
00216 {
00217 bool MRPTDLLIMPEXP isNan(float v);
00218 bool MRPTDLLIMPEXP isNan(double v);
00219 bool MRPTDLLIMPEXP isFinite(float v);
00220 bool MRPTDLLIMPEXP isFinite(double v);
00221 }
00222 }
00223
00224
00225
00226
00227 #if defined(__BORLANDC__)
00228 #define __CURRENT_FUNCTION_NAME__ __FUNC__
00229 #elif defined(_MSC_VER) && (_MSC_VER>=1300)
00230 #define __CURRENT_FUNCTION_NAME__ __FUNCTION__
00231 #elif defined(_MSC_VER) && (_MSC_VER<1300)
00232
00233 #define __CURRENT_FUNCTION_NAME__ ::system::extractFileName(__FILE__).c_str()
00234 #else
00235 #define __CURRENT_FUNCTION_NAME__ __PRETTY_FUNCTION__
00236 #endif
00237
00238
00239
00240
00241
00242
00243
00244 #define THROW_EXCEPTION(msg) \
00245 {\
00246 std::ostringstream auxCompStr;\
00247 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00248 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00249 auxCompStr << msg << std::endl; \
00250 auxCompStr << mrpt::system::stack_trace(); \
00251 throw std::logic_error( auxCompStr.str() );\
00252 }\
00253
00254
00255
00256
00257
00258 #define THROW_EXCEPTION_CUSTOM_MSG1(msg,param1) \
00259 {\
00260 std::ostringstream auxCompStr;\
00261 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00262 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00263 auxCompStr << format(msg,param1)<< std::endl; \
00264 auxCompStr << mrpt::system::stack_trace(); \
00265 throw std::logic_error( auxCompStr.str() );\
00266 }\
00267
00268
00269
00270
00271
00272
00273 #define THROW_TYPED_EXCEPTION(msg,exceptionClass) \
00274 {\
00275 std::ostringstream auxCompStr;\
00276 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00277 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00278 auxCompStr << msg << std::endl; \
00279 auxCompStr << mrpt::system::stack_trace(); \
00280 throw exceptionClass( auxCompStr.str() );\
00281 }\
00282
00283
00284
00285
00286
00287 #define THROW_TYPED_EXCEPTION_CUSTOM_MSG1(msg,param1,exceptionClass) \
00288 {\
00289 std::ostringstream auxCompStr;\
00290 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00291 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00292 auxCompStr << format(msg,param1)<< std::endl; \
00293 auxCompStr << mrpt::system::stack_trace(); \
00294 throw exceptionClass( auxCompStr.str() );\
00295 }\
00296
00297
00298
00299
00300
00301 #define THROW_STACKED_EXCEPTION(e) \
00302 {\
00303 std::string str( e.what() );\
00304 if (str.find("MRPT stack trace")==std::string::npos) \
00305 { \
00306 str+= __CURRENT_FUNCTION_NAME__;\
00307 str+= format(", line %i:\n", __LINE__ );\
00308 if (str.size()>3000) { std::cerr << "TOO MANY STACKED EXCEPTIONS!: " << std::endl << str << std::endl; abort(); } \
00309 throw std::logic_error( str );\
00310 } \
00311 else throw std::logic_error( e.what() );\
00312 }\
00313
00314
00315
00316
00317
00318 #define THROW_STACKED_EXCEPTION_CUSTOM_MSG1(e,msg) \
00319 {\
00320 std::ostringstream auxCompStr;\
00321 auxCompStr << e.what() ; \
00322 auxCompStr << msg << std::endl; \
00323 throw std::logic_error( auxCompStr.str() );\
00324 }\
00325
00326
00327
00328
00329
00330 #define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(e,stuff,param1) \
00331 {\
00332 std::ostringstream auxCompStr;\
00333 auxCompStr << e.what() ; \
00334 auxCompStr << format( stuff, param1 ) << std::endl; \
00335 throw std::logic_error( auxCompStr.str() );\
00336 }\
00337
00338
00339 #define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(V) THROW_EXCEPTION(format("Cannot parse object: unknown serialization version number: '%i'",static_cast<int>(version)))
00340
00341
00342 #if MRPT_HAS_ASSERT
00343
00344
00345
00346
00347 # define ASSERTMSG_(f,__ERROR_MSG) \
00348 { \
00349 if (!(f)) THROW_EXCEPTION( ::std::string( __ERROR_MSG ) ); \
00350 }
00351
00352
00353
00354
00355
00356 # define ASSERT_(f) \
00357 ASSERTMSG_(f, "Assert condition failed: " + ::std::string(#f) )
00358
00359
00360
00361 #define MRPT_CHECK_NORMAL_NUMBER(val) \
00362 { \
00363 double v=(val); \
00364 if (math::isNan(v)) THROW_EXCEPTION("Check failed (value is NaN)"); \
00365 if (!math::isFinite(v)) THROW_EXCEPTION("Check failed (value is not finite)"); \
00366 }
00367
00368
00369
00370
00371 namespace mrpt
00372 {
00373 namespace utils
00374 {
00375 template <bool value> struct compile_time_assert;
00376 template <> struct compile_time_assert<true> { enum {value=1}; };
00377 }
00378 }
00379 #define MRPT_COMPILE_TIME_ASSERT(expression) \
00380 typedef char BOOST_JOIN(MRPT_CTA, __LINE__)[::mrpt::utils::compile_time_assert<(bool)(expression)>::value];
00381
00382 #else
00383 # define ASSERTMSG_(f,__ERROR_MSG) { }
00384 # define ASSERT_(f) { }
00385 # define MRPT_CHECK_NORMAL_NUMBER(val) { }
00386 # define MRPT_COMPILE_TIME_ASSERT(f) { }
00387 #endif
00388
00389
00390
00391
00392
00393 #define MRPT_UNUSED_PARAM(a) (void)(a)
00394
00395 #if MRPT_HAS_STACKED_EXCEPTIONS
00396
00397
00398
00399 # define MRPT_TRY_START \
00400 try { \
00401
00402
00403
00404
00405 # define MRPT_TRY_END \
00406 } \
00407 catch (std::bad_alloc &e) \
00408 { \
00409 throw e; \
00410 } \
00411 catch (std::exception &e) \
00412 { \
00413 THROW_STACKED_EXCEPTION(e); \
00414 } \
00415 catch (...) \
00416 { \
00417 THROW_EXCEPTION("Unexpected runtime error!"); \
00418 } \
00419
00420
00421
00422
00423 # define MRPT_TRY_END_WITH_CLEAN_UP(stuff) \
00424 } \
00425 catch (std::bad_alloc &e) \
00426 { \
00427 throw e; \
00428 } \
00429 catch (std::exception &e) \
00430 { \
00431 {stuff} \
00432 THROW_STACKED_EXCEPTION(e); \
00433 } \
00434 catch (...) \
00435 { \
00436 { stuff } \
00437 THROW_EXCEPTION("Unexpected runtime error!"); \
00438 } \
00439
00440 #else
00441 # define MRPT_TRY_START
00442 # define MRPT_TRY_END
00443 # define MRPT_TRY_END_WITH_CLEAN_UP(stuff)
00444 #endif
00445
00446
00447
00448
00449 #ifndef M_PI
00450
00451
00452 #define M_PI 3.14159265358979323846264338327950288
00453 #endif
00454
00455 #ifndef M_2PI
00456
00457
00458 #define M_2PI 6.283185307179586476925286766559
00459 #endif
00460
00461 #define M_PIf 3.14159265358979f
00462 #define M_2PIf 6.28318530717959f
00463
00464
00465
00466 #if defined(MRPT_OS_WINDOWS) && !defined(NOMINMAX)
00467 # define NOMINMAX
00468 # ifdef max
00469 # undef max
00470 # undef min
00471 # endif
00472 #endif
00473
00474
00475
00476
00477 #if defined(_MSC_VER) && (_MSC_VER<1300)
00478 # ifndef max
00479 namespace std
00480 {
00481 template<class T> inline const T max(const T& A,const T& B) { return A>B ? A:B; }
00482 template<class T> inline const T min(const T& A,const T& B) { return A<B ? A:B; }
00483 }
00484 # else
00485 # define MAX3_MSVC6_VERSION
00486 # endif
00487 #endif
00488
00489
00490 #ifndef MAX3_MSVC6_VERSION
00491 template<typename T> inline const T min3(const T& A, const T& B,const T& C) { return std::min<T>(A, std::min<T>(B,C) ); }
00492 template<typename T> inline const T max3(const T& A, const T& B,const T& C) { return std::max<T>(A, std::max<T>(B,C) ); }
00493 #else
00494 # define max3(A,B,C) max(A,max(B,C))
00495 # define min3(A,B,C) min(A,min(B,C))
00496 #endif
00497
00498 namespace mrpt
00499 {
00500 namespace utils
00501 {
00502
00503 inline double DEG2RAD(const double &x) { return x*M_PI/180.0; }
00504
00505
00506 inline float DEG2RAD(const float &x) { return x*M_PIf/180.0f; }
00507
00508
00509 inline float DEG2RAD(const int &x) { return x*M_PIf/180.0f; }
00510
00511
00512 inline double RAD2DEG(const double &x) { return x*180.0/M_PI; }
00513
00514
00515 inline float RAD2DEG(const float &x) { return x*180.0f/M_PIf; }
00516
00517
00518 template <typename T>
00519 int sign(T x) { return x<0 ? -1:1; }
00520
00521
00522 template <typename T>
00523 int signWithZero(T x) { return x==0?0:sign(x);}
00524
00525
00526 template <typename T>
00527 int round(T x) { return static_cast<int>(floor(static_cast<double>(x+0.5))); }
00528
00529
00530 template <typename T>
00531 long round_long(T x) { return static_cast<long>(floor(static_cast<double>(x+0.5))); }
00532
00533
00534 template <typename T>
00535 int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
00536
00537
00538 template<class T>
00539 inline T square(const T& x) { return x*x; }
00540
00541
00542
00543 template <class R, class P>
00544 R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
00545
00546
00547 template <class R, class P>
00548 const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
00549
00550
00551
00552 template <class T> void reverseBytes(const T &v_in, T& v_out)
00553 {
00554 v_out = v_in;
00555 uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_out);
00556 std::reverse(ptr,ptr+sizeof(T));
00557 }
00558
00559 }
00560 }
00561 #endif