TypeFuncs.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_TYPEFUNCS_H
00014 #define _SUPPORT_TYPEFUNCS_H
00015 
00021 #include <support/SupportDefs.h>
00022 
00023 #include <string.h>
00024 #include <new>
00025 
00026 #if _SUPPORTS_NAMESPACE
00027 namespace palmos {
00028 namespace support {
00029 #endif
00030 
00035 /*--------------------------------------------------------*/
00036 /*----- Basic operations on types ------------------------*/
00037 
00040 
00041 template<class TYPE>
00042 inline void BConstruct(TYPE* base)
00043 {
00044     new(base) TYPE;
00045 }
00046 
00047 template<class TYPE>
00048 inline void BDestroy(TYPE* base)
00049 {
00050     base->~TYPE();
00051 }
00052 
00053 template<class TYPE>
00054 inline void BCopy(TYPE* to, const TYPE* from)
00055 {
00056     new(to) TYPE(*from);
00057 }
00058 
00059 template<class TYPE>
00060 inline void BReplicate(TYPE* to, const TYPE* protoElement)
00061 {
00062     new(to) TYPE(*protoElement);
00063 }
00064 
00065 template<class TYPE>
00066 inline void BMoveBefore(TYPE* to, TYPE* from)
00067 {
00068     new(to) TYPE(*from);
00069     from->~TYPE();
00070 }
00071 
00072 template<class TYPE>
00073 inline void BMoveAfter(TYPE* to, TYPE* from)
00074 {
00075     new(to) TYPE(*from);
00076     from->~TYPE();
00077 }
00078 
00079 template<class TYPE>
00080 inline void BAssign(TYPE* to, const TYPE* from)
00081 {
00082     *to = *from;
00083 }
00084 
00085 
00086 
00087 template<class TYPE>
00088 inline void BConstruct(TYPE* base, size_t count)
00089 {
00090     while (--count != (size_t)-1) {
00091         new(base) TYPE;
00092         base++;
00093     }
00094 }
00095 
00096 template<class TYPE>
00097 inline void BDestroy(TYPE* base, size_t count)
00098 {
00099     while (--count != (size_t)-1) {
00100         base->~TYPE();
00101         base++;
00102     }
00103 }
00104 
00105 template<class TYPE>
00106 inline void BCopy(TYPE* to, const TYPE* from, size_t count)
00107 {
00108     while (--count != (size_t)-1) {
00109         new(to) TYPE(*from);
00110         to++;
00111         from++;
00112     }
00113 }
00114 
00115 
00116 template<class TYPE>
00117 inline void BReplicate(TYPE* to, const TYPE* protoElement, size_t count)
00118 {
00119     while (--count != (size_t)-1) {
00120         new(to) TYPE(*protoElement);
00121         to++;
00122     }
00123 }
00124 
00125 template<class TYPE>
00126 inline void BMoveBefore(    TYPE* to, TYPE* from, size_t count)
00127 {
00128     while (--count != (size_t)-1) {
00129         new(to) TYPE(*from);
00130         from->~TYPE();
00131         to++;
00132         from++;
00133     }
00134 }
00135 
00136 template<class TYPE>
00137 inline void BMoveAfter( TYPE* to, TYPE* from, size_t count)
00138 {
00139     to+=(count-1);
00140     from+=(count-1);
00141     while (--count != (size_t)-1) {
00142         new(to) TYPE(*from);
00143         from->~TYPE();
00144         to--;
00145         from--;
00146     }
00147 }
00148 
00149 template<class TYPE>
00150 inline void BAssign(    TYPE* to, const TYPE* from, size_t count)
00151 {
00152     while (--count != (size_t)-1) {
00153         *to = *from;
00154         to++;
00155         from++;
00156     }
00157 }
00158 
00159 template<class TYPE>
00160 inline void BSwap(TYPE& v1, TYPE& v2)
00161 {
00162     TYPE tmp(v1); v1 = v2; v2 = tmp;
00163 }
00164 
00165 template<class TYPE>
00166 inline int32_t BCompare(const TYPE& v1, const TYPE& v2)
00167 {
00168     return (v1 < v2) ? -1 : ( (v2 < v1) ? 1 : 0 );
00169 }
00170 
00171 template<class TYPE>
00172 inline bool BLessThan(const TYPE& v1, const TYPE& v2)
00173 {
00174     return v1 < v2;
00175 }
00176 
00178 
00181 #if !defined(_MSC_VER)
00182 
00183 /*--------------------------------------------------------*/
00184 /*----- Optimizations for all pointer types --------------*/
00185 
00186 template<class TYPE>
00187 inline void BConstruct(TYPE** base, size_t count = 1)
00188     { (void)base; (void)count; }
00189 template<class TYPE>
00190 inline void BDestroy(TYPE** base, size_t count = 1)
00191     { (void)base; (void)count; }
00192 template<class TYPE>
00193 inline void BCopy(TYPE** to, TYPE* const * from, size_t count = 1)
00194     { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE*)*count); }
00195 template<class TYPE>
00196 inline void BMoveBefore(TYPE** to, TYPE** from, size_t count = 1)
00197     { if (count == 1) *to = *from; else memmove(to, from, sizeof(TYPE*)*count); }
00198 template<class TYPE>
00199 inline void BMoveAfter(TYPE** to, TYPE** from, size_t count = 1)
00200     { if (count == 1) *to = *from; else memmove(to, from, sizeof(TYPE*)*count); }
00201 template<class TYPE>
00202 inline void BAssign(TYPE** to, TYPE* const * from, size_t count = 1)
00203     { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE*)*count); }
00204 
00205 #endif
00206 
00207 /*--------------------------------------------------------*/
00208 /*----- Optimizations for basic data types ---------------*/
00209 
00210 // Standard optimizations for types that don't contain internal or
00211 // other pointers to themselves.
00212 #define B_IMPLEMENT_SIMPLE_TYPE_FUNCS(TYPE)                                             \
00213 inline void BMoveBefore(TYPE* to, TYPE* from, size_t count)                         \
00214     { memmove(to, from, sizeof(TYPE)*count); }                                          \
00215 inline void BMoveAfter(TYPE* to, TYPE* from, size_t count)                              \
00216     { memmove(to, from, sizeof(TYPE)*count); }                                          \
00217 
00218 // Extreme optimizations for types whose constructor and destructor
00219 // don't need to be called.
00220 #define B_IMPLEMENT_BASIC_TYPE_FUNCS(TYPE)                                              \
00221 inline void BConstruct(TYPE* base, size_t count)                                        \
00222     { (void)base; (void)count; }                                                        \
00223 inline void BDestroy(TYPE* base, size_t count)                                          \
00224     { (void)base; (void)count; }                                                        \
00225 inline void BCopy(TYPE* to, const TYPE* from, size_t count)                         \
00226     { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE)*count); }     \
00227 inline void BReplicate(TYPE* to, const TYPE* protoElement, size_t count)                \
00228     { while (--count != (size_t)-1) { *to = *protoElement; to++; } }                    \
00229 inline void BMoveBefore(TYPE* to, TYPE* from, size_t count)                         \
00230     { if (count == 1) *to = *from; else memmove(to, from, sizeof(TYPE)*count); }        \
00231 inline void BMoveAfter(TYPE* to, TYPE* from, size_t count)                              \
00232     { if (count == 1) *to = *from; else memmove(to, from, sizeof(TYPE)*count); }        \
00233 inline void BAssign(TYPE* to, const TYPE* from, size_t count)                           \
00234     { if (count == 1) *to = *from; else memcpy(to, from, sizeof(TYPE)*count); }     \
00235 
00236 B_IMPLEMENT_BASIC_TYPE_FUNCS(bool)
00237 B_IMPLEMENT_BASIC_TYPE_FUNCS(int8_t)
00238 B_IMPLEMENT_BASIC_TYPE_FUNCS(uint8_t)
00239 B_IMPLEMENT_BASIC_TYPE_FUNCS(int16_t)
00240 B_IMPLEMENT_BASIC_TYPE_FUNCS(uint16_t)
00241 B_IMPLEMENT_BASIC_TYPE_FUNCS(int32_t)
00242 B_IMPLEMENT_BASIC_TYPE_FUNCS(uint32_t)
00243 B_IMPLEMENT_BASIC_TYPE_FUNCS(int64_t)
00244 B_IMPLEMENT_BASIC_TYPE_FUNCS(uint64_t)
00245 B_IMPLEMENT_BASIC_TYPE_FUNCS(float)
00246 B_IMPLEMENT_BASIC_TYPE_FUNCS(double)
00247 
00248 #if _SUPPORTS_NAMESPACE
00249 } } // namespace palmos::support
00250 #endif
00251 
00252 #endif