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 #if (defined(_MSC_VER) || defined(__BORLANDC__)) && !defined(MRPT_SKIP_AUTOINCLUDE_LIBS)
00085 # if defined(_DEBUG)
00086 # pragma comment (lib,"libmrpt-core-dbg.lib")
00087 # if !defined(MRPT_BUILT_AS_DLL) // If built as DLL, user apps only need to link against mrpt-core
00088 # pragma comment (lib,"libmrpt-ann-dbg.lib")
00089 # pragma comment (lib,"libmrpt-3ds-dbg.lib")
00090 # pragma comment (lib,"libmrpt-sift-hess-dbg.lib")
00091 # if !MRPT_HAS_WXWIDGETS && MRPT_HAS_ZLIB && !MRPT_HAS_ZLIB_SYSTEM
00092 # pragma comment (lib,"libmrpt-zlib-dbg.lib")
00093 # endif
00094 # endif
00095 # else
00096 # pragma comment (lib,"libmrpt-core.lib")
00097 # if !defined(MRPT_BUILT_AS_DLL) // If built as DLL, user apps only need to link against mrpt-core
00098 # pragma comment (lib,"libmrpt-ann.lib")
00099 # pragma comment (lib,"libmrpt-3ds.lib")
00100 # pragma comment (lib,"libmrpt-sift-hess.lib")
00101 # if !MRPT_HAS_WXWIDGETS && MRPT_HAS_ZLIB && !MRPT_HAS_ZLIB_SYSTEM
00102 # pragma comment (lib,"libmrpt-zlib.lib")
00103 # endif
00104 # endif
00105 # endif
00106 #endif
00107
00108
00109
00110 #if defined(__GNUC__) && (__GNUC__ - 0 > 3 || (__GNUC__ - 0 == 3 && __GNUC_MINOR__ - 0 >= 2))
00111
00112 # define MRPT_DEPRECATED_PRE
00113 # define MRPT_DEPRECATED_POST __attribute__ ((deprecated))
00114 # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
00115
00116 # define MRPT_DEPRECATED_PRE __declspec(deprecated)
00117 # define MRPT_DEPRECATED_POST
00118 # else
00119 # define MRPT_DEPRECATED_PRE
00120 # define MRPT_DEPRECATED_POST
00121 # endif
00122
00123
00124
00125 #include <stdlib.h>
00126 #include <cstdlib>
00127 #include <cmath>
00128
00129 #if HAVE_ALLOCA_H
00130 #include <alloca.h>
00131 #endif
00132
00133
00134 #include <algorithm>
00135 #include <iostream>
00136 #include <iomanip>
00137 #include <fstream>
00138 #include <cstring>
00139 #include <exception>
00140 #include <stdexcept>
00141 #include <limits>
00142 #include <sstream>
00143
00144
00145 #include <mrpt/otherlibs/stlplus/smart_ptr.hpp>
00146
00147
00148 #include <mrpt/utils/types.h>
00149
00150
00151
00152 namespace mrpt
00153 {
00159 std::string MRPTDLLIMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
00160
00161 namespace utils
00162 {
00163 class CFileStream;
00164 }
00165 namespace system
00166 {
00167
00168 std::string MRPTDLLIMPEXP extractFileName(const std::string &filePath);
00169 }
00170
00171
00172 namespace math
00173 {
00174 bool MRPTDLLIMPEXP isNan(float v);
00175 bool MRPTDLLIMPEXP isNan(double v);
00176 bool MRPTDLLIMPEXP isFinite(float v);
00177 bool MRPTDLLIMPEXP isFinite(double v);
00178 }
00179 }
00180
00181
00184 #if defined(__BORLANDC__)
00185 #define __CURRENT_FUNCTION_NAME__ __FUNC__
00186 #elif defined(_MSC_VER) && (_MSC_VER>=1300)
00187 #define __CURRENT_FUNCTION_NAME__ __FUNCTION__
00188 #elif defined(_MSC_VER) && (_MSC_VER<1300)
00189
00190 #define __CURRENT_FUNCTION_NAME__ ::system::extractFileName(__FILE__).c_str()
00191 #else
00192 #define __CURRENT_FUNCTION_NAME__ __PRETTY_FUNCTION__
00193 #endif
00194
00195
00201 #define THROW_EXCEPTION(msg) \
00202 {\
00203 std::ostringstream auxCompStr;\
00204 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00205 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00206 auxCompStr << msg << "\nCALL STACK:\n";\
00207 throw std::logic_error( auxCompStr.str() );\
00208 }\
00209
00210
00214 #define THROW_EXCEPTION_CUSTOM_MSG1(msg,param1) \
00215 {\
00216 std::ostringstream auxCompStr;\
00217 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00218 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00219 auxCompStr << format(msg,param1);\
00220 auxCompStr << "\nCALL STACK:\n";\
00221 throw std::logic_error( auxCompStr.str() );\
00222 }\
00223
00224
00229 #define THROW_TYPED_EXCEPTION(msg,exceptionClass) \
00230 {\
00231 std::ostringstream auxCompStr;\
00232 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00233 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00234 auxCompStr << msg << "\nCALL STACK:\n";\
00235 throw exceptionClass( auxCompStr.str() );\
00236 }\
00237
00238
00242 #define THROW_TYPED_EXCEPTION_CUSTOM_MSG1(msg,param1,exceptionClass) \
00243 {\
00244 std::ostringstream auxCompStr;\
00245 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00246 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00247 auxCompStr << format(msg,param1);\
00248 auxCompStr << "\nCALL STACK:\n";\
00249 throw exceptionClass( auxCompStr.str() );\
00250 }\
00251
00252
00256 #define THROW_STACKED_EXCEPTION(e) \
00257 {\
00258 std::string str( e.what() );\
00259 str+= __CURRENT_FUNCTION_NAME__;\
00260 str+= format(", line %i:\n", __LINE__ );\
00261 if (str.size()>3000) { std::cerr << "TOO MANY STACKED EXCEPTIONS!: " << std::endl << str << std::endl; abort(); } \
00262 throw std::logic_error( str );\
00263 }\
00264
00265
00269 #define THROW_STACKED_EXCEPTION_CUSTOM_MSG1(e,msg) \
00270 {\
00271 std::ostringstream auxCompStr;\
00272 auxCompStr << e.what() ; \
00273 auxCompStr << msg << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << "(User msg above)\n";\
00274 throw std::logic_error( auxCompStr.str() );\
00275 }\
00276
00277
00281 #define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(e,stuff,param1) \
00282 {\
00283 std::ostringstream auxCompStr;\
00284 auxCompStr << e.what(); \
00285 auxCompStr << format( stuff, param1 ) << "\n" << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00286 throw std::logic_error( auxCompStr.str() );\
00287 }\
00288
00289
00290 #define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(V) THROW_EXCEPTION(format("Cannot parse object: unknown serialization version number: '%i'",static_cast<int>(version)))
00291
00292
00293 #if MRPT_HAS_ASSERT
00294
00299 # define ASSERT_(f) \
00300 { \
00301 if (!(f)) \
00302 { \
00303 ::std::string tmpStr("Assert condition failed: "); \
00304 tmpStr+= #f; \
00305 THROW_EXCEPTION( tmpStr.c_str() ); \
00306 } \
00307 }
00308
00311 #define MRPT_CHECK_NORMAL_NUMBER(val) \
00312 { \
00313 double v=(val); \
00314 if (math::isNan(v)) THROW_EXCEPTION("Check failed (value is NaN)"); \
00315 if (!math::isFinite(v)) THROW_EXCEPTION("Check failed (value is not finite)"); \
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 #ifndef BOOST_JOIN
00329 #define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
00330 #define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
00331 #define BOOST_DO_JOIN2( X, Y ) X##Y
00332 #endif
00333
00334
00335
00336
00337 namespace mrpt
00338 {
00339 namespace utils
00340 {
00341 template <bool value> struct compile_time_assert;
00342 template <> struct compile_time_assert<true> { enum {value=1}; };
00343 }
00344 }
00345 #define MRPT_COMPILE_TIME_ASSERT(expression) \
00346 typedef char BOOST_JOIN(MRPT_CTA, __LINE__)[::mrpt::utils::compile_time_assert<(bool)(expression)>::value];
00347
00348 #else
00349 # define ASSERT_(f)
00350 # define MRPT_CHECK_NORMAL_NUMBER(val)
00351 # define MRPT_COMPILE_TIME_ASSERT(f)
00352 #endif
00353
00354
00355
00358 #define MRPT_UNUSED_PARAM(a) (void)(a)
00359
00360 #if MRPT_HAS_STACKED_EXCEPTIONS
00361
00364 # define MRPT_TRY_START \
00365 try { \
00366
00367
00370 # define MRPT_TRY_END \
00371 } \
00372 catch (std::bad_alloc &e) \
00373 { \
00374 throw e; \
00375 } \
00376 catch (std::exception &e) \
00377 { \
00378 THROW_STACKED_EXCEPTION(e); \
00379 } \
00380 catch (...) \
00381 { \
00382 THROW_EXCEPTION("Unexpected runtime error!"); \
00383 } \
00384
00385
00388 # define MRPT_TRY_END_WITH_CLEAN_UP(stuff) \
00389 } \
00390 catch (std::bad_alloc &e) \
00391 { \
00392 throw e; \
00393 } \
00394 catch (std::exception &e) \
00395 { \
00396 {stuff} \
00397 THROW_STACKED_EXCEPTION(e); \
00398 } \
00399 catch (...) \
00400 { \
00401 { stuff } \
00402 THROW_EXCEPTION("Unexpected runtime error!"); \
00403 } \
00404
00405 #else
00406 # define MRPT_TRY_START
00407 # define MRPT_TRY_END
00408 # define MRPT_TRY_END_WITH_CLEAN_UP(stuff)
00409 #endif
00410
00411
00412
00413
00414 #ifndef M_PI
00415
00417 #define M_PI 3.14159265358979323846264338327950288
00418 #endif
00419
00420 #ifndef M_2PI
00421
00423 #define M_2PI 6.283185307179586476925286766559
00424 #endif
00425
00426 #define M_PIf 3.14159265358979f
00427 #define M_2PIf 6.28318530717959f
00428
00429
00430
00431 #if defined(MRPT_OS_WINDOWS) && !defined(NOMINMAX)
00432 # define NOMINMAX
00433 # ifdef max
00434 # undef max
00435 # undef min
00436 # endif
00437 #endif
00438
00439
00440
00441
00442 #if defined(_MSC_VER) && (_MSC_VER<1300)
00443 # ifndef max
00444 namespace std
00445 {
00446 template<class T> inline const T max(const T& A,const T& B) { return A>B ? A:B; }
00447 template<class T> inline const T min(const T& A,const T& B) { return A<B ? A:B; }
00448 }
00449 # else
00450 # define MAX3_MSVC6_VERSION
00451 # endif
00452 #endif
00453
00454
00455 #ifndef MAX3_MSVC6_VERSION
00456 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) ); }
00457 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) ); }
00458 #else
00459 # define max3(A,B,C) max(A,max(B,C))
00460 # define min3(A,B,C) min(A,min(B,C))
00461 #endif
00462
00463 namespace mrpt
00464 {
00465 namespace utils
00466 {
00468 inline double DEG2RAD(const double &x) { return x*M_PI/180.0; }
00469
00471 inline float DEG2RAD(const float &x) { return x*M_PIf/180.0f; }
00472
00474 inline float DEG2RAD(const int &x) { return x*M_PIf/180.0f; }
00475
00477 inline double RAD2DEG(const double &x) { return x*180.0/M_PI; }
00478
00480 inline float RAD2DEG(const float &x) { return x*180.0f/M_PIf; }
00481
00483 template <typename T>
00484 int sign(T x) { return x<0 ? -1:1; }
00485
00487 template <typename T>
00488 int round(T x) { return static_cast<int>(floor(static_cast<double>(x+0.5))); }
00489
00491 template <typename T>
00492 long round_long(T x) { return static_cast<long>(floor(static_cast<double>(x+0.5))); }
00493
00495 template <typename T>
00496 int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
00497
00499 template<class T>
00500 inline T square(const T& x) { return x*x; }
00501
00502
00504 template <class R, class P>
00505 R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
00506
00508 template <class R, class P>
00509 const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
00510
00511
00513 template <class T> void reverseBytes(const T &v_in, T& v_out)
00514 {
00515 v_out = v_in;
00516 uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_out);
00517 std::reverse(ptr,ptr+sizeof(T));
00518 }
00519
00520 }
00521 }
00522 #endif