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,2026 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 copy constructor
39 template<typename EType>
40 inline EType* construct(EType* ptr,const EType& org){
41  ::new ((void*)ptr) EType(org); return ptr;
42  }
43 
44 
45 // In-situ construct element at pointer, with argument
46 template<typename EType,typename Arg>
47 inline EType* construct(EType* ptr,Arg arg){
48  ::new ((void*)ptr) EType(arg); return ptr;
49  }
50 
51 
52 // In-situ destroy element at pointer
53 template<typename EType>
54 inline void destruct(EType* ptr){
55  ptr->~EType();
56  }
57 
58 
60 template<typename EType>
61 inline void constructElms(EType* ptr,FXuval n){
62  auto rtp=ptr+n;
63  while(ptr!=rtp){ construct(ptr++); }
64  }
65 
66 
68 template<typename EType,typename Arg>
69 inline void constructElms(EType* ptr,Arg arg,FXuval n){
70  auto rtp=ptr+n;
71  while(ptr!=rtp){ construct(ptr++,arg); }
72  }
73 
74 
76 template<typename EType>
77 inline void destructElms(EType* ptr,FXuval n){
78  auto rtp=ptr+n;
79  while(ptr!=rtp){ destruct(--rtp); }
80  }
81 
82 
84 template<typename EType,typename OType>
85 inline void copyElms(EType* dst,const OType* src,FXuval n){
86  while(n--){ *dst++ = *src++; }
87  }
88 
89 
91 template<typename EType,typename OType>
92 inline void rcopyElms(EType* dst,const OType* src,FXuval n){
93  dst+=n;
94  src+=n;
95  while(n--){ *--dst = *--src; }
96  }
97 
98 
100 template<typename EType>
101 inline void moveElms(EType* dst,const EType* src,FXuval n){
102  if(0<(src-dst)){
103  copyElms(dst,src,n);
104  }
105  else if(0>(src-dst)){
106  rcopyElms(dst,src,n);
107  }
108  }
109 
110 
112 template<typename EType>
113 inline void bitcopyElms(EType* dst,const EType* src,FXuval n){
114  memcpy((void*)dst,(const void*)src,n*sizeof(EType));
115  }
116 
117 
119 template<typename EType>
120 inline void bitmoveElms(EType* dst,const EType* src,FXuval n){
121  memmove((void*)dst,(const void*)src,n*sizeof(EType));
122  }
123 
124 
126 template<typename EType>
127 inline EType& swap(EType& dst,EType& src){
128  EType t=dst; dst=src; src=t;
129  return dst;
130  }
131 
132 
134 template<typename EType>
135 inline void swapElms(EType* dst,const EType* src,FXuval n){
136  while(n--){ swap(*dst++,*src++); }
137  }
138 
139 
141 template<typename EType>
142 inline FXbool equalElms(const EType* dst,const EType* src,FXuval n){
143  while(n--){ if(!(*dst++ == *src++)) return false; }
144  return true;
145  }
146 
147 
149 template<typename EType,typename OType>
150 inline void fillElms(EType* dst,const OType& src,FXuval n){
151  while(n--){ *dst++ = src; }
152  }
153 
154 
156 template<typename EType>
157 inline void clearElms(EType* dst,FXuval n){
158  memset((void*)dst,0,sizeof(EType)*n);
159  }
160 
161 
163 template<typename EType>
164 inline FXbool allocElms(EType*& ptr,FXuval n){
165  return fxmalloc((void**)&ptr,sizeof(EType)*n);
166  }
167 
168 
170 template<typename EType>
171 inline FXbool callocElms(EType*& ptr,FXuval n){
172  return fxcalloc((void**)&ptr,sizeof(EType)*n);
173  }
174 
175 
177 template<typename EType>
178 inline FXbool dupElms(EType*& ptr,const EType* src,FXuval n){
179  return fxmemdup((void**)&ptr,src,sizeof(EType)*n);
180  }
181 
182 
184 template<typename EType>
185 inline FXbool resizeElms(EType*& ptr,FXuval n){
186  return fxresize((void**)&ptr,sizeof(EType)*n);
187  }
188 
189 
191 template<typename EType>
192 inline void freeElms(EType*& ptr){
193  fxfree((void**)&ptr);
194  }
195 
196 
197 /********************** I m p l e m e n t a t i o n ************************/
198 
199 // Specific implementations for built-in types
200 
201 
202 // No-op constructors for array of basic type
203 inline void constructElms(FXchar*,FXuval){ }
204 inline void constructElms(FXuchar*,FXuval){ }
205 inline void constructElms(FXschar*,FXuval){ }
206 inline void constructElms(FXushort*,FXuval){ }
207 inline void constructElms(FXshort*,FXuval){ }
208 inline void constructElms(FXuint*,FXuval){ }
209 inline void constructElms(FXint*,FXuval){ }
210 inline void constructElms(FXulong*,FXuval){ }
211 inline void constructElms(FXlong*,FXuval){ }
212 inline void constructElms(FXfloat*,FXuval){ }
213 inline void constructElms(FXdouble*,FXuval){ }
214 
215 // No-op constructors for array of pointers to any type
216 template<typename EType> inline void constructElms(EType**,FXuval){ }
217 
218 
219 // No-op destructors for array of basic type
220 inline void destructElms(FXchar*,FXuval){ }
221 inline void destructElms(FXuchar*,FXuval){ }
222 inline void destructElms(FXschar*,FXuval){ }
223 inline void destructElms(FXushort*,FXuval){ }
224 inline void destructElms(FXshort*,FXuval){ }
225 inline void destructElms(FXuint*,FXuval){ }
226 inline void destructElms(FXint*,FXuval){ }
227 inline void destructElms(FXulong*,FXuval){ }
228 inline void destructElms(FXlong*,FXuval){ }
229 inline void destructElms(FXfloat*,FXuval){ }
230 inline void destructElms(FXdouble*,FXuval){ }
231 
232 // No-op destructors for array of pointers to any type
233 template<typename EType> inline void destructElms(EType**,FXuval){ }
234 
235 
236 // Simple bit-wise copy for array of basic type
237 inline void copyElms(FXchar* dst,const FXchar* src,FXuval n){ memcpy(dst,src,n); }
238 inline void copyElms(FXuchar* dst,const FXuchar* src,FXuval n){ memcpy(dst,src,n); }
239 inline void copyElms(FXschar* dst,const FXschar* src,FXuval n){ memcpy(dst,src,n); }
240 inline void copyElms(FXushort* dst,const FXushort* src,FXuval n){ memcpy(dst,src,n<<1); }
241 inline void copyElms(FXshort* dst,const FXshort* src,FXuval n){ memcpy(dst,src,n<<1); }
242 inline void copyElms(FXuint* dst,const FXuint* src,FXuval n){ memcpy(dst,src,n<<2); }
243 inline void copyElms(FXint* dst,const FXint* src,FXuval n){ memcpy(dst,src,n<<2); }
244 inline void copyElms(FXulong* dst,const FXulong* src,FXuval n){ memcpy(dst,src,n<<3); }
245 inline void copyElms(FXlong* dst,const FXlong* src,FXuval n){ memcpy(dst,src,n<<3); }
246 inline void copyElms(FXfloat* dst,const FXfloat* src,FXuval n){ memcpy(dst,src,n<<2); }
247 inline void copyElms(FXdouble* dst,const FXdouble* src,FXuval n){ memcpy(dst,src,n<<3); }
248 
249 // Simple bit-wise copy for array of pointers to any type
250 template<typename EType> inline void copyElms(EType** dst,const EType** src,FXuval n){ memcpy(dst,src,n*sizeof(void*)); }
251 
252 
253 // Simple bit-wise move for array of basic type
254 inline void moveElms(FXchar* dst,const FXchar* src,FXuval n){ memmove(dst,src,n); }
255 inline void moveElms(FXuchar* dst,const FXuchar* src,FXuval n){ memmove(dst,src,n); }
256 inline void moveElms(FXschar* dst,const FXschar* src,FXuval n){ memmove(dst,src,n); }
257 inline void moveElms(FXushort* dst,const FXushort* src,FXuval n){ memmove(dst,src,n<<1); }
258 inline void moveElms(FXshort* dst,const FXshort* src,FXuval n){ memmove(dst,src,n<<1); }
259 inline void moveElms(FXuint* dst,const FXuint* src,FXuval n){ memmove(dst,src,n<<2); }
260 inline void moveElms(FXint* dst,const FXint* src,FXuval n){ memmove(dst,src,n<<2); }
261 inline void moveElms(FXulong* dst,const FXulong* src,FXuval n){ memmove(dst,src,n<<3); }
262 inline void moveElms(FXlong* dst,const FXlong* src,FXuval n){ memmove(dst,src,n<<3); }
263 inline void moveElms(FXfloat* dst,const FXfloat* src,FXuval n){ memmove(dst,src,n<<2); }
264 inline void moveElms(FXdouble* dst,const FXdouble* src,FXuval n){ memmove(dst,src,n<<3); }
265 
266 // Simple bit-wise move for array of pointers to any type
267 template<typename EType> inline void moveElms(EType** dst,const EType** src,FXuval n){ memmove(dst,src,n*sizeof(void*)); }
268 
269 
270 // Simple bit-wise comparison for array of basic type
271 inline FXbool equalElms(const FXchar* dst,const FXchar* src,FXuval n){ return memcmp(dst,src,n)==0; }
272 inline FXbool equalElms(const FXuchar* dst,const FXuchar* src,FXuval n){ return memcmp(dst,src,n)==0; }
273 inline FXbool equalElms(const FXschar* dst,const FXschar* src,FXuval n){ return memcmp(dst,src,n)==0; }
274 inline FXbool equalElms(const FXushort* dst,const FXushort* src,FXuval n){ return memcmp(dst,src,n<<1)==0; }
275 inline FXbool equalElms(const FXshort* dst,const FXshort* src,FXuval n){ return memcmp(dst,src,n<<1)==0; }
276 inline FXbool equalElms(const FXuint* dst,const FXuint* src,FXuval n){ return memcmp(dst,src,n<<2)==0; }
277 inline FXbool equalElms(const FXint* dst,const FXint* src,FXuval n){ return memcmp(dst,src,n<<2)==0; }
278 inline FXbool equalElms(const FXulong* dst,const FXulong* src,FXuval n){ return memcmp(dst,src,n<<3)==0; }
279 inline FXbool equalElms(const FXlong* dst,const FXlong* src,FXuval n){ return memcmp(dst,src,n<<3)==0; }
280 inline FXbool equalElms(const FXfloat* dst,const FXfloat* src,FXuval n){ return memcmp(dst,src,n<<2)==0; }
281 inline FXbool equalElms(const FXdouble* dst,const FXdouble* src,FXuval n){ return memcmp(dst,src,n<<3)==0; }
282 
283 // Simple bit-wise comparison for array of pointers to any type
284 template<typename EType> inline FXbool equalElms(EType** dst,const EType** src,FXuval n){ return memcmp(dst,src,n*sizeof(void*))==0; }
285 
286 
287 // Fill byte arrays with constant
288 inline void fillElms(FXchar* dst,const FXchar& src,FXuval n){ memset(dst,src,n); }
289 inline void fillElms(FXuchar* dst,const FXuchar& src,FXuval n){ memset(dst,src,n); }
290 inline void fillElms(FXschar* dst,const FXschar& src,FXuval n){ memset(dst,src,n); }
291 
292 }
293 
294 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2026 Jeroen van der Zijp