Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef CLASP_PLATFORM_H_INCLUDED
00022 #define CLASP_PLATFORM_H_INCLUDED
00023 #ifdef _MSC_VER
00024 #pragma once
00025 #endif
00026
00027 #define STRING2(x) #x
00028 #define STRING(x) STRING2(x)
00029
00030 #if defined(_MSC_VER) && _MSC_VER >= 1200
00031 #define CLASP_PRAGMA_TODO(X) __pragma(message(__FILE__ "(" STRING(__LINE__) ") : TODO: " X))
00032 #define FUNC_NAME __FUNCTION__
00033 #include <basetsd.h>
00034 #if _MSC_VER >= 1600
00035 #include <stdint.h>
00036 #endif
00037 typedef UINT8 uint8;
00038 typedef UINT16 uint16;
00039 typedef INT32 int32;
00040 typedef UINT32 uint32;
00041 typedef UINT64 uint64;
00042 typedef INT64 int64;
00043 typedef UINT_PTR uintp;
00044 typedef INT16 int16;
00045 #define PRIu64 "llu"
00046 #define PRId64 "lld"
00047 template <unsigned> struct Uint_t;
00048 template <> struct Uint_t<sizeof(uint8)> { typedef uint8 type; };
00049 template <> struct Uint_t<sizeof(uint16)> { typedef uint16 type; };
00050 template <> struct Uint_t<sizeof(uint32)> { typedef uint32 type; };
00051 template <> struct Uint_t<sizeof(uint64)> { typedef uint64 type; };
00052 #define BIT_MASK(x,n) ( static_cast<Uint_t<sizeof((x))>::type>(1) << (n) )
00053 #elif defined(__GNUC__) && __GNUC__ >= 3
00054 #define FUNC_NAME __PRETTY_FUNCTION__
00055 #if !defined(__STDC_FORMAT_MACROS)
00056 #define __STDC_FORMAT_MACROS
00057 #endif
00058 #include <inttypes.h>
00059 typedef uint8_t uint8;
00060 typedef uint16_t uint16;
00061 typedef int16_t int16;
00062 typedef int32_t int32;
00063 typedef uint32_t uint32;
00064 typedef uint64_t uint64;
00065 typedef int64_t int64;
00066 typedef uintptr_t uintp;
00067 #define BIT_MASK(x,n) ( static_cast<__typeof((x))>(1)<<(n) )
00068 #define APPLY_PRAGMA(x) _Pragma (#x)
00069 #define CLASP_PRAGMA_TODO(x) APPLY_PRAGMA(message ("TODO: " #x))
00070 #else
00071 #error unknown compiler or platform. Please add typedefs manually.
00072 #endif
00073 #ifndef UINT32_MAX
00074 #define UINT32_MAX (~uint32(0))
00075 #endif
00076 #ifndef UINT64_MAX
00077 #define UINT64_MAX (~uint64(0))
00078 #endif
00079 #ifndef INT64_MAX
00080 #define INT64_MAX ((int64)(UINT64_MAX >> 1))
00081 #endif
00082 #ifndef UINTP_MAX
00083 #define UINTP_MAX (~uintp(0))
00084 #endif
00085 #ifndef INT16_MAX
00086 #define INT16_MAX (0x7fff)
00087 #endif
00088 #ifndef INT16_MIN
00089 #define INT16_MIN (-INT16_MAX - 1)
00090 #endif
00091 #ifndef FUNC_NAME
00092 #define FUNC_NAME __FILE__
00093 #endif
00094
00095
00096 #define set_bit(x,n) ( (x) | BIT_MASK((x),(n)) )
00097 #define clear_bit(x,n) ( (x) & ~BIT_MASK((x),(n)) )
00098 #define toggle_bit(x,n)( (x) ^ BIT_MASK((x),(n)) )
00099
00100
00101 #define store_set_bit(x,n) ( (x) |= BIT_MASK((x),(n)) )
00102 #define store_clear_bit(x,n) ( (x) &= ~BIT_MASK((x),(n)) )
00103 #define store_toggle_bit(x,n)( (x) ^= BIT_MASK((x),(n)) )
00104
00105
00106 #define test_bit(x,n) ( ((x) & BIT_MASK((x),(n))) != 0 )
00107
00108 #define right_most_bit(x) ( (x) & (-(x)) )
00109
00110 template <class T>
00111 bool aligned(void* mem) {
00112 uintp x = reinterpret_cast<uintp>(mem);
00113 #if (_MSC_VER >= 1300)
00114 return (x & (__alignof(T)-1)) == 0;
00115 #elif defined(__GNUC__)
00116 return (x & (__alignof__(T)-1)) == 0;
00117 #else
00118 struct AL { char x; T y; };
00119 return (x & (sizeof(AL)-sizeof(T))) == 0;
00120 #endif
00121 }
00122
00123 template <bool> struct static_assertion;
00124 template <> struct static_assertion<true> {};
00125
00126 #if !defined(__cplusplus) || (__cplusplus < 201103L && !defined(static_assert))
00127 #define static_assert(x, message) (void)sizeof(static_assertion< (x) >)
00128 #endif
00129
00130 extern const char* clasp_format_error(const char* m, ...);
00131 extern const char* clasp_format(char* buf, unsigned size, const char* m, ...);
00132
00133 #define CLASP_FAIL_IF(exp, fmt, ...) \
00134 (void)( (!(exp)) || (throw std::logic_error(clasp_format_error(fmt, ##__VA_ARGS__ )), 0))
00135
00136 #ifndef CLASP_NO_ASSERT_CONTRACT
00137
00138 #define CLASP_ASSERT_CONTRACT_MSG(exp, msg) \
00139 (void)( (!!(exp)) || (throw std::logic_error(clasp_format_error("%s@%d: contract violated: %s", FUNC_NAME, __LINE__, (msg))), 0))
00140
00141 #else
00142 #include <cassert>
00143 #define CLASP_ASSERT_CONTRACT_MSG(exp, msg) assert((exp) && ((msg),true))
00144 #endif
00145
00146 #define CLASP_ASSERT_CONTRACT(exp) CLASP_ASSERT_CONTRACT_MSG(exp, #exp)
00147
00148 #if !defined(CLASP_ENABLE_PRAGMA_TODO) || CLASP_ENABLE_PRAGMA_TODO==0
00149 #undef CLASP_PRAGMA_TODO
00150 #define CLASP_PRAGMA_TODO(X)
00151 #endif
00152
00153 #endif