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

FXElement.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                           Generic Element Handling                            *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2002 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXElement.h,v 1.4 2002/01/18 22:42:52 jeroen Exp $                       *
00023 ********************************************************************************/
00024 #ifndef FXELEMENT_H
00025 #define FXELEMENT_H
00026 
00027 
00028 /****************************  D e f i n i t i o n  ****************************/
00029 
00030 // Generic implementations for generic objects
00031 
00032 
00033 // Construct some elements at a location
00034 template<class TYPE>
00035 inline void constructElms(TYPE* ptr,unsigned int n){
00036   while(n--){ ::new ((void*)ptr) TYPE; ptr++; }
00037   }
00038 
00039 
00040 // Destruct some elements at a location
00041 template<class TYPE>
00042 inline void destructElms(TYPE* ptr,unsigned int n){
00043   while(n--){ ptr->~TYPE(); ptr++; }
00044   }
00045 
00046 
00047 // Copy some elements from one place to another
00048 template<class TYPE>
00049 inline void copyElms(TYPE* dst,const TYPE* src,unsigned int n){
00050   while(n--){ *dst++ = *src++; }
00051   }
00052 
00053 
00054 // Move some elements from overlapping place to another
00055 template<class TYPE>
00056 inline void moveElms(TYPE* dst,const TYPE* src,unsigned int n){
00057   if(src>dst){
00058     while(n--){ *dst++ = *src++; }
00059     }
00060   else if(dst>src){
00061     dst+=n;
00062     src+=n;
00063     while(n--){ *--dst = *--src; }
00064     }
00065   }
00066 
00067 
00068 // Save some elements to persistent store
00069 template<class TYPE>
00070 inline void saveElms(FXStream& store,const TYPE* ptr,unsigned int n){
00071   while(n--){ store << *ptr; ptr++; }
00072   }
00073 
00074 
00075 // Load some elements from persistent store
00076 template<class TYPE>
00077 inline void loadElms(FXStream& store,TYPE* ptr,unsigned int n){
00078   while(n--){ store >> *ptr; ptr++; }
00079   }
00080 
00081 
00082 // Allocate array of elements, uninitialized
00083 template<class TYPE>
00084 inline void allocElms(TYPE*& ptr,unsigned int n){
00085   fxmalloc((void**)&ptr,sizeof(TYPE)*n);
00086   }
00087 
00088 
00089 // Allocate array of elements, initialized with zero
00090 template<class TYPE>
00091 inline void callocElms(TYPE*& ptr,unsigned int n){
00092   fxcalloc((void**)&ptr,sizeof(TYPE)*n);
00093   }
00094 
00095 
00096 // Resize array of elements, without constructor or destructor
00097 template<class TYPE>
00098 inline void resizeElms(TYPE*& ptr,unsigned int n){
00099   fxresize((void**)&ptr,sizeof(TYPE)*n);
00100   }
00101 
00102 
00103 // Free array of elements, without destruction
00104 template<class TYPE>
00105 inline void freeElms(TYPE*& ptr){
00106   fxfree((void**)&ptr);
00107   }
00108 
00109 
00110 /**********************  I m p l e m e n t a t i o n  ************************/
00111 
00112 // Specific implementations for built-in types
00113 
00114 
00115 // No-op constructors for array of basic types
00116 inline void constructElms(FXuchar*,unsigned int){ }
00117 inline void constructElms(FXchar*,unsigned int){ }
00118 inline void constructElms(FXushort*,unsigned int){ }
00119 inline void constructElms(FXshort*,unsigned int){ }
00120 inline void constructElms(FXuint*,unsigned int){ }
00121 inline void constructElms(FXint*,unsigned int){ }
00122 inline void constructElms(FXfloat*,unsigned int){ }
00123 inline void constructElms(FXdouble*,unsigned int){ }
00124 inline void constructElms(void**,unsigned int){ }
00125 
00126 // No-op destructors for array of basic types
00127 inline void destructElms(FXuchar*,unsigned int){ }
00128 inline void destructElms(FXchar*,unsigned int){ }
00129 inline void destructElms(FXushort*,unsigned int){ }
00130 inline void destructElms(FXshort*,unsigned int){ }
00131 inline void destructElms(FXuint*,unsigned int){ }
00132 inline void destructElms(FXint*,unsigned int){ }
00133 inline void destructElms(FXfloat*,unsigned int){ }
00134 inline void destructElms(FXdouble*,unsigned int){ }
00135 inline void destructElms(void**,unsigned int){ }
00136 
00137 // Simple bit-wise copy for basic types
00138 inline void copyElms(FXuchar* dst,const FXuchar* src,unsigned int n){ memcpy(dst,src,n); }
00139 inline void copyElms(FXchar* dst,const FXchar* src,unsigned int n){ memcpy(dst,src,n); }
00140 inline void copyElms(FXushort* dst,const FXushort* src,unsigned int n){ memcpy(dst,src,n<<1); }
00141 inline void copyElms(FXshort* dst,const FXshort* src,unsigned int n){ memcpy(dst,src,n<<1); }
00142 inline void copyElms(FXuint* dst,const FXuint* src,unsigned int n){ memcpy(dst,src,n<<2); }
00143 inline void copyElms(FXint* dst,const FXint* src,unsigned int n){ memcpy(dst,src,n<<2); }
00144 inline void copyElms(FXfloat* dst,const FXfloat* src,unsigned int n){ memcpy(dst,src,n<<2); }
00145 inline void copyElms(FXdouble* dst,const FXdouble* src,unsigned int n){ memcpy(dst,src,n<<3); }
00146 inline void copyElms(void** dst,const void** src,unsigned int n){ memcpy(dst,src,n<<2); }
00147 
00148 // Simple bit-wise move for basic types
00149 inline void moveElms(FXuchar* dst,const FXuchar* src,unsigned int n){ memmove(dst,src,n); }
00150 inline void moveElms(FXchar* dst,const FXchar* src,unsigned int n){ memmove(dst,src,n); }
00151 inline void moveElms(FXushort* dst,const FXushort* src,unsigned int n){ memmove(dst,src,n<<1); }
00152 inline void moveElms(FXshort* dst,const FXshort* src,unsigned int n){ memmove(dst,src,n<<1); }
00153 inline void moveElms(FXuint* dst,const FXuint* src,unsigned int n){ memmove(dst,src,n<<2); }
00154 inline void moveElms(FXint* dst,const FXint* src,unsigned int n){ memmove(dst,src,n<<2); }
00155 inline void moveElms(FXfloat* dst,const FXfloat* src,unsigned int n){ memmove(dst,src,n<<2); }
00156 inline void moveElms(FXdouble* dst,const FXdouble* src,unsigned int n){ memmove(dst,src,n<<3); }
00157 inline void moveElms(void** dst,const void** src,unsigned int n){ memmove(dst,src,n<<2); }
00158 
00159 // Type-safe save for basic types
00160 inline void saveElms(FXStream& store,const FXuchar* ptr,unsigned int n){ store.save(ptr,n); }
00161 inline void saveElms(FXStream& store,const FXchar* ptr,unsigned int n){ store.save(ptr,n); }
00162 inline void saveElms(FXStream& store,const FXushort* ptr,unsigned int n){ store.save(ptr,n); }
00163 inline void saveElms(FXStream& store,const FXshort* ptr,unsigned int n){ store.save(ptr,n); }
00164 inline void saveElms(FXStream& store,const FXuint* ptr,unsigned int n){ store.save(ptr,n); }
00165 inline void saveElms(FXStream& store,const FXint* ptr,unsigned int n){ store.save(ptr,n); }
00166 inline void saveElms(FXStream& store,const FXfloat* ptr,unsigned int n){ store.save(ptr,n); }
00167 inline void saveElms(FXStream& store,const FXdouble* ptr,unsigned int n){ store.save(ptr,n); }
00168 
00169 // Type-safe load for basic types
00170 inline void loadElms(FXStream& store,FXuchar* ptr,unsigned int n){ store.load(ptr,n); }
00171 inline void loadElms(FXStream& store,FXchar* ptr,unsigned int n){ store.load(ptr,n); }
00172 inline void loadElms(FXStream& store,FXushort* ptr,unsigned int n){ store.load(ptr,n); }
00173 inline void loadElms(FXStream& store,FXshort* ptr,unsigned int n){ store.load(ptr,n); }
00174 inline void loadElms(FXStream& store,FXuint* ptr,unsigned int n){ store.load(ptr,n); }
00175 inline void loadElms(FXStream& store,FXint* ptr,unsigned int n){ store.load(ptr,n); }
00176 inline void loadElms(FXStream& store,FXfloat* ptr,unsigned int n){ store.load(ptr,n); }
00177 inline void loadElms(FXStream& store,FXdouble* ptr,unsigned int n){ store.load(ptr,n); }
00178 
00179 
00180 #endif