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

FXElement.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * Generic Element Handling *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXElement.h,v 1.19 2006/01/22 17:58:01 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXELEMENT_H
25 #define FXELEMENT_H
26 
27 namespace FX {
28 
29 /**************************** D e f i n i t i o n ****************************/
30 
31 // Generic implementations for generic objects
32 
33 
34 /// Construct some elements at a location
35 template<class TYPE>
36 inline void constructElms(TYPE* ptr,unsigned long n){
37  while(n--){ ::new ((void*)ptr) TYPE; ptr++; }
38  }
39 
40 
41 /// Destruct some elements at a location
42 template<class TYPE>
43 inline void destructElms(TYPE* ptr,unsigned long n){
44  while(n--){ ptr->~TYPE(); ptr++; }
45  }
46 
47 
48 /// Copy some elements from one place to another
49 template<class TYPE>
50 inline void copyElms(TYPE* dst,const TYPE* src,unsigned long n){
51  while(n--){ *dst++ = *src++; }
52  }
53 
54 
55 /// Move some elements from overlapping place to another
56 template<class TYPE>
57 inline void moveElms(TYPE* dst,const TYPE* src,unsigned long n){
58  if(src>dst){
59  while(n--){ *dst++ = *src++; }
60  }
61  else if(dst>src){
62  dst+=n;
63  src+=n;
64  while(n--){ *--dst = *--src; }
65  }
66  }
67 
68 
69 /// Fill array of elements with given element
70 template<class TYPE>
71 inline void fillElms(TYPE* dst,const TYPE& src,unsigned long n){
72  while(n--){ *dst++ = src; }
73  }
74 
75 
76 /// Zero out array of elements
77 template<class TYPE>
78 inline void clearElms(TYPE* dst,unsigned long n){
79  memset(dst,0,sizeof(TYPE)*n);
80  }
81 
82 
83 /// Save some elements to persistent store
84 template<class TYPE>
85 inline void saveElms(FXStream& store,const TYPE* ptr,unsigned long n){
86  while(n--){ store << *ptr; ptr++; }
87  }
88 
89 
90 /// Load some elements from persistent store
91 template<class TYPE>
92 inline void loadElms(FXStream& store,TYPE* ptr,unsigned long n){
93  while(n--){ store >> *ptr; ptr++; }
94  }
95 
96 
97 /// Allocate array of elements, uninitialized
98 template<class TYPE>
99 inline FXint allocElms(TYPE*& ptr,unsigned long n){
100  return fxmalloc((void**)&ptr,sizeof(TYPE)*n);
101  }
102 
103 
104 /// Allocate array of elements, initialized with zero
105 template<class TYPE>
106 inline FXint callocElms(TYPE*& ptr,unsigned long n){
107  return fxcalloc((void**)&ptr,sizeof(TYPE)*n);
108  }
109 
110 
111 /// Allocate array of elements, initialized with bit-wise copy of src array
112 template<class TYPE>
113 inline FXint dupElms(TYPE*& ptr,const TYPE* src,unsigned long n){
114  return fxmemdup((void**)&ptr,src,sizeof(TYPE)*n);
115  }
116 
117 
118 /// Resize array of elements, without constructor or destructor
119 template<class TYPE>
120 inline FXint resizeElms(TYPE*& ptr,unsigned long n){
121  return fxresize((void**)&ptr,sizeof(TYPE)*n);
122  }
123 
124 
125 /// Free array of elements, without destruction
126 template<class TYPE>
127 inline void freeElms(TYPE*& ptr){
128  fxfree((void**)&ptr);
129  }
130 
131 
132 /********************** I m p l e m e n t a t i o n ************************/
133 
134 // Specific implementations for built-in types
135 
136 
137 // No-op constructors for array of basic type
138 inline void constructElms(FXuchar*,unsigned long){ }
139 inline void constructElms(FXchar*,unsigned long){ }
140 inline void constructElms(FXushort*,unsigned long){ }
141 inline void constructElms(FXshort*,unsigned long){ }
142 inline void constructElms(FXuint*,unsigned long){ }
143 inline void constructElms(FXint*,unsigned long){ }
144 inline void constructElms(FXfloat*,unsigned long){ }
145 inline void constructElms(FXdouble*,unsigned long){ }
146 
147 // No-op destructors for array of basic type
148 inline void destructElms(FXuchar*,unsigned long){ }
149 inline void destructElms(FXchar*,unsigned long){ }
150 inline void destructElms(FXushort*,unsigned long){ }
151 inline void destructElms(FXshort*,unsigned long){ }
152 inline void destructElms(FXuint*,unsigned long){ }
153 inline void destructElms(FXint*,unsigned long){ }
154 inline void destructElms(FXfloat*,unsigned long){ }
155 inline void destructElms(FXdouble*,unsigned long){ }
156 
157 // Simple bit-wise copy for array of basic type
158 inline void copyElms(FXuchar* dst,const FXuchar* src,unsigned long n){ memcpy(dst,src,n); }
159 inline void copyElms(FXchar* dst,const FXchar* src,unsigned long n){ memcpy(dst,src,n); }
160 inline void copyElms(FXushort* dst,const FXushort* src,unsigned long n){ memcpy(dst,src,n<<1); }
161 inline void copyElms(FXshort* dst,const FXshort* src,unsigned long n){ memcpy(dst,src,n<<1); }
162 inline void copyElms(FXuint* dst,const FXuint* src,unsigned long n){ memcpy(dst,src,n<<2); }
163 inline void copyElms(FXint* dst,const FXint* src,unsigned long n){ memcpy(dst,src,n<<2); }
164 inline void copyElms(FXfloat* dst,const FXfloat* src,unsigned long n){ memcpy(dst,src,n<<2); }
165 inline void copyElms(FXdouble* dst,const FXdouble* src,unsigned long n){ memcpy(dst,src,n<<3); }
166 
167 // Simple bit-wise copy for array of pointers to any type
168 template<class TYPE> inline void copyElms(TYPE** dst,const TYPE** src,unsigned long n){ memcpy(dst,src,n*sizeof(void*)); }
169 
170 // Simple bit-wise move for array of basic type
171 inline void moveElms(FXuchar* dst,const FXuchar* src,unsigned long n){ memmove(dst,src,n); }
172 inline void moveElms(FXchar* dst,const FXchar* src,unsigned long n){ memmove(dst,src,n); }
173 inline void moveElms(FXushort* dst,const FXushort* src,unsigned long n){ memmove(dst,src,n<<1); }
174 inline void moveElms(FXshort* dst,const FXshort* src,unsigned long n){ memmove(dst,src,n<<1); }
175 inline void moveElms(FXuint* dst,const FXuint* src,unsigned long n){ memmove(dst,src,n<<2); }
176 inline void moveElms(FXint* dst,const FXint* src,unsigned long n){ memmove(dst,src,n<<2); }
177 inline void moveElms(FXfloat* dst,const FXfloat* src,unsigned long n){ memmove(dst,src,n<<2); }
178 inline void moveElms(FXdouble* dst,const FXdouble* src,unsigned long n){ memmove(dst,src,n<<3); }
179 
180 // Simple bit-wise move for array of pointers to any type
181 template<class TYPE> inline void moveElms(TYPE** dst,const TYPE** src,unsigned long n){ memmove(dst,src,n*sizeof(void*)); }
182 
183 // Fill byte arrays with constant
184 inline void fillElms(FXuchar* dst,const FXuchar& src,unsigned long n){ memset(dst,src,n); }
185 inline void fillElms(FXchar* dst,const FXchar& src,unsigned long n){ memset(dst,src,n); }
186 
187 // Type-safe save for basic types
188 inline void saveElms(FXStream& store,const FXuchar* ptr,unsigned long n){ store.save(ptr,n); }
189 inline void saveElms(FXStream& store,const FXchar* ptr,unsigned long n){ store.save(ptr,n); }
190 inline void saveElms(FXStream& store,const FXushort* ptr,unsigned long n){ store.save(ptr,n); }
191 inline void saveElms(FXStream& store,const FXshort* ptr,unsigned long n){ store.save(ptr,n); }
192 inline void saveElms(FXStream& store,const FXuint* ptr,unsigned long n){ store.save(ptr,n); }
193 inline void saveElms(FXStream& store,const FXint* ptr,unsigned long n){ store.save(ptr,n); }
194 inline void saveElms(FXStream& store,const FXfloat* ptr,unsigned long n){ store.save(ptr,n); }
195 inline void saveElms(FXStream& store,const FXdouble* ptr,unsigned long n){ store.save(ptr,n); }
196 
197 // Type-safe load for basic types
198 inline void loadElms(FXStream& store,FXuchar* ptr,unsigned long n){ store.load(ptr,n); }
199 inline void loadElms(FXStream& store,FXchar* ptr,unsigned long n){ store.load(ptr,n); }
200 inline void loadElms(FXStream& store,FXushort* ptr,unsigned long n){ store.load(ptr,n); }
201 inline void loadElms(FXStream& store,FXshort* ptr,unsigned long n){ store.load(ptr,n); }
202 inline void loadElms(FXStream& store,FXuint* ptr,unsigned long n){ store.load(ptr,n); }
203 inline void loadElms(FXStream& store,FXint* ptr,unsigned long n){ store.load(ptr,n); }
204 inline void loadElms(FXStream& store,FXfloat* ptr,unsigned long n){ store.load(ptr,n); }
205 inline void loadElms(FXStream& store,FXdouble* ptr,unsigned long n){ store.load(ptr,n); }
206 
207 }
208 
209 #endif
void fillElms(TYPE *dst, const TYPE &src, unsigned long n)
Fill array of elements with given element.
Definition: FXElement.h:71
unsigned short FXushort
Definition: fxdefs.h:394
void saveElms(FXStream &store, const TYPE *ptr, unsigned long n)
Save some elements to persistent store.
Definition: FXElement.h:85
char FXchar
Definition: fxdefs.h:387
FXint dupElms(TYPE *&ptr, const TYPE *src, unsigned long n)
Allocate array of elements, initialized with bit-wise copy of src array.
Definition: FXElement.h:113
short FXshort
Definition: fxdefs.h:395
FXStream & save(const FXuchar *p, FXuval n)
Save arrays of items to stream.
unsigned int FXuint
Definition: fxdefs.h:396
void destructElms(TYPE *ptr, unsigned long n)
Destruct some elements at a location.
Definition: FXElement.h:43
FXint fxcalloc(void **ptr, unsigned long size)
Allocate cleaned memory.
void clearElms(TYPE *dst, unsigned long n)
Zero out array of elements.
Definition: FXElement.h:78
FXint callocElms(TYPE *&ptr, unsigned long n)
Allocate array of elements, initialized with zero.
Definition: FXElement.h:106
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
void constructElms(TYPE *ptr, unsigned long n)
Construct some elements at a location.
Definition: FXElement.h:36
void loadElms(FXStream &store, TYPE *ptr, unsigned long n)
Load some elements from persistent store.
Definition: FXElement.h:92
double FXdouble
Definition: fxdefs.h:399
void freeElms(TYPE *&ptr)
Free array of elements, without destruction.
Definition: FXElement.h:127
FXint resizeElms(TYPE *&ptr, unsigned long n)
Resize array of elements, without constructor or destructor.
Definition: FXElement.h:120
FXint fxmemdup(void **ptr, const void *src, unsigned long size)
Duplicate memory.
int FXint
Definition: fxdefs.h:397
FXint fxmalloc(void **ptr, unsigned long size)
Allocate memory.
FXint allocElms(TYPE *&ptr, unsigned long n)
Allocate array of elements, uninitialized.
Definition: FXElement.h:99
void copyElms(TYPE *dst, const TYPE *src, unsigned long n)
Copy some elements from one place to another.
Definition: FXElement.h:50
unsigned char FXuchar
Definition: fxdefs.h:392
void fxfree(void **ptr)
Free memory, resets ptr to NULL afterward.
float FXfloat
Definition: fxdefs.h:398
FXStream & load(FXuchar *p, FXuval n)
Load arrays of items from stream.
FXint fxresize(void **ptr, unsigned long size)
Resize memory.
void moveElms(TYPE *dst, const TYPE *src, unsigned long n)
Move some elements from overlapping place to another.
Definition: FXElement.h:57

Copyright © 1997-2005 Jeroen van der Zijp