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
00029
00030
00031
00032
00033
00034
00035 #ifndef _SUPPORT_TYPEFUNCS_H
00036 #define _SUPPORT_TYPEFUNCS_H
00037
00038
00039
00040 #include <memory.h>
00041 #include <new>
00042 #include <stdint.h>
00043
00044 namespace alp {
00045
00046
00047
00048
00049 template<class TYPE>
00050 static inline void Construct(TYPE* base, size_t count = 1)
00051 {
00052 while (--count != (size_t)-1) {
00053 new(base) TYPE();
00054 base++;
00055 }
00056 }
00057
00058 template<class TYPE>
00059 static inline void Destroy(TYPE* base, size_t count = 1)
00060 {
00061 while (--count != (size_t)-1) {
00062 base->~TYPE();
00063 base++;
00064 }
00065 }
00066
00067 template<class TYPE>
00068 static inline void Copy(TYPE* to, const TYPE* from, size_t count = 1)
00069 {
00070 while (--count != (size_t)-1) {
00071 new(to) TYPE(*from);
00072 to++;
00073 from++;
00074 }
00075 }
00076
00077 template<class TYPE>
00078 static inline void MoveBefore( TYPE* to, TYPE* from, size_t count = 1)
00079 {
00080 while (--count != (size_t)-1) {
00081 new(to) TYPE(*from);
00082 from->~TYPE();
00083 to++;
00084 from++;
00085 }
00086 }
00087
00088 template<class TYPE>
00089 static inline void MoveAfter( TYPE* to, TYPE* from, size_t count = 1)
00090 {
00091 to+=(count-1);
00092 from+=(count-1);
00093 while (--count != (size_t)-1) {
00094 new(to) TYPE(*from);
00095 from->~TYPE();
00096 to--;
00097 from--;
00098 }
00099 }
00100
00101 template<class TYPE>
00102 static inline void Assign( TYPE* to, const TYPE* from, size_t count = 1)
00103 {
00104 while (--count != (size_t)-1) {
00105 *to = *from;
00106 to++;
00107 from++;
00108 }
00109 }
00110
00111 template<class TYPE>
00112 static inline void Swap(TYPE& v1, TYPE& v2)
00113 {
00114 TYPE tmp(v1); v1 = v2; v2 = tmp;
00115 }
00116
00117 template<class TYPE>
00118 static inline int32_t Compare(const TYPE& v1, const TYPE& v2)
00119 {
00120 return (v1 < v2) ? -1 : ( (v2 < v1) ? 1 : 0 );
00121 }
00122
00123 template<class TYPE>
00124 static inline bool LessThan(const TYPE& v1, const TYPE& v2)
00125 {
00126 return v1 < v2;
00127 }
00128
00129
00130
00131
00132 template<class TYPE>
00133 static inline void Construct(TYPE** base, size_t count = 1)
00134 { (void)base; (void)count; }
00135 template<class TYPE>
00136 static inline void Destroy(TYPE** base, size_t count = 1)
00137 { (void)base; (void)count; }
00138 template<class TYPE>
00139 static inline void Copy(TYPE** to, TYPE* const * from, size_t count = 1)
00140 { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE*)*count); }
00141 template<class TYPE>
00142 static inline void MoveBefore(TYPE** to, TYPE** from, size_t count = 1)
00143 { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE*)*count); }
00144 template<class TYPE>
00145 static inline void MoveAfter(TYPE** to, TYPE** from, size_t count = 1)
00146 { if (count == 1) *to = *from; else memmove(to, from, sizeof(TYPE*)*count); }
00147 template<class TYPE>
00148 static inline void Assign(TYPE** to, TYPE* const * from, size_t count = 1)
00149 { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE*)*count); }
00150
00151
00152
00153
00154
00155
00156 #define ALP_IMPLEMENT_SIMPLE_TYPE_FUNCS(TYPE) \
00157 static inline void MoveBefore(TYPE* to, TYPE* from, size_t count = 1) \
00158 { memcpy(to, from, (sizeof(TYPE[2])/2)*count); } \
00159 static inline void MoveAfter(TYPE* to, TYPE* from, size_t count = 1) \
00160 { memmove(to, from, (sizeof(TYPE[2])/2)*count); } \
00161
00162
00163
00164 #define ALP_IMPLEMENT_BASIC_TYPE_FUNCS(TYPE) \
00165 static inline void Construct(TYPE* base, size_t count) \
00166 { (void)base; (void)count; } \
00167 static inline void Destroy(TYPE* base, size_t count) \
00168 { (void)base; (void)count; } \
00169 static inline void Copy(TYPE* to, const TYPE* from, size_t count = 1) \
00170 { if (count == 1) *to = *from; else memcpy(to, from, (sizeof(TYPE[2])/2)*count); } \
00171 static inline void MoveBefore(TYPE* to, TYPE* from, size_t count = 1) \
00172 { if (count == 1) *to = *from; else memcpy(to, from, (sizeof(TYPE[2])/2)*count); } \
00173 static inline void MoveAfter(TYPE* to, TYPE* from, size_t count = 1) \
00174 { if (count == 1) *to = *from; else memmove(to, from, (sizeof(TYPE[2])/2)*count); } \
00175 static inline void Assign(TYPE* to, const TYPE* from, size_t count = 1) \
00176 { if (count == 1) *to = *from; else memcpy(to, from, (sizeof(TYPE[2])/2)*count); } \
00177
00178 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(bool)
00179 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(int8_t)
00180 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(uint8_t)
00181 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(int16_t)
00182 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(uint16_t)
00183 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(int32_t)
00184 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(uint32_t)
00185 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(int64_t)
00186 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(uint64_t)
00187 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(float)
00188 ALP_IMPLEMENT_BASIC_TYPE_FUNCS(double)
00189
00190 }
00191
00192 #endif