Rudiments
nodeinlines.h
1// Copyright (c) 1999-2018 David Muse
2// See the COPYING file for more information
3#ifndef RUDIMENTS_NODEINLINES_H
4#define RUDIMENTS_NODEINLINES_H
5
6#include <rudiments/charstring.h>
7#include <rudiments/wcharstring.h>
8#include <rudiments/stdio.h>
9
10
11// Ideally we'd use explicit specialization here but old enough
12// compilers don't support it and this isn't any less efficient.
13
14
15// Why do the node_delete_value() and node_duplicate_value() functions take
16// pointers, just to dereference them?
17//
18// Older compilers (SCO CC on uw-7.0.1, gcc 2.95.3 on solaris 9 x86, probably
19// others...) seem to have trouble determining which function to call when
20// passing a reference rather than passing a pointer.
21//
22// Eg. If there are a bunch of node_delete_value() methods for primitives and
23// templated ones for const valuetype & (which does nothing),
24// valuetype & (which also does nothing) and valuetype * (which actually
25// deletes), then when compiling code like:
26//
27// someclass a;
28// node_delete_value(a,true,false);
29//
30// the compiler get confused whether to call the const valuetype &, valuetype &,
31// or valuetype * version.
32//
33// It's not clear why except just that there's a bug in the compiler. Newer
34// compilers work as expected.
35//
36// This works around the problem though. Apparently those old compilers don't
37// have any trouble figuring out which function to call if you have functions
38// that all take pointers and you call something like:
39//
40// someclass a;
41// node_delete_value(&a,true,false);
42//
43// So, for now at least, we'll do it this way.
44
45
46
47
48
49// delete methods...
50
51inline
52void node_delete_value(const char **value, bool managed, bool managedarray) {
53 if (managedarray) {
54 delete[] (char *)*value;
55 }
56}
57
58inline
59void node_delete_value(char **value, bool managed, bool managedarray) {
60 if (managedarray) {
61 delete[] *value;
62 }
63}
64
65inline
66void node_delete_value(const wchar_t **value, bool managed, bool managedarray) {
67 if (managedarray) {
68 delete[] (wchar_t *)*value;
69 }
70}
71
72inline
73void node_delete_value(wchar_t **value, bool managed, bool managedarray) {
74 if (managedarray) {
75 delete[] *value;
76 }
77}
78
79inline
80void node_delete_value(char *value, bool managed, bool managedarray) {
81}
82
83inline
84void node_delete_value(wchar_t *value, bool managed, bool managedarray) {
85}
86
87inline
88void node_delete_value(int16_t *value, bool managed, bool managedarray) {
89}
90
91inline
92void node_delete_value(int32_t *value, bool managed, bool managedarray) {
93}
94
95inline
96void node_delete_value(int64_t *value, bool managed, bool managedarray) {
97}
98
99inline
100void node_delete_value(const byte_t **value, bool managed, bool managedarray) {
101 if (managedarray) {
102 delete[] (byte_t *)*value;
103 }
104}
105
106inline
107void node_delete_value(byte_t **value, bool managed, bool managedarray) {
108 if (managedarray) {
109 delete[] *value;
110 }
111}
112
113inline
114void node_delete_value(byte_t *value, bool managed, bool managedarray) {
115}
116
117inline
118void node_delete_value(uint16_t *value, bool managed, bool managedarray) {
119}
120
121inline
122void node_delete_value(uint32_t *value, bool managed, bool managedarray) {
123}
124
125inline
126void node_delete_value(uint64_t *value, bool managed, bool managedarray) {
127}
128
129inline
130void node_delete_value(float *value, bool managed, bool managedarray) {
131}
132
133inline
134void node_delete_value(double *value, bool managed, bool managedarray) {
135}
136
137inline
138void node_delete_value(long double *value, bool managed, bool managedarray) {
139}
140
141template <class valuetype>
142inline
143void node_delete_value(valuetype **value, bool managed, bool managedarray) {
144 if (managed) {
145 delete *value;
146 } else if (managedarray) {
147 delete[] *value;
148 }
149}
150
151template <class valuetype>
152inline
153void node_delete_value(valuetype *value, bool managed, bool managedarray) {
154}
155
156
157
158
159
160// zero methods...
161
162inline
163void node_zero_value(const char **value) {
164 *((char **)value)=NULL;
165}
166
167inline
168void node_zero_value(char **value) {
169 *value=NULL;
170}
171
172inline
173void node_zero_value(const wchar_t **value) {
174 *((wchar_t **)value)=NULL;
175}
176
177inline
178void node_zero_value(wchar_t **value) {
179 *value=NULL;
180}
181
182inline
183void node_zero_value(char *value) {
184 *value='\0';
185}
186
187inline
188void node_zero_value(wchar_t *value) {
189 *value=L'\0';
190}
191
192inline
193void node_zero_value(int16_t *value) {
194 *value=0;
195}
196
197inline
198void node_zero_value(int32_t *value) {
199 *value=0;
200}
201
202inline
203void node_zero_value(int64_t *value) {
204 *value=0;
205}
206
207inline
208void node_zero_value(const byte_t **value) {
209 *((byte_t **)value)=NULL;
210}
211
212inline
213void node_zero_value(byte_t **value) {
214 *value=NULL;
215}
216
217inline
218void node_zero_value(byte_t *value) {
219 *value='\0';
220}
221
222inline
223void node_zero_value(uint16_t *value) {
224 *value=0;
225}
226
227inline
228void node_zero_value(uint32_t *value) {
229 *value=0;
230}
231
232inline
233void node_zero_value(uint64_t *value) {
234 *value=0;
235}
236
237inline
238void node_zero_value(float *value) {
239 *value=0.0;
240}
241
242inline
243void node_zero_value(double *value) {
244 *value=0.0;
245}
246
247inline
248void node_zero_value(long double *value) {
249 *value=0.0;
250}
251
252template <class valuetype>
253inline
254void node_zero_value(valuetype **value) {
255 *value=NULL;
256}
257
258template <class valuetype>
259inline
260void node_zero_value(valuetype *value) {
261}
262
263
264
265
266
267// duplicate methods...
268
269inline
270char *node_duplicate_value(const char **value,
271 bool managed, bool managedarray) {
272 if (managedarray) {
273 return charstring::duplicate(*value);
274 } else {
275 return (char *)value;
276 }
277}
278
279inline
280char *node_duplicate_value(char **value,
281 bool managed, bool managedarray) {
282 if (managedarray) {
283 return charstring::duplicate(*value);
284 } else {
285 return *value;
286 }
287}
288
289inline
290const wchar_t *node_duplicate_value(const wchar_t **value,
291 bool managed, bool managedarray) {
292 if (managedarray) {
293 return wcharstring::duplicate(*value);
294 } else {
295 return (wchar_t *)*value;
296 }
297}
298
299inline
300wchar_t *node_duplicate_value(wchar_t **value,
301 bool managed, bool managedarray) {
302 if (managedarray) {
303 return wcharstring::duplicate(*value);
304 } else {
305 return *value;
306 }
307}
308
309inline
310char node_duplicate_value(char *value,
311 bool managed, bool managedarray) {
312 return *value;
313}
314
315inline
316wchar_t node_duplicate_value(wchar_t *value,
317 bool managed, bool managedarray) {
318 return *value;
319}
320
321inline
322int16_t node_duplicate_value(int16_t *value,
323 bool managed, bool managedarray) {
324 return *value;
325}
326
327inline
328int32_t node_duplicate_value(int32_t *value,
329 bool managed, bool managedarray) {
330 return *value;
331}
332
333inline
334int64_t node_duplicate_value(int64_t *value,
335 bool managed, bool managedarray) {
336 return *value;
337}
338
339inline
340byte_t *node_duplicate_value(const byte_t **value,
341 bool managed, bool managedarray) {
342 if (managedarray) {
343 // this isn't a true duplicate, but it's
344 // the best we can do without a size
345 return (byte_t *)charstring::duplicate((const char *)*value);
346 } else {
347 return (byte_t *)*value;
348 }
349}
350
351inline
352byte_t *node_duplicate_value(byte_t **value,
353 bool managed, bool managedarray) {
354 if (managedarray) {
355 // this isn't a true duplicate, but it's
356 // the best we can do without a size
357 return (byte_t *)charstring::duplicate((char *)*value);
358 } else {
359 return *value;
360 }
361}
362
363inline
364byte_t node_duplicate_value(byte_t *value,
365 bool managed, bool managedarray) {
366 return *value;
367}
368
369inline
370uint16_t node_duplicate_value(uint16_t *value,
371 bool managed, bool managedarray) {
372 return *value;
373}
374
375inline
376uint32_t node_duplicate_value(uint32_t *value,
377 bool managed, bool managedarray) {
378 return *value;
379}
380
381inline
382uint64_t node_duplicate_value(uint64_t *value,
383 bool managed, bool managedarray) {
384 return *value;
385}
386
387inline
388float node_duplicate_value(float *value,
389 bool managed, bool managedarray) {
390 return *value;
391}
392
393inline
394double node_duplicate_value(double *value,
395 bool managed, bool managedarray) {
396 return *value;
397}
398
399inline
400long double node_duplicate_value(long double *value,
401 bool managed, bool managedarray) {
402 return *value;
403}
404
405template <class valuetype>
406inline
407valuetype *node_duplicate_value(valuetype **value,
408 bool managed, bool managedarray) {
409 if (managed) {
410 return new valuetype(**value);
411 } else if (managedarray) {
412 // no way to do this without a length
413 return *value;
414 } else {
415 return *value;
416 }
417}
418
419template <class valuetype>
420inline
421valuetype &node_duplicate_value(valuetype *value,
422 bool managed, bool managedarray) {
423 if (managed) {
424 return *(new valuetype(*value));
425 } else if (managedarray) {
426 // no way to do this without a length
427 return *value;
428 } else {
429 return *value;
430 }
431}
432
433#endif
Definition avltree.h:11
static char * duplicate(const char *str)
static wchar_t * duplicate(const wchar_t *str)