Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXElement.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                           Generic Element Handling                            *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2010 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXELEMENT_H
00022 #define FXELEMENT_H
00023 
00024 namespace FX {
00025 
00026 /****************************  D e f i n i t i o n  ****************************/
00027 
00028 // Generic implementations for generic objects
00029 
00030 
00031 /// Construct some elements at a location
00032 template<class EType>
00033 inline void constructElms(EType* ptr,unsigned long n){
00034   while(n--){ ::new ((void*)ptr) EType; ptr++; }
00035   }
00036 
00037 
00038 /// Destruct some elements at a location
00039 template<class EType>
00040 inline void destructElms(EType* ptr,unsigned long n){
00041   while(n--){ ptr->~EType(); ptr++; }
00042   }
00043 
00044 
00045 /// Copy some elements from one place to another
00046 template<class EType, class OType>
00047 inline void copyElms(EType* dst,const OType* src,unsigned long n){
00048   while(n--){ *dst++ = *src++; }
00049   }
00050 
00051 
00052 /// Move some elements from overlapping place to another
00053 template<class EType>
00054 inline void moveElms(EType* dst,const EType* src,unsigned long n){
00055   if(src>dst){
00056     while(n--){ *dst++ = *src++; }
00057     }
00058   else if(dst>src){
00059     dst+=n;
00060     src+=n;
00061     while(n--){ *--dst = *--src; }
00062     }
00063   }
00064 
00065 
00066 /// Fill array of elements with given element
00067 template<class EType, class OType>
00068 inline void fillElms(EType* dst,const OType& src,unsigned long n){
00069   while(n--){ *dst++ = src; }
00070   }
00071 
00072 
00073 /// Zero out array of elements
00074 template<class EType>
00075 inline void clearElms(EType* dst,unsigned long n){
00076   memset(dst,0,sizeof(EType)*n);
00077   }
00078 
00079 
00080 /// Allocate array of elements, uninitialized
00081 template<class EType>
00082 inline FXbool allocElms(EType*& ptr,unsigned long n){
00083   return fxmalloc((void**)&ptr,sizeof(EType)*n);
00084   }
00085 
00086 
00087 /// Allocate array of elements, initialized with zero
00088 template<class EType>
00089 inline FXbool callocElms(EType*& ptr,unsigned long n){
00090   return fxcalloc((void**)&ptr,sizeof(EType)*n);
00091   }
00092 
00093 
00094 /// Allocate array of elements, initialized with bit-wise copy of src array
00095 template<class EType>
00096 inline FXbool dupElms(EType*& ptr,const EType* src,unsigned long n){
00097   return fxmemdup((void**)&ptr,src,sizeof(EType)*n);
00098   }
00099 
00100 
00101 /// Resize array of elements, without constructor or destructor
00102 template<class EType>
00103 inline FXbool resizeElms(EType*& ptr,unsigned long n){
00104   return fxresize((void**)&ptr,sizeof(EType)*n);
00105   }
00106 
00107 
00108 /// Free array of elements, without destruction
00109 template<class EType>
00110 inline void freeElms(EType*& ptr){
00111   fxfree((void**)&ptr);
00112   }
00113 
00114 
00115 /**********************  I m p l e m e n t a t i o n  ************************/
00116 
00117 // Specific implementations for built-in types
00118 
00119 
00120 // No-op constructors for array of basic type
00121 inline void constructElms(FXuchar*,unsigned long){ }
00122 inline void constructElms(FXchar*,unsigned long){ }
00123 inline void constructElms(FXushort*,unsigned long){ }
00124 inline void constructElms(FXshort*,unsigned long){ }
00125 inline void constructElms(FXuint*,unsigned long){ }
00126 inline void constructElms(FXint*,unsigned long){ }
00127 inline void constructElms(FXfloat*,unsigned long){ }
00128 inline void constructElms(FXdouble*,unsigned long){ }
00129 
00130 // No-op destructors for array of basic type
00131 inline void destructElms(FXuchar*,unsigned long){ }
00132 inline void destructElms(FXchar*,unsigned long){ }
00133 inline void destructElms(FXushort*,unsigned long){ }
00134 inline void destructElms(FXshort*,unsigned long){ }
00135 inline void destructElms(FXuint*,unsigned long){ }
00136 inline void destructElms(FXint*,unsigned long){ }
00137 inline void destructElms(FXfloat*,unsigned long){ }
00138 inline void destructElms(FXdouble*,unsigned long){ }
00139 
00140 // Simple bit-wise copy for array of basic type
00141 inline void copyElms(FXuchar* dst,const FXuchar* src,unsigned long n){ memcpy(dst,src,n); }
00142 inline void copyElms(FXchar* dst,const FXchar* src,unsigned long n){ memcpy(dst,src,n); }
00143 inline void copyElms(FXushort* dst,const FXushort* src,unsigned long n){ memcpy(dst,src,n<<1); }
00144 inline void copyElms(FXshort* dst,const FXshort* src,unsigned long n){ memcpy(dst,src,n<<1); }
00145 inline void copyElms(FXuint* dst,const FXuint* src,unsigned long n){ memcpy(dst,src,n<<2); }
00146 inline void copyElms(FXint* dst,const FXint* src,unsigned long n){ memcpy(dst,src,n<<2); }
00147 inline void copyElms(FXfloat* dst,const FXfloat* src,unsigned long n){ memcpy(dst,src,n<<2); }
00148 inline void copyElms(FXdouble* dst,const FXdouble* src,unsigned long n){ memcpy(dst,src,n<<3); }
00149 
00150 // Simple bit-wise copy for array of pointers to any type
00151 template<class EType> inline void copyElms(EType** dst,const EType** src,unsigned long n){ memcpy(dst,src,n*sizeof(void*)); }
00152 
00153 // Simple bit-wise move for array of basic type
00154 inline void moveElms(FXuchar* dst,const FXuchar* src,unsigned long n){ memmove(dst,src,n); }
00155 inline void moveElms(FXchar* dst,const FXchar* src,unsigned long n){ memmove(dst,src,n); }
00156 inline void moveElms(FXushort* dst,const FXushort* src,unsigned long n){ memmove(dst,src,n<<1); }
00157 inline void moveElms(FXshort* dst,const FXshort* src,unsigned long n){ memmove(dst,src,n<<1); }
00158 inline void moveElms(FXuint* dst,const FXuint* src,unsigned long n){ memmove(dst,src,n<<2); }
00159 inline void moveElms(FXint* dst,const FXint* src,unsigned long n){ memmove(dst,src,n<<2); }
00160 inline void moveElms(FXfloat* dst,const FXfloat* src,unsigned long n){ memmove(dst,src,n<<2); }
00161 inline void moveElms(FXdouble* dst,const FXdouble* src,unsigned long n){ memmove(dst,src,n<<3); }
00162 
00163 // Simple bit-wise move for array of pointers to any type
00164 template<class EType> inline void moveElms(EType** dst,const EType** src,unsigned long n){ memmove(dst,src,n*sizeof(void*)); }
00165 
00166 // Fill byte arrays with constant
00167 inline void fillElms(FXuchar* dst,const FXuchar& src,unsigned long n){ memset(dst,src,n); }
00168 inline void fillElms(FXchar* dst,const FXchar& src,unsigned long n){ memset(dst,src,n); }
00169 
00170 }
00171 
00172 #endif

Copyright © 1997-2010 Jeroen van der Zijp