|
| 1 | +#ifndef IU_CORE_UTILS_CORETYPES_H |
| 2 | +#define IU_CORE_UTILS_CORETYPES_H |
| 3 | + |
| 4 | +#include <string> |
| 5 | +#define __STDC_CONSTANT_MACROS |
| 6 | +#include <stdint.h> |
| 7 | +//#include "Core/3rdpart/pstdint.h" |
| 8 | +#include <memory> |
| 9 | +typedef std::string Utf8String; |
| 10 | +#if _MSC_VER && (_MSC_VER < 1800) |
| 11 | +namespace std_tr = std::tr1; |
| 12 | +#else |
| 13 | +namespace std_tr = std; |
| 14 | +#endif |
| 15 | + |
| 16 | +/*#ifdef _MSC_VER |
| 17 | + typedef __int64 int64_t; |
| 18 | + typedef unsigned __int64 zuint64; |
| 19 | +#else |
| 20 | + typedef long long int64_t; |
| 21 | + typedef unsigned long long zuint64; |
| 22 | +#endif*/ |
| 23 | + |
| 24 | +// A macro to disallow the copy constructor and operator= functions |
| 25 | +// This should be used in the private: declarations for a class |
| 26 | +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 27 | + TypeName(const TypeName&); \ |
| 28 | + void operator=(const TypeName&) |
| 29 | + |
| 30 | + |
| 31 | +// The ARRAY_SIZE(arr) macro returns the # of elements in an array arr. |
| 32 | +// The expression is a compile-time constant, and therefore can be |
| 33 | +// used in defining new arrays, for example. If you use arraysize on |
| 34 | +// a pointer by mistake, you will get a compile-time error. |
| 35 | +// |
| 36 | +// One caveat is that ARRAY_SIZE() doesn't accept any array of an |
| 37 | +// anonymous type or a type defined inside a function. In these rare |
| 38 | +// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is |
| 39 | +// due to a limitation in C++'s template system. The limitation might |
| 40 | +// eventually be removed, but it hasn't happened yet. |
| 41 | + |
| 42 | +// This template function declaration is used in defining ARRAY_SIZE. |
| 43 | +// Note that the function doesn't need an implementation, as we only |
| 44 | +// use its type. |
| 45 | +template <typename T, size_t N> |
| 46 | +char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 47 | + |
| 48 | +// That gcc wants both of these prototypes seems mysterious. VC, for |
| 49 | +// its part, can't decide which to use (another mystery). Matching of |
| 50 | +// template overloads: the final frontier. |
| 51 | +#ifndef _MSC_VER |
| 52 | +template <typename T, size_t N> |
| 53 | +char (&ArraySizeHelper(const T (&array)[N]))[N]; |
| 54 | +#endif |
| 55 | + |
| 56 | +#define ARRAY_SIZE(array) (sizeof(ArraySizeHelper(array))) |
| 57 | + |
| 58 | +template <class T> struct EnumWrapper |
| 59 | +{ |
| 60 | + T value_; |
| 61 | + operator T&() |
| 62 | + { |
| 63 | + return value_; |
| 64 | + } |
| 65 | + |
| 66 | + T& operator =(const T& value) |
| 67 | + { |
| 68 | + value_ = value; |
| 69 | + return *this; |
| 70 | + } |
| 71 | + |
| 72 | + bool operator==(const T value) |
| 73 | + { |
| 74 | + return value_ == value; |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +// std::shared_ptr release() implementation |
| 79 | +// thx to http://stackoverflow.com/questions/1833356/detach-a-pointer-from-a-shared-ptr/5995770#5995770 |
| 80 | +// |
| 81 | +template <typename T> |
| 82 | +class release_deleter{ |
| 83 | +public: |
| 84 | + release_deleter() : released_(new bool(false)){} |
| 85 | + void release() {*released_ = true;} |
| 86 | + void reset_released() { *released_ = false;} |
| 87 | + void operator()(T* ptr){ |
| 88 | + if(!*released_) { |
| 89 | + delete ptr; |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +private: |
| 95 | + //DISALLOW_COPY_AND_ASSIGN(release_deleter<T>); |
| 96 | + std_tr::shared_ptr<bool> released_; |
| 97 | +}; |
| 98 | +#endif |
0 commit comments