1 #ifndef SimTK_SimTKCOMMON_COMMON_H_
2 #define SimTK_SimTKCOMMON_COMMON_H_
104 #ifndef SimTK_DEFAULT_PRECISION
105 # define SimTK_DEFAULT_PRECISION 2
108 #if (SimTK_DEFAULT_PRECISION == 1)
111 #elif (SimTK_DEFAULT_PRECISION == 2)
114 #elif (SimTK_DEFAULT_PRECISION == 4)
118 #error ILLEGAL VALUE FOR DEFAULT PRECISION
122 #if defined(__cplusplus)
124 #define SimTK_DEBUG(s) std::printf("DBG: " s)
125 #define SimTK_DEBUG1(s,a1) std::printf("DBG: " s,a1)
126 #define SimTK_DEBUG2(s,a1,a2) std::printf("DBG: " s,a1,a2)
127 #define SimTK_DEBUG3(s,a1,a2,a3) std::printf("DBG: " s,a1,a2,a3)
128 #define SimTK_DEBUG4(s,a1,a2,a3,a4) std::printf("DBG: " s,a1,a2,a3,a4)
131 #define SimTK_DEBUG(s) printf("DBG: " s)
132 #define SimTK_DEBUG1(s,a1) printf("DBG: " s,a1)
133 #define SimTK_DEBUG2(s,a1,a2) printf("DBG: " s,a1,a2)
134 #define SimTK_DEBUG3(s,a1,a2,a3) printf("DBG: " s,a1,a2,a3)
135 #define SimTK_DEBUG4(s,a1,a2,a3,a4) printf("DBG: " s,a1,a2,a3,a4)
138 #define SimTK_DEBUG(s)
139 #define SimTK_DEBUG1(s,a1)
140 #define SimTK_DEBUG2(s,a1,a2)
141 #define SimTK_DEBUG3(s,a1,a2,a3)
142 #define SimTK_DEBUG4(s,a1,a2,a3,a4)
167 #pragma warning(disable:4231)
168 #pragma warning(disable:4251)
169 #pragma warning(disable:4275)
170 #pragma warning(disable:4345)
172 #if defined(SimTK_SimTKCOMMON_BUILDING_SHARED_LIBRARY)
173 #define SimTK_SimTKCOMMON_EXPORT __declspec(dllexport)
176 #pragma warning(disable:4661)
178 #elif defined(SimTK_SimTKCOMMON_BUILDING_STATIC_LIBRARY) || defined(SimTK_USE_STATIC_LIBRARIES)
179 #define SimTK_SimTKCOMMON_EXPORT
181 #define SimTK_SimTKCOMMON_EXPORT __declspec(dllimport)
202 #define SimTK_SimTKCOMMON_EXPORT // Linux, Mac
208 #if defined(__cplusplus)
220 #if defined(__cplusplus)
228 #if defined(__cplusplus)
243 #define OVERRIDE_11 override
244 #define FINAL_11 final
246 #define OVERRIDE_11 override
247 #define FINAL_11 sealed
261 #if defined(_MSC_VER) && (_MSC_VER <= 1700) // VC++ 12 (2013, _MSC_VER=1800) added these
263 inline bool isfinite(
float f) {
return _finite(f) != 0;}
264 inline bool isfinite(
double d) {
return _finite(d) != 0;}
265 inline bool isfinite(
long double l) {
return _finite(l) != 0;}
266 inline bool isnan(
float f) {
return _isnan(f) != 0;}
267 inline bool isnan(
double d) {
return _isnan(d) != 0;}
268 inline bool isnan(
long double l) {
return _isnan(l) != 0;}
269 inline bool isinf(
float f) {
return std::abs(f)==std::numeric_limits<float>::infinity();}
270 inline bool isinf(
double d) {
return std::abs(d)==std::numeric_limits<double>::infinity();}
271 inline bool isinf(
long double l) {
return std::abs(l)==std::numeric_limits<double>::infinity();}
272 inline bool signbit(
float f) {
return (*reinterpret_cast<unsigned*>(&f) & 0x80000000U) != 0;}
273 inline bool signbit(
double d) {
return (*reinterpret_cast<unsigned long long*>(&d)
274 & 0x8000000000000000ULL) != 0;}
275 inline bool signbit(
long double l) {
return (*reinterpret_cast<unsigned long long*>(&l)
276 & 0x8000000000000000ULL) != 0;}
293 inline bool canStoreInInt(
unsigned int u) {
return (
unsigned int)(int(u)) == u;}
295 inline bool canStoreInInt(
unsigned long u) {
return (
unsigned long)(int(u)) == u;}
297 inline bool canStoreInInt(
unsigned long long u) {
return (
unsigned long long)(int(u)) == u;}
320 inline bool isSizeInRange(
signed char sz,
signed char mx){
return 0<=sz&&sz<=mx;}
324 inline bool isSizeInRange(
long long sz,
long long mx){
return 0<=sz&&sz<=mx;}
325 inline bool isSizeInRange(
unsigned char sz,
unsigned char mx){
return sz<=mx;}
326 inline bool isSizeInRange(
unsigned short sz,
unsigned short mx){
return sz<=mx;}
328 inline bool isSizeInRange(
unsigned long sz,
unsigned long mx){
return sz<=mx;}
329 inline bool isSizeInRange(
unsigned long long sz,
unsigned long long mx){
return sz<=mx;}
348 inline bool isIndexInRange(
unsigned long long ix,
unsigned long long sz){
return ix<sz;}
407 #define SimTK_DEFINE_UNIQUE_INDEX_TYPE(NAME) \
408 SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(,,,NAME) \
409 static const NAME Invalid ## NAME;
413 #define SimTK_DEFINE_AND_EXPORT_UNIQUE_INDEX_TYPE(EXPORT,NAME) \
414 SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(EXPORT,,,NAME) \
415 static const NAME Invalid ## NAME;
418 #define SimTK_DEFINE_UNIQUE_LOCAL_INDEX_TYPE(PARENT,NAME) \
419 SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(,PARENT,::,NAME)
423 #define SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(EXPORT,PARENT,SEP,NAME) \
424 class EXPORT NAME { \
427 NAME() : ix(SimTK::InvalidIndex) { } \
428 explicit NAME(int i) : ix(i) {assert(i>=0 || i==SimTK::InvalidIndex);} \
429 explicit NAME(long l): ix((int)l) {assert(SimTK::canStoreInNonnegativeInt(l));} \
430 explicit NAME(unsigned int u) : ix((int)u) {assert(SimTK::canStoreInInt(u));} \
431 explicit NAME(unsigned long ul) : ix((int)ul) {assert(SimTK::canStoreInInt(ul));} \
432 operator int() const {return ix;} \
433 bool isValid() const {return ix>=0;} \
434 bool isValidExtended() const {return ix>=-1;} \
435 void invalidate(){ix=SimTK::InvalidIndex;} \
437 bool operator==(int i) const {assert(isValidExtended() && isValidExtended(i)); return ix==i;} \
438 bool operator==(short s) const{assert(isValidExtended() && isValidExtended(s)); return ix==(int)s;} \
439 bool operator==(long l) const {assert(isValidExtended() && isValidExtended(l)); return ix==(int)l;} \
440 bool operator==(unsigned int u) const {assert(isValidExtended() && isValid(u)); return ix==(int)u;} \
441 bool operator==(unsigned short us)const {assert(isValidExtended() && isValid(us)); return ix==(int)us;} \
442 bool operator==(unsigned long ul) const {assert(isValidExtended() && isValid(ul)); return ix==(int)ul;} \
443 bool operator!=(int i) const {return !operator==(i);} \
444 bool operator!=(short s) const {return !operator==(s);} \
445 bool operator!=(long l) const {return !operator==(l);} \
446 bool operator!=(unsigned int u) const {return !operator==(u);} \
447 bool operator!=(unsigned long ul) const {return !operator==(ul);} \
449 bool operator< (int i) const {assert(isValidExtended() && isValidExtended(i)); return ix<i;} \
450 bool operator< (short s) const{assert(isValidExtended() && isValidExtended(s)); return ix<(int)s;} \
451 bool operator< (long l) const {assert(isValidExtended() && isValidExtended(l)); return ix<(int)l;} \
452 bool operator< (unsigned int u) const {assert(isValidExtended() && isValid(u)); return ix<(int)u;} \
453 bool operator< (unsigned short us)const {assert(isValidExtended() && isValid(us)); return ix<(int)us;} \
454 bool operator< (unsigned long ul) const {assert(isValidExtended() && isValid(ul)); return ix<(int)ul;} \
455 bool operator>=(int i) const {return !operator<(i);} \
456 bool operator>=(short s) const {return !operator<(s);} \
457 bool operator>=(long l) const {return !operator<(l);} \
458 bool operator>=(unsigned int u) const {return !operator<(u);} \
459 bool operator>=(unsigned short us)const {return !operator<(us);} \
460 bool operator>=(unsigned long ul) const {return !operator<(ul);} \
462 bool operator> (int i) const {assert(isValidExtended() && isValidExtended(i)); return ix>i;} \
463 bool operator> (short s) const{assert(isValidExtended() && isValidExtended(s)); return ix>(int)s;} \
464 bool operator> (long l) const {assert(isValidExtended() && isValidExtended(l)); return ix>(int)l;} \
465 bool operator> (unsigned int u) const {assert(isValidExtended() && isValid(u)); return ix>(int)u;} \
466 bool operator> (unsigned short us)const {assert(isValidExtended() && isValid(us)); return ix>(int)us;} \
467 bool operator> (unsigned long ul) const {assert(isValidExtended() && isValid(ul)); return ix>(int)ul;} \
468 bool operator<=(int i) const {return !operator>(i);} \
469 bool operator<=(short s) const {return !operator>(s);} \
470 bool operator<=(long l) const {return !operator>(l);} \
471 bool operator<=(unsigned int u) const {return !operator>(u);} \
472 bool operator<=(unsigned short us)const {return !operator>(us);} \
473 bool operator<=(unsigned long ul) const {return !operator>(ul);} \
475 const NAME& operator++() {assert(isValid()); ++ix; return *this;} \
476 NAME operator++(int) {assert(isValid()); ++ix; return NAME(ix-1);} \
477 const NAME& operator--() {assert(isValid()); --ix; return *this;} \
478 NAME operator--(int) {assert(isValid()); --ix; return NAME(ix+1);} \
479 NAME next() const {assert(isValid()); return NAME(ix+1);} \
480 NAME prev() const {assert(isValid()); return NAME(ix-1);} \
482 NAME& operator+=(int i) {assert(isValid() && isValidExtended(ix+i)); ix+=i; return *this;} \
483 NAME& operator-=(int i) {assert(isValid() && isValidExtended(ix-i)); ix-=i; return *this;} \
484 NAME& operator+=(short s){assert(isValid() && SimTK::canStoreInInt(s) && isValidExtended(ix+(int)s)); ix+=(int)s; return *this;} \
485 NAME& operator-=(short s){assert(isValid() && SimTK::canStoreInInt(s) && isValidExtended(ix-(int)s)); ix-=(int)s; return *this;} \
486 NAME& operator+=(long l) {assert(isValid() && SimTK::canStoreInInt(l) && isValidExtended(ix+(int)l)); ix+=(int)l; return *this;} \
487 NAME& operator-=(long l) {assert(isValid() && SimTK::canStoreInInt(l) && isValidExtended(ix-(int)l)); ix-=(int)l; return *this;} \
488 NAME& operator+=(unsigned int u) {assert(isValid()&& SimTK::canStoreInInt(u) && isValid(ix+(int)u)); ix+=(int)u; return *this;} \
489 NAME& operator-=(unsigned int u) {assert(isValid()&& SimTK::canStoreInInt(u) && isValidExtended(ix-(int)u)); ix-=(int)u; return *this;} \
490 NAME& operator+=(unsigned short us){assert(isValid()&& SimTK::canStoreInInt(us) && isValid(ix+(int)us)); ix+=(int)us; return *this;} \
491 NAME& operator-=(unsigned short us){assert(isValid()&& SimTK::canStoreInInt(us) && isValidExtended(ix-(int)us)); ix-=(int)us; return *this;} \
492 NAME& operator+=(unsigned long ul) {assert(isValid()&& SimTK::canStoreInInt(ul) && isValid(ix+(int)ul)); ix+=(int)ul; return *this;} \
493 NAME& operator-=(unsigned long ul) {assert(isValid()&& SimTK::canStoreInInt(ul) && isValidExtended(ix-(int)ul)); ix-=(int)ul; return *this;} \
495 static const NAME& Invalid() {static const NAME invalid; return invalid;} \
496 static bool isValid(int i) {return i>=0;} \
497 static bool isValid(short s){return s>=0;} \
498 static bool isValid(long l) {return SimTK::canStoreInNonnegativeInt(l);} \
499 static bool isValid(unsigned int u) {return SimTK::canStoreInInt(u);} \
500 static bool isValid(unsigned short) {return true;} \
501 static bool isValid(unsigned long ul) {return SimTK::canStoreInInt(ul);} \
502 static bool isValidExtended(int i) {return i>=-1;} \
503 static bool isValidExtended(short s){return s>=-1;} \
504 static bool isValidExtended(long l) {return SimTK::canStoreInInt(l) && l>=-1;} \
506 typedef int size_type; \
507 typedef int difference_type; \
508 static size_type max_size() {return std::numeric_limits<int>::max();} \
518 #define SimTK_DYNAMIC_CAST_DEBUG dynamic_cast // safe but slow
520 #define SimTK_DYNAMIC_CAST_DEBUG static_cast // unsafe but fast
526 #define SimTK_DOWNCAST(Derived,Parent) \
527 static bool isA(const Parent& p) \
528 { return dynamic_cast<const Derived*>(&p) != 0; } \
529 static const Derived& downcast(const Parent& p) \
530 { return SimTK_DYNAMIC_CAST_DEBUG<const Derived&>(p); } \
531 static Derived& updDowncast(Parent& p) \
532 { return SimTK_DYNAMIC_CAST_DEBUG<Derived&>(p); } \
533 static Derived& downcast(Parent& p) \
534 { return SimTK_DYNAMIC_CAST_DEBUG<Derived&>(p); }
538 #define SimTK_DOWNCAST2(Derived,Helper,Parent) \
539 static bool isA(const Parent& p) \
540 { return Helper::isA(p); } \
541 static const Derived& downcast(const Parent& p) \
542 { return static_cast<const Derived&>(Helper::downcast(p)); } \
543 static Derived& updDowncast(Parent& p) \
544 { return static_cast<Derived&>(Helper::downcast(p)); } \
545 static Derived& downcast(Parent& p) \
546 { return static_cast<Derived&>(Helper::downcast(p)); }
552 #define SimTK_PIMPL_DOWNCAST(Derived, Parent) \
553 static bool isInstanceOf(const Parent&); \
554 static const Derived& downcast(const Parent&); \
555 static Derived& updDowncast(Parent&)
561 namespace Exception { }
577 template <
int M,
class ELT=Real,
int STRIDE=1>
class Vec;
578 template <
int N,
class ELT=Real,
int STRIDE=1>
class Row;
579 template <
int M,
int N,
class ELT=Real,
int CS=M,
int RS=1>
class Mat;
580 template <
int M,
class ELT=Real,
int RS=1>
class SymMat;
587 assert(l>=0 && ofs>=0);
645 #define SimTK_SPECIALIZE_INTEGRAL_TYPE(T) \
646 template<> struct IsIntegralType<T> \
647 {typedef TrueType Result; static const bool result = true;}
676 #define SimTK_SPECIALIZE_FLOATING_TYPE(T) \
677 template<> struct IsFloatingType<T> \
678 {typedef TrueType Result; static const bool result = true;}
729 static const char*
name() {
return typeid(T).
name();}
735 #define SimTK_NICETYPENAME_LITERAL(T) \
736 template <> struct NiceTypeName< T > { \
737 static const char* name() { return #T; } \