CEPS  24.01
Cardiac ElectroPhysiology Simulator
CepsMemory.hpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
29 
31 #pragma once
32 
37 
39 namespace ceps
40 {
41 
43  template <typename _Type>
44  CepsBool
45  isNullPtr(_Type* ptr)
46  {
47  return ptr == nullptr;
48  }
49 
51  template <typename _Type>
52  CepsBool
53  isNullPtr(std::shared_ptr<_Type> ptr)
54  {
55  return ptr.get() == nullptr;
56  }
57 
59  template <typename _Type>
60  CepsBool
61  isValidPtr(_Type* ptr)
62  {
63  return not isNullPtr(ptr);
64  }
65 
66 
68  template <typename _Type>
69  CepsBool
70  isValidPtr(std::shared_ptr<_Type> ptr)
71  {
72  return not isNullPtr(ptr);
73  }
74 
76  template <typename _Type, typename U>
77  CepsBool
78  isSamePtr(_Type* xptr, U* yptr)
79  {
80  return (xptr==yptr);
81  }
82 
84  template <typename _Type, typename U>
85  CepsBool
86  isDifferentPtr(_Type* xptr, U* yptr)
87  {
88  return not isSamePtr(xptr,yptr);
89  }
90 
92  template <typename _Derived, class _Base>
93  _Derived
94  runtimeCast(_Base x)
95  {
96  if constexpr (std::is_pointer_v<_Base>)
97  {
98  _Derived res = dynamic_cast<_Derived>(x);
100  "invalid dynamic cast from " << typeid(_Base).name() << " to " << typeid(_Derived).name()
101  );
102  return res;
103  }
104  else
105  return static_cast<_Derived>(x);
106  }
107 
108 
109  // --------- AUTO DESTROYER -------------------
110 
114  template <typename _Type>
115  void
116  destroyObject (_Type &)
117  {}
118 
122  template <typename _Type>
123  void
124  destroyObject(_Type*& obj)
125  {
126  if (obj != nullptr)
127  delete obj;
128  obj = nullptr;
129  return;
130  }
131 
135  template <typename _Type>
136  void
137  destroyObject(const _Type* obj)
138  {
139  if (obj != nullptr)
140  delete obj;
141  return;
142  }
143 
147  template <typename _Type>
148  void
150  {
151  }
152 
156  template <typename _Type>
157  void
158  destroyTabular(_Type*& obj)
159  {
160  if (obj != nullptr)
161  delete[] obj;
162  obj = nullptr;
163  return;
164  }
165 
168  template <typename _Type, typename... _Args>
169  _Type*
170  getNew(_Args... args)
171  {
172  _Type* arr = new(std::nothrow) _Type(args...);
173  // Any case that can be tested ?
174  // CEPS_ABORT_IF(ceps::isNullPtr(arr),
175  // "Could not allocate memory for new object"
176  // );
177  return arr;
178  }
179 
181  template <typename _Type>
182  _Type*
183  newArray(long unsigned int n, _Type fill=_Type())
184  {
185  _Type* arr = new _Type[n];
187  "Could not allocate memory for new object"
188  );
189  for (CepsUInt i=0; i<n; ++i)
190  arr[i] = _Type(fill);
191  return arr;
192  }
193 
195  template <typename _Type>
196  _Type*
197  newCopyArray(_Type* src, long unsigned int n)
198  {
199  _Type* arr = new(std::nothrow) _Type[n];
201  "Could not allocate memory for new object"
202  );
203  memcpy(arr,src,sizeof(_Type)*n);
204  return arr;
205  }
206 
207 } // namespace ceps
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
A namespace for all utility methods.
_Type * newArray(long unsigned int n, _Type fill=_Type())
Creates a C-array with optional init value.
Definition: CepsMemory.hpp:183
void destroyTabular(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:149
CepsBool isValidPtr(_Type *ptr)
Tells if pointer is not null.
Definition: CepsMemory.hpp:61
_Derived runtimeCast(_Base x)
Perform a runtime cast between base type and derived type if we can.
Definition: CepsMemory.hpp:94
_Type * getNew(_Args... args)
Allocates memory for one object. Be careful, the expansion of arguments may produce some weird result...
Definition: CepsMemory.hpp:170
CepsBool isNullPtr(_Type *ptr)
Tells if passed pointer is null.
Definition: CepsMemory.hpp:45
CepsBool isDifferentPtr(_Type *xptr, U *yptr)
Tells if two pointers designate two different addresses.
Definition: CepsMemory.hpp:86
void destroyObject(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:116
_Type * newCopyArray(_Type *src, long unsigned int n)
Generates a C-style copy of a C-style array.
Definition: CepsMemory.hpp:197
CepsBool isSamePtr(_Type *xptr, U *yptr)
Tells if two pointers designate the same address.
Definition: CepsMemory.hpp:78