Free Electron
Delete.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __core_Delete_h__
8 #define __core_Delete_h__
9 
10 namespace fe
11 {
12 
13 /**************************************************************************//**
14  @fn deleteByType
15  @brief Contingent deletion of contents, based on type
16 
17  @ingroup core
18 
19  Usage:
20  @code
21  int x;
22  int* pX=new int();
23  int** array=new int[10]();
24  sp<Counted> counted=new Counted();
25 
26  deleteByType(x); // does nothing
27  deleteByType(pX); // calls delete pX
28  deleteByType(array); // calls delete[] array
29  deleteByType(counted); // does nothing
30  @endcode
31 *//***************************************************************************/
32 
33 template<typename T>
34 inline
35 void deleteByType(T t) { }
36 
37 inline
38 void deleteByType(void* pT)
39  { feLog("deleteByType() deleting void* is undefined\n"); }
40 
41 template<typename T>
42 inline
43 void deleteByType(T* pT) { delete pT; }
44 
45 template<typename T>
46 inline
47 void deleteByType(T** ppT) { delete[] ppT; }
48 
49 /* NOTE don't actually work, falls back on T
50 template<typename T>
51 inline
52 void deleteByType(sp<T> spT) { }
53 
54 template<typename T>
55 inline
56 void deleteByType(hp<T> hpT) { }
57 */
58 
59 /**************************************************************************//**
60  @fn defaultByType
61  @brief Returns NULL or closely equivalent value, based on type
62 
63  @ingroup core
64 
65  Usage:
66  @code
67  int x;
68  int* pX=new int();
69  int** array=new int[10]();
70  sp<Counted> counted=new Counted();
71 
72  defaultByType(x); // returns 0
73  defaultByType(pX); // returns NULL
74  defaultByType(array); // returns NULL
75  defaultByType(counted); // returns sp<Counted>()
76 
77  @endcode
78 
79  The argument is required for proper template matching during
80  compilation, but it is ignored during runtime.
81 
82  If a non-pointer type can not be initialized with 0,
83  an additional overload is required.
84 *//***************************************************************************/
85 
86 template<typename T>
87 inline
88 T defaultByType(T t) { return T(0); }
89 
90 template<typename T>
91 inline
92 T* defaultByType(T* pT) { return NULL; }
93 
94 /* NOTE don't actually work, falls back on T
95 template<typename T>
96 inline
97 sp<T> defaultByType(sp<T> spT) { return sp<T>(); }
98 
99 template<typename T>
100 inline
101 hp<T> defaultByType(hp<T> hpT) { return hp<T>(); }
102 */
103 
104 } // namespace
105 
106 #endif // __core_Delete_h__
kernel
Definition: namespace.dox:3