Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimTKcommon/include/SimTKcommon/internal/common.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_COMMON_H_
2 #define SimTK_SimTKCOMMON_COMMON_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
39 // Provide doxygen documentation for the SimTK namespace.
40 
49 // Define shared doxygen "modules" and sub-modules here. We'll put things
50 // in them at various places when appropriate.
51 
91 /*****************************/
92 /* ANSI-C COMPATIBLE SECTION */
93 /*****************************/
94 
95 /* Set up a few compile-time options that affect all SimTK Core headers. */
96 
104 #ifndef SimTK_DEFAULT_PRECISION
105 # define SimTK_DEFAULT_PRECISION 2
106 #endif
107 
108 #if (SimTK_DEFAULT_PRECISION == 1)
109 
110  typedef float SimTK_Real;
111 #elif (SimTK_DEFAULT_PRECISION == 2)
112 
113  typedef double SimTK_Real;
114 #elif (SimTK_DEFAULT_PRECISION == 4)
115 
116  typedef long double SimTK_Real;
117 #else
118  #error ILLEGAL VALUE FOR DEFAULT PRECISION
119 #endif
120 
121 #ifndef NDEBUG
122  #if defined(__cplusplus)
123  #include <cstdio>
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)
129  #else
130  #include <stdio.h>
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)
136  #endif
137 #else
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)
143 #endif
144 
145 /*
146  * Shared libraries are messy in Visual Studio. We have to distinguish three
147  * cases:
148  * (1) this header is being used to build the SimTKcommon shared library (dllexport)
149  * (2) this header is being used by a *client* of the SimTKcommon shared
150  * library (dllimport)
151  * (3) we are building the SimTKcommon static library, or the client is
152  * being compiled with the expectation of linking with the
153  * SimTKcommon static library (nothing special needed)
154  * In the CMake script for building this library, we define one of the symbols
155  * SimTK_SimTKCOMMON_BUILDING_{SHARED|STATIC}_LIBRARY
156  * Client code normally has no special symbol defined, in which case we'll
157  * assume it wants to use the shared library. However, if the client defines
158  * the symbol SimTK_USE_STATIC_LIBRARIES we'll suppress the dllimport so
159  * that the client code can be linked with static libraries. Note that
160  * the client symbol is not library dependent, while the library symbols
161  * affect only the SimTKcommon library, meaning that other libraries can
162  * be clients of this one. However, we are assuming all-static or all-shared.
163  */
164 
165 #ifdef _WIN32
166  #ifdef _MSC_VER
167  #pragma warning(disable:4231) /*need to use 'extern' template explicit instantiation*/
168  #pragma warning(disable:4251) /*no DLL interface for type of member of exported class*/
169  #pragma warning(disable:4275) /*no DLL interface for base class of exported class*/
170  #pragma warning(disable:4345) /*warning about PODs being default-initialized*/
171  #endif
172  #if defined(SimTK_SimTKCOMMON_BUILDING_SHARED_LIBRARY)
173  #define SimTK_SimTKCOMMON_EXPORT __declspec(dllexport)
174  /* Keep MS VC++ quiet when it tries to instantiate incomplete template classes in a DLL. */
175  #ifdef _MSC_VER
176  #pragma warning(disable:4661)
177  #endif
178  #elif defined(SimTK_SimTKCOMMON_BUILDING_STATIC_LIBRARY) || defined(SimTK_USE_STATIC_LIBRARIES)
179  #define SimTK_SimTKCOMMON_EXPORT
180  #else
181  #define SimTK_SimTKCOMMON_EXPORT __declspec(dllimport) /*i.e., a client of a shared library*/
182  #endif
183  /* VC++ tries to be secure by leaving bounds checking on for STL containers
184  * even in Release mode. This macro exists to disable that feature and can
185  * result in a considerable speedup.
186  * CAUTION: every linked-together compilation unit must have this set the same
187  * way. Everyone who properly includes this file first is fine; but as of this
188  * writing Simmath's IpOpt doesn't do so.
189  * NOTE: Microsoft corrected this problem with VC10 -- the feature is
190  * disabled by default in that compiler and later.
191  */
192  /* (sherm 081204 disabling for now: doesn't work on VC++ 8 and is
193  * tricky on VC++ 9 because all libraries, including 3rd party, must
194  * be built the same way). Better to use the SimTK::Array_<T> class in
195  * place of the std::vector<T> class to get better performance.
196  #ifdef NDEBUG
197  #undef _SECURE_SCL
198  #define _SECURE_SCL 0
199  #endif
200  */
201 #else
202  #define SimTK_SimTKCOMMON_EXPORT // Linux, Mac
203 #endif
204 
205 /* Every SimTK Core library must provide these two routines, with the library
206  * name appearing after the "version_" and "about_".
207  */
208 #if defined(__cplusplus)
209 extern "C" {
210 #endif
211 
212  SimTK_SimTKCOMMON_EXPORT void SimTK_version_SimTKcommon(int* major, int* minor, int* build);
219  SimTK_SimTKCOMMON_EXPORT void SimTK_about_SimTKcommon(const char* key, int maxlen, char* value);
220 #if defined(__cplusplus)
221 }
222 #endif
223 
224 /************************************/
225 /* END OF ANSI-C COMPATIBLE SECTION */
226 /************************************/
227 
228 #if defined(__cplusplus)
229 
230 #include <cstddef>
231 #include <cassert>
232 #include <cmath>
233 #include <cfloat>
234 #include <complex>
235 #include <limits>
236 #include <typeinfo>
237 #include <algorithm>
238 
239 /* Transition macros for C++11 support. VC10 and VC11 have partial support for
240 C++11, early VC's do not. Currently we're assuming no support from gcc. */
241 #ifndef SWIG
242  #if _MSC_VER>=1700 /* VC11 or higher */
243  #define OVERRIDE_11 override
244  #define FINAL_11 final
245  #elif _MSC_VER==1600 /* VC10 */
246  #define OVERRIDE_11 override
247  #define FINAL_11 sealed
248  #else /* gcc or earlier VC */
249  #define OVERRIDE_11
250  #define FINAL_11
251  #endif
252 #else /* Swigging */
253  #define OVERRIDE_11
254  #define FINAL_11
255 #endif
256 
257 
258 /* In Microsoft VC++ 11 (2012) and earlier these C99-compatible floating
259 point functions are missing. We'll create them here and install them into
260 namespace std. They were added in VC++ 12 (2013). */
261 #if defined(_MSC_VER) && (_MSC_VER <= 1700) // VC++ 12 (2013, _MSC_VER=1800) added these
262 namespace std {
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;}
277 }
278 #endif
279 
280 
281 namespace SimTK {
282 
283 
284 // This utility answers the question "if I put this integral value in an int and then
285 // get it back, will its value be the same?".
286 inline bool canStoreInInt(bool) {return true;}
287 inline bool canStoreInInt(char) {return true;}
288 inline bool canStoreInInt(unsigned char) {return true;}
289 inline bool canStoreInInt(signed char) {return true;}
290 inline bool canStoreInInt(short) {return true;}
291 inline bool canStoreInInt(unsigned short) {return true;}
292 inline bool canStoreInInt(int) {return true;}
293 inline bool canStoreInInt(unsigned int u) {return (unsigned int)(int(u)) == u;}
294 inline bool canStoreInInt(long i) {return long(int(i)) == i;}
295 inline bool canStoreInInt(unsigned long u) {return (unsigned long)(int(u)) == u;}
296 inline bool canStoreInInt(long long i) {return (long long)(int(i)) == i;}
297 inline bool canStoreInInt(unsigned long long u) {return (unsigned long long)(int(u)) == u;}
298 
299 // This utility answers the question "is this integral value a nonnegative number
300 // that can be stored in an int?".
301 inline bool canStoreInNonnegativeInt(bool) {return true;}
302 inline bool canStoreInNonnegativeInt(char c) {return c >= 0;}
303 inline bool canStoreInNonnegativeInt(unsigned char) {return true;}
304 inline bool canStoreInNonnegativeInt(signed char c) {return c >= 0;}
305 inline bool canStoreInNonnegativeInt(short s) {return s >= 0;}
306 inline bool canStoreInNonnegativeInt(unsigned short) {return true;}
307 inline bool canStoreInNonnegativeInt(int i) {return i >= 0;}
308 inline bool canStoreInNonnegativeInt(long l) {return canStoreInInt(l) && l >= 0;}
309 inline bool canStoreInNonnegativeInt(long long l) {return canStoreInInt(l) && l >= 0;}
310 inline bool canStoreInNonnegativeInt(unsigned int u) {return canStoreInInt(u);}
311 inline bool canStoreInNonnegativeInt(unsigned long u) {return canStoreInInt(u);}
312 inline bool canStoreInNonnegativeInt(unsigned long long u) {return canStoreInInt(u);}
313 
314 // This utility answers the question of whether an integer is suitable as a size
315 // limited by the given maximum size. Signed types must be checked for being
316 // nonegative; doing that with unsigned types leads to compiler warnings.
317 
318 // char can be signed or unsigned depending on the compiler; assume signed.
319 inline bool isSizeInRange(char sz, char mx){return 0<=sz&&sz<=mx;}
320 inline bool isSizeInRange(signed char sz, signed char mx){return 0<=sz&&sz<=mx;}
321 inline bool isSizeInRange(short sz, short mx){return 0<=sz&&sz<=mx;}
322 inline bool isSizeInRange(int sz, int mx){return 0<=sz&&sz<=mx;}
323 inline bool isSizeInRange(long sz, long 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;}
327 inline bool isSizeInRange(unsigned int sz, unsigned int 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;}
330 
331 // This utility answers the question of whether an integer is suitable as an index
332 // for an array limited by the given maximum size. Signed types must be checked for being
333 // nonegative; doing that with unsigned types leads to compiler warnings. This is just
334 // like the "size in range" check above except the maximum value allowed for an index
335 // is one less that the size.
336 
337 // char can be signed or unsigned depending on the compiler; assume signed.
338 inline bool isIndexInRange(char ix, char sz){return 0<=ix&&ix<sz;}
339 inline bool isIndexInRange(signed char ix, signed char sz){return 0<=ix&&ix<sz;}
340 inline bool isIndexInRange(short ix, short sz){return 0<=ix&&ix<sz;}
341 inline bool isIndexInRange(int ix, int sz){return 0<=ix&&ix<sz;}
342 inline bool isIndexInRange(long ix, long sz){return 0<=ix&&ix<sz;}
343 inline bool isIndexInRange(long long ix, long long sz){return 0<=ix&&ix<sz;}
344 inline bool isIndexInRange(unsigned char ix, unsigned char sz){return ix<sz;}
345 inline bool isIndexInRange(unsigned short ix, unsigned short sz){return ix<sz;}
346 inline bool isIndexInRange(unsigned int ix, unsigned int sz){return ix<sz;}
347 inline bool isIndexInRange(unsigned long ix, unsigned long sz){return ix<sz;}
348 inline bool isIndexInRange(unsigned long long ix, unsigned long long sz){return ix<sz;}
349 
350 // This utility answers the question: is this integral value nonnegative? The answer
351 // is always true for unsigned types and you'll get a warning from some compilers if
352 // you check.
353 
354 inline bool isNonnegative(bool) {return true;}
355 // char can be signed or unsigned depending on the compiler; assume signed.
356 inline bool isNonnegative(char n) {return n>=0;}
357 inline bool isNonnegative(signed char n) {return n>=0;}
358 inline bool isNonnegative(short n) {return n>=0;}
359 inline bool isNonnegative(int n) {return n>=0;}
360 inline bool isNonnegative(long n) {return n>=0;}
361 inline bool isNonnegative(long long n) {return n>=0;}
362 inline bool isNonnegative(unsigned char) {return true;}
363 inline bool isNonnegative(unsigned short) {return true;}
364 inline bool isNonnegative(unsigned int) {return true;}
365 inline bool isNonnegative(unsigned long) {return true;}
366 inline bool isNonnegative(unsigned long long){return true;}
367 
368 // A NaN-like value for unique index types created using the macro
369 // SimTK_DEFINE_UNIQUE_INDEX_TYPE(). A unique, typed constant with
370 // this numerical value is created for each index type.
371 static const int InvalidIndex = -1111111111;
372 }
373 
374 
375 
407 #define SimTK_DEFINE_UNIQUE_INDEX_TYPE(NAME) \
408  SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(,,,NAME) \
409  static const NAME Invalid ## NAME;
410 
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;
416 
418 #define SimTK_DEFINE_UNIQUE_LOCAL_INDEX_TYPE(PARENT,NAME) \
419  SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(,PARENT,::,NAME)
420 
423 #define SimTK_DEFINE_AND_EXPORT_UNIQUE_LOCAL_INDEX_TYPE(EXPORT,PARENT,SEP,NAME) \
424 class EXPORT NAME { \
425  int ix; \
426 public: \
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;} \
436  \
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);} \
448  \
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);} \
461  \
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);} \
474  \
475  const NAME& operator++() {assert(isValid()); ++ix; return *this;} /*prefix */ \
476  NAME operator++(int) {assert(isValid()); ++ix; return NAME(ix-1);} /*postfix*/ \
477  const NAME& operator--() {assert(isValid()); --ix; return *this;} /*prefix */ \
478  NAME operator--(int) {assert(isValid()); --ix; return NAME(ix+1);} /*postfix*/ \
479  NAME next() const {assert(isValid()); return NAME(ix+1);} \
480  NAME prev() const {assert(isValid()); return NAME(ix-1);} /*might return -1*/ \
481  \
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;} \
494  \
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;} \
505  /* IndexTraits for use in Array_<T,X> with this as X; same as int */ \
506  typedef int size_type; \
507  typedef int difference_type; \
508  static size_type max_size() {return std::numeric_limits<int>::max();} \
509 };
510 
517 #ifndef NDEBUG
518  #define SimTK_DYNAMIC_CAST_DEBUG dynamic_cast // safe but slow
519 #else
520  #define SimTK_DYNAMIC_CAST_DEBUG static_cast // unsafe but fast
521 #endif
522 
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); }
535 
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)); }
547 
548 
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&)
556 
557 namespace SimTK {
558 
561 namespace Exception { }
562 
565 typedef SimTK_Real Real;
568 typedef std::complex<Real> Complex;
570 typedef std::complex<float> fComplex;
572 typedef std::complex<float> dComplex;
573 
574 
575 // Forward declaration giving template defaults must come before any
576 // other declarations.
577 template <int M, class E=Real, int STRIDE=1> class Vec;
578 template <int N, class E=Real, int STRIDE=1> class Row;
579 template <int M, int N, class E=Real, int CS=M, int RS=1> class Mat; // col & row spacing
580 template <int M, class E=Real, int RS=1> class SymMat;
581 
582 
585 struct Segment {
586  Segment() : length(0), offset(0) { }
587  explicit Segment(int l, int ofs=0) : length(l), offset(ofs) {
588  assert(l>=0 && ofs>=0);
589  }
590  // default copy, assignment, destructor
591  int length;
592  int offset;
593 };
594 
595 
601 struct DontCopy {};
605 struct TrustMe {};
606 
609 struct FalseType {};
612 struct TrueType {};
613 
615 template <class L, class R> struct AndOpType {};
616 template<> struct AndOpType<FalseType,FalseType> {typedef FalseType Result;};
617 template<> struct AndOpType<FalseType,TrueType> {typedef FalseType Result;};
618 template<> struct AndOpType<TrueType, FalseType> {typedef FalseType Result;};
619 template<> struct AndOpType<TrueType, TrueType> {typedef TrueType Result;};
620 
622 template <class L, class R> struct OrOpType {};
623 template<> struct OrOpType<FalseType,FalseType> {typedef FalseType Result;};
624 template<> struct OrOpType<FalseType,TrueType> {typedef TrueType Result;};
625 template<> struct OrOpType<TrueType, FalseType> {typedef TrueType Result;};
626 template<> struct OrOpType<TrueType, TrueType> {typedef TrueType Result;};
627 
629 template <class L, class R> struct XorOpType {};
630 template<> struct XorOpType<FalseType,FalseType> {typedef FalseType Result;};
631 template<> struct XorOpType<FalseType,TrueType> {typedef TrueType Result;};
632 template<> struct XorOpType<TrueType, FalseType> {typedef TrueType Result;};
633 template<> struct XorOpType<TrueType, TrueType> {typedef FalseType Result;};
634 
636 template <class T> struct IsIntegralType {
639  typedef FalseType Result;
642  static const bool result = false;
643 };
646 #define SimTK_SPECIALIZE_INTEGRAL_TYPE(T) \
647  template<> struct IsIntegralType<T> \
648  {typedef TrueType Result; static const bool result = true;}
649 
650 SimTK_SPECIALIZE_INTEGRAL_TYPE(bool);
651 SimTK_SPECIALIZE_INTEGRAL_TYPE(char);
652 // This causes problems when used with Qt which for some crazy
653 // reason likes to make its own wchar_t rather than using the built in.
654 // SimTK_SPECIALIZE_INTEGRAL_TYPE(wchar_t);
655 SimTK_SPECIALIZE_INTEGRAL_TYPE(signed char);
656 SimTK_SPECIALIZE_INTEGRAL_TYPE(unsigned char);
657 SimTK_SPECIALIZE_INTEGRAL_TYPE(short);
658 SimTK_SPECIALIZE_INTEGRAL_TYPE(unsigned short);
659 SimTK_SPECIALIZE_INTEGRAL_TYPE(int);
660 SimTK_SPECIALIZE_INTEGRAL_TYPE(unsigned int); // a.k.a. "unsigned"
661 SimTK_SPECIALIZE_INTEGRAL_TYPE(long);
662 SimTK_SPECIALIZE_INTEGRAL_TYPE(unsigned long);
663 SimTK_SPECIALIZE_INTEGRAL_TYPE(long long);
664 SimTK_SPECIALIZE_INTEGRAL_TYPE(unsigned long long);
665 
667 template <class T> struct IsFloatingType {
670  typedef FalseType Result;
673  static const bool result = false;
674 };
677 #define SimTK_SPECIALIZE_FLOATING_TYPE(T) \
678  template<> struct IsFloatingType<T> \
679  {typedef TrueType Result; static const bool result = true;}
680 
681 SimTK_SPECIALIZE_FLOATING_TYPE(float);
682 SimTK_SPECIALIZE_FLOATING_TYPE(double);
683 SimTK_SPECIALIZE_FLOATING_TYPE(long double);
684 
686 template <class T> struct IsVoidType {
689  typedef FalseType Result;
692  static const bool result = false;
693 };
694 template<> struct IsVoidType<void>
695 {typedef TrueType Result; static const bool result = true;};
696 
699 template <class T> struct IsArithmeticType {
702  typedef OrOpType<typename IsIntegralType<T>::Result,
703  typename IsFloatingType<T>::Result> Result;
706  static const bool result = IsIntegralType<T>::result
707  || IsFloatingType<T>::result;
708 };
709 
710 // This struct's sole use is to allow us to define the typedef
711 // Is64BitPlatformType as equivalent to either TrueType or FalseType.
712 template <bool is64Bit> struct Is64BitHelper {};
713 template<> struct Is64BitHelper<true>
714 {typedef TrueType Result; static const bool result = true;};
715 template<> struct Is64BitHelper<false>
716 {typedef FalseType Result; static const bool result = false;};
717 
722 static const bool Is64BitPlatform = sizeof(size_t) > sizeof(int);
723 typedef Is64BitHelper<Is64BitPlatform>::Result Is64BitPlatformType;
724 
725 
729 template <class T> struct NiceTypeName {
730  static const char* name() {return typeid(T).name();}
731 };
732 
736 #define SimTK_NICETYPENAME_LITERAL(T) \
737 template <> struct NiceTypeName< T > { \
738  static const char* name() { return #T; } \
739 };
740 
741 // Some types for which we'd like to see nice type names.
742 SimTK_NICETYPENAME_LITERAL(bool);
743 SimTK_NICETYPENAME_LITERAL(char);
744 // This causes problems when used with Qt which for some crazy
745 // reason likes to make its own wchar_t rather than using the built in.
746 // SimTK_NICETYPENAME_LITERAL(wchar_t);
747 SimTK_NICETYPENAME_LITERAL(signed char);
748 SimTK_NICETYPENAME_LITERAL(unsigned char);
749 SimTK_NICETYPENAME_LITERAL(short);
750 SimTK_NICETYPENAME_LITERAL(unsigned short);
751 SimTK_NICETYPENAME_LITERAL(int);
752 SimTK_NICETYPENAME_LITERAL(unsigned); // preferred to "unsigned int"
753 SimTK_NICETYPENAME_LITERAL(long);
754 SimTK_NICETYPENAME_LITERAL(unsigned long);
755 SimTK_NICETYPENAME_LITERAL(long long);
756 SimTK_NICETYPENAME_LITERAL(unsigned long long);
757 SimTK_NICETYPENAME_LITERAL(float);
758 SimTK_NICETYPENAME_LITERAL(double);
759 SimTK_NICETYPENAME_LITERAL(long double);
760 SimTK_NICETYPENAME_LITERAL(std::string);
761 SimTK_NICETYPENAME_LITERAL(std::complex<float>);
762 SimTK_NICETYPENAME_LITERAL(std::complex<double>);
763 SimTK_NICETYPENAME_LITERAL(std::complex<long double>);
764 SimTK_NICETYPENAME_LITERAL(SimTK::FalseType);
765 SimTK_NICETYPENAME_LITERAL(SimTK::TrueType);
766 
767 } // namespace SimTK
768 
769 #endif /* C++ stuff */
770 
771 #endif /* SimTK_SimTKCOMMON_COMMON_H_ */
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:202
double SimTK_Real
This type is for use in C; in C++ use SimTK::Real instead.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:113
void SimTK_about_SimTKcommon(const char *key, int maxlen, char *value)
Obtain "about" information for the currently-loaded SimTKcommon library.
void SimTK_version_SimTKcommon(int *major, int *minor, int *build)
Obtain version information for the currently-loaded SimTKcommon library.
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120