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

FXElement.h
1 /********************************************************************************
2 * *
3 * G e n e r i c E l e m e n t H a n d l i n g *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2022 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (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 *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXELEMENT_H
22 #define FXELEMENT_H
23 
24 namespace FX {
25 
26 /**************************** D e f i n i t i o n ****************************/
27 
28 // Generic implementations for generic objects
29 
30 
31 // In-situ construct element at pointer
32 template<typename EType>
33 inline EType* construct(EType* ptr){
34  ::new ((void*)ptr) EType; return ptr;
35  }
36 
37 
38 // In-situ construct element at pointer, with argument
39 template<typename EType,typename Arg>
40 inline EType* construct(EType* ptr,Arg arg){
41  ::new ((void*)ptr) EType(arg); return ptr;
42  }
43 
44 
45 // In-situ destroy element at pointer
46 template<typename EType>
47 inline void destruct(EType* ptr){
48  ptr->~EType();
49  }
50 
51 
53 template<typename EType>
54 inline void constructElms(EType* ptr,FXuval n){
55  while(n--){ construct(ptr); ptr++; }
56  }
57 
58 
60 template<typename EType,typename Arg>
61 inline void constructElms(EType* ptr,Arg arg,FXuval n){
62  while(n--){ construct(ptr,arg); ptr++; }
63  }
64 
65 
67 template<typename EType>
68 inline void destructElms(EType* ptr,FXuval n){
69  while(n--){ destruct(ptr); ptr++; }
70  }
71 
72 
74 template<typename EType,typename OType>
75 inline void copyElms(EType* dst,const OType* src,FXuval n){
76  while(n--){ *dst++ = *src++; }
77  }
78 
79 
81 template<typename EType>
82 inline void bitcopyElms(EType* dst,const EType* src,FXuval n){
83  memcpy((void*)dst,(const void*)src,n*sizeof(EType));
84  }
85 
86 
88 template<typename EType>
89 inline void moveElms(EType* dst,const EType* src,FXuval n){
90  if(src!=dst){
91  if(0<(src-dst)){
92  while(n--){ *dst++ = *src++; }
93  }
94  else{
95  dst+=n;
96  src+=n;
97  while(n--){ *--dst = *--src; }
98  }
99  }
100  }
101 
102 
104 template<typename EType>
105 inline void bitmoveElms(EType* dst,const EType* src,FXuval n){
106  memmove((void*)dst,(const void*)src,n*sizeof(EType));
107  }
108 
109 
111 template<typename EType>
112 inline EType& swap(EType& dst,EType& src){
113  EType t=dst; dst=src; src=t;
114  return dst;
115  }
116 
117 
119 template<typename EType>
120 inline void swapElms(EType* dst,const EType* src,FXuval n){
121  while(n--){ swap(*dst++,*src++); }
122  }
123 
124 
126 template<typename EType>
127 inline FXbool equalElms(const EType* dst,const EType* src,FXuval n){
128  while(n--){ if(!(*dst++ == *src++)) return false; }
129  return true;
130  }
131 
132 
134 template<typename EType,typename OType>
135 inline void fillElms(EType* dst,const OType& src,FXuval n){
136  while(n--){ *dst++ = src; }
137  }
138 
139 
141 template<typename EType>
142 inline void clearElms(EType* dst,FXuval n){
143  memset((void*)dst,0,sizeof(EType)*n);
144  }
145 
146 
148 template<typename EType>
149 inline FXbool allocElms(EType*& ptr,FXuval n){
150  return fxmalloc((void**)&ptr,sizeof(EType)*n);
151  }
152 
153 
155 template<typename EType>
156 inline FXbool callocElms(EType*& ptr,FXuval n){
157  return fxcalloc((void**)&ptr,sizeof(EType)*n);
158  }
159 
160 
162 template<typename EType>
163 inline FXbool dupElms(EType*& ptr,const EType* src,FXuval n){
164  return fxmemdup((void**)&ptr,src,sizeof(EType)*n);
165  }
166 
167 
169 template<typename EType>
170 inline FXbool resizeElms(EType*& ptr,FXuval n){
171  return fxresize((void**)&ptr,sizeof(EType)*n);
172  }
173 
174 
176 template<typename EType>
177 inline void freeElms(EType*& ptr){
178  fxfree((void**)&ptr);
179  }
180 
181 
182 /********************** I m p l e m e n t a t i o n ************************/
183 
184 // Specific implementations for built-in types
185 
186 
187 // No-op constructors for array of basic type
188 inline void constructElms(FXchar*,FXuval){ }
189 inline void constructElms(FXuchar*,FXuval){ }
190 inline void constructElms(FXschar*,FXuval){ }
191 inline void constructElms(FXushort*,FXuval){ }
192 inline void constructElms(FXshort*,FXuval){ }
193 inline void constructElms(FXuint*,FXuval){ }
194 inline void constructElms(FXint*,FXuval){ }
195 inline void constructElms(FXulong*,FXuval){ }
196 inline void constructElms(FXlong*,FXuval){ }
197 inline void constructElms(FXfloat*,FXuval){ }
198 inline void constructElms(FXdouble*,FXuval){ }
199 
200 // No-op constructors for array of pointers to any type
201 template<typename EType> inline void constructElms(EType**,FXuval){ }
202 
203 
204 // No-op destructors for array of basic type
205 inline void destructElms(FXchar*,FXuval){ }
206 inline void destructElms(FXuchar*,FXuval){ }
207 inline void destructElms(FXschar*,FXuval){ }
208 inline void destructElms(FXushort*,FXuval){ }
209 inline void destructElms(FXshort*,FXuval){ }
210 inline void destructElms(FXuint*,FXuval){ }
211 inline void destructElms(FXint*,FXuval){ }
212 inline void destructElms(FXulong*,FXuval){ }
213 inline void destructElms(FXlong*,FXuval){ }
214 inline void destructElms(FXfloat*,FXuval){ }
215 inline void destructElms(FXdouble*,FXuval){ }
216 
217 // No-op destructors for array of pointers to any type
218 template<typename EType> inline void destructElms(EType**,FXuval){ }
219 
220 
221 // Simple bit-wise copy for array of basic type
222 inline void copyElms(FXchar* dst,const FXchar* src,FXuval n){ memcpy(dst,src,n); }
223 inline void copyElms(FXuchar* dst,const FXuchar* src,FXuval n){ memcpy(dst,src,n); }
224 inline void copyElms(FXschar* dst,const FXschar* src,FXuval n){ memcpy(dst,src,n); }
225 inline void copyElms(FXushort* dst,const FXushort* src,FXuval n){ memcpy(dst,src,n<<1); }
226 inline void copyElms(FXshort* dst,const FXshort* src,FXuval n){ memcpy(dst,src,n<<1); }
227 inline void copyElms(FXuint* dst,const FXuint* src,FXuval n){ memcpy(dst,src,n<<2); }
228 inline void copyElms(FXint* dst,const FXint* src,FXuval n){ memcpy(dst,src,n<<2); }
229 inline void copyElms(FXulong* dst,const FXulong* src,FXuval n){ memcpy(dst,src,n<<3); }
230 inline void copyElms(FXlong* dst,const FXlong* src,FXuval n){ memcpy(dst,src,n<<3); }
231 inline void copyElms(FXfloat* dst,const FXfloat* src,FXuval n){ memcpy(dst,src,n<<2); }
232 inline void copyElms(FXdouble* dst,const FXdouble* src,FXuval n){ memcpy(dst,src,n<<3); }
233 
234 // Simple bit-wise copy for array of pointers to any type
235 template<typename EType> inline void copyElms(EType** dst,const EType** src,FXuval n){ memcpy(dst,src,n*sizeof(void*)); }
236 
237 
238 // Simple bit-wise move for array of basic type
239 inline void moveElms(FXchar* dst,const FXchar* src,FXuval n){ memmove(dst,src,n); }
240 inline void moveElms(FXuchar* dst,const FXuchar* src,FXuval n){ memmove(dst,src,n); }
241 inline void moveElms(FXschar* dst,const FXschar* src,FXuval n){ memmove(dst,src,n); }
242 inline void moveElms(FXushort* dst,const FXushort* src,FXuval n){ memmove(dst,src,n<<1); }
243 inline void moveElms(FXshort* dst,const FXshort* src,FXuval n){ memmove(dst,src,n<<1); }
244 inline void moveElms(FXuint* dst,const FXuint* src,FXuval n){ memmove(dst,src,n<<2); }
245 inline void moveElms(FXint* dst,const FXint* src,FXuval n){ memmove(dst,src,n<<2); }
246 inline void moveElms(FXulong* dst,const FXulong* src,FXuval n){ memmove(dst,src,n<<3); }
247 inline void moveElms(FXlong* dst,const FXlong* src,FXuval n){ memmove(dst,src,n<<3); }
248 inline void moveElms(FXfloat* dst,const FXfloat* src,FXuval n){ memmove(dst,src,n<<2); }
249 inline void moveElms(FXdouble* dst,const FXdouble* src,FXuval n){ memmove(dst,src,n<<3); }
250 
251 // Simple bit-wise move for array of pointers to any type
252 template<typename EType> inline void moveElms(EType** dst,const EType** src,FXuval n){ memmove(dst,src,n*sizeof(void*)); }
253 
254 
255 // Simple bit-wise comparison for array of basic type
256 inline FXbool equalElms(const FXchar* dst,const FXchar* src,FXuval n){ return memcmp(dst,src,n)==0; }
257 inline FXbool equalElms(const FXuchar* dst,const FXuchar* src,FXuval n){ return memcmp(dst,src,n)==0; }
258 inline FXbool equalElms(const FXschar* dst,const FXschar* src,FXuval n){ return memcmp(dst,src,n)==0; }
259 inline FXbool equalElms(const FXushort* dst,const FXushort* src,FXuval n){ return memcmp(dst,src,n<<1)==0; }
260 inline FXbool equalElms(const FXshort* dst,const FXshort* src,FXuval n){ return memcmp(dst,src,n<<1)==0; }
261 inline FXbool equalElms(const FXuint* dst,const FXuint* src,FXuval n){ return memcmp(dst,src,n<<2)==0; }
262 inline FXbool equalElms(const FXint* dst,const FXint* src,FXuval n){ return memcmp(dst,src,n<<2)==0; }
263 inline FXbool equalElms(const FXulong* dst,const FXulong* src,FXuval n){ return memcmp(dst,src,n<<3)==0; }
264 inline FXbool equalElms(const FXlong* dst,const FXlong* src,FXuval n){ return memcmp(dst,src,n<<3)==0; }
265 inline FXbool equalElms(const FXfloat* dst,const FXfloat* src,FXuval n){ return memcmp(dst,src,n<<2)==0; }
266 inline FXbool equalElms(const FXdouble* dst,const FXdouble* src,FXuval n){ return memcmp(dst,src,n<<3)==0; }
267 
268 // Simple bit-wise comparison for array of pointers to any type
269 template<typename EType> inline FXbool equalElms(EType** dst,const EType** src,FXuval n){ return memcmp(dst,src,n*sizeof(void*))==0; }
270 
271 
272 // Fill byte arrays with constant
273 inline void fillElms(FXchar* dst,const FXchar& src,FXuval n){ memset(dst,src,n); }
274 inline void fillElms(FXuchar* dst,const FXuchar& src,FXuval n){ memset(dst,src,n); }
275 inline void fillElms(FXschar* dst,const FXschar& src,FXuval n){ memset(dst,src,n); }
276 
277 }
278 
279 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp