Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

TypeFuncs.h

Go to the documentation of this file.
00001 /********************************************************************
00002 
00003 Copyright 2006, ACCESS Systems Americas, Inc. All rights reserved.
00004 
00005 The contents of this file are subject to the Mozilla Public License Version
00006 1.1 (the "License"); you may not use this file except in compliance with
00007 the License. You may obtain a copy of the License at
00008 http://www.mozilla.org/MPL/
00009 
00010 Software distributed under the License is distributed on an "AS IS" basis,
00011 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012 for the specific language governing rights and limitations under the
00013 License.
00014 
00015 The Original Code is the entire contents of this file.
00016 
00017 The Initial Developer of the Original Code is ACCESS Systems Americas, Inc.
00018 
00019 Portions created by ACCESS Systems Americas, Inc. are Copyright © 2006. All
00020 Rights Reserved.
00021 
00022 Contributor(s): none.
00023 
00024 ********************************************************************/
00025 /***************************************************************************
00026 //
00027 //      File:                   support/TypeFuncs.h
00028 //
00029 //      Description:    Templatized functions for various type operations.
00030 //
00031 //      Copyright 2001, Be Incorporated, All Rights Reserved.
00032 //
00033 ***************************************************************************/
00034 
00035 #ifndef _SUPPORT_TYPEFUNCS_H
00036 #define _SUPPORT_TYPEFUNCS_H
00037 
00038 //#include <support/SupportDefs.h>
00039 
00040 #include <memory.h>
00041 #include <new>
00042 #include <stdint.h>
00043 
00044 namespace alp {
00045 
00046 /*--------------------------------------------------------*/
00047 /*----- Basic operations on types ------------------------*/
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 /*----- Optimizations for all pointer types --------------*/
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 /*----- Optimizations for basic data types ---------------*/
00153 
00154 // Standard optimizations for types that don't contain internal or
00155 // other pointers to themselves.
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 // Extreme optimizations for types whose constructor and destructor
00163 // don't need to be called.
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 }       // namespace alp
00191 
00192 #endif

Generated on Sat Dec 16 20:29:47 2006 for hiker-0.9 by  doxygen 1.4.4