Deallocate.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Deallocate_H
21 #define MIRTK_Deallocate_H
22 
23 namespace mirtk {
24 
25 
26 /// Delete object
27 template <typename Type>
28 inline void Delete(Type *&p)
29 {
30  delete p;
31  p = NULL;
32 }
33 
34 /// Deallocate 1D array
35 template <typename Type>
36 inline void Deallocate(Type *&p)
37 {
38  delete[] p;
39  p = NULL;
40 }
41 
42 /// Deallocate 2D array stored in contiguous memory block
43 ///
44 /// \param[in] matrix Previously allocated array or \c NULL.
45 /// \param[in] data Contiguous memory used by this array, but managed
46 /// separately. If not \c NULL, only the pointers in the
47 /// array are deallocated, but not the \c data memory itself.
48 /// Otherwise, also the contiguous data memory block is freed.
49 template <class Type>
50 inline void Deallocate(Type **&matrix, void *data = NULL)
51 {
52  if (matrix) {
53  if (matrix[0] != data) {
54  delete[] matrix[0];
55  }
56  delete[] matrix;
57  matrix = NULL;
58  }
59 }
60 
61 /// Deallocate 3D array stored in contiguous memory block
62 ///
63 /// \param[in] matrix Previously allocated array or \c NULL.
64 /// \param[in] data Contiguous memory used by this array, but managed
65 /// separately. If not \c NULL, only the pointers in the
66 /// array are deallocated, but not the \c data memory itself.
67 /// Otherwise, also the contiguous data memory block is freed.
68 template <class Type>
69 inline void Deallocate(Type ***&matrix, void *data = NULL)
70 {
71  if (matrix) {
72  if (matrix[0][0] != data) {
73  delete[] matrix[0][0];
74  }
75  delete[] matrix[0];
76  delete[] matrix;
77  matrix = NULL;
78  }
79 }
80 
81 /// Deallocate 4D array stored in contiguous memory block
82 ///
83 /// \param[in] matrix Previously allocated array or \c NULL.
84 /// \param[in] data Contiguous memory used by this array, but managed
85 /// separately. If not \c NULL, only the pointers in the
86 /// array are deallocated, but not the \c data memory itself.
87 /// Otherwise, also the contiguous data memory block is freed.
88 template <class Type>
89 inline void Deallocate(Type ****&matrix, void *data = NULL)
90 {
91  if (matrix) {
92  if (matrix[0][0][0] != data) {
93  delete[] matrix[0][0][0];
94  }
95  delete[] matrix[0][0];
96  delete[] matrix[0];
97  delete[] matrix;
98  matrix = NULL;
99  }
100 }
101 
102 
103 } // namespace mirtk
104 
105 #endif // MIRTK_Deallocate_H
void Delete(Type *&p)
Delete object.
Definition: Deallocate.h:28
Definition: IOConfig.h:41
void Deallocate(Type *&p)
Deallocate 1D array.
Definition: Deallocate.h:36