xoreos  0.0.5
deallocator.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_DEALLOCATOR_H
26 #define COMMON_DEALLOCATOR_H
27 
28 #include <cstdlib>
29 
30 namespace Common {
31 
32 /* Based on Boost's checked_delete. Makes sure the type is complete
33  * before deletion, becaused deleting an object with incomplete type
34  * is undefined behaviour.
35  */
36 
39  template<typename T>
40  static void destroy(T *x) {
41  // intentionally complex - simplification causes regressions
42  typedef char type_must_be_complete[sizeof(T) ? 1 : -1];
43  (void) sizeof(type_must_be_complete);
44  delete x;
45  }
46 };
47 
50  template<typename T>
51  static void destroy(T *x) {
52  // intentionally complex - simplification causes regressions
53  typedef char type_must_be_complete[sizeof(T) ? 1 : -1];
54  (void) sizeof(type_must_be_complete);
55  delete[] x;
56  }
57 };
58 
61  template<typename T>
62  static void destroy(T *x) {
63  free(x);
64  }
65 };
66 
67 } // End of namespace Common
68 
69 #endif // COMMON_DEALLOCATOR_H
Definition: 2dafile.h:39
static void destroy(T *x)
Definition: deallocator.h:40
static void destroy(T *x)
Definition: deallocator.h:62
Deallocate a normal pointer.
Definition: deallocator.h:38
static void destroy(T *x)
Definition: deallocator.h:51
Deallocate a pointer to an array.
Definition: deallocator.h:49
Deallocate a pointer using free().
Definition: deallocator.h:60