EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SafeMem.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Bloody.Rabbit
24 */
25 
26 #ifndef __UTILS__SAFE_MEM_H__INCL__
27 #define __UTILS__SAFE_MEM_H__INCL__
28 
29 #include <cstddef>
30 #include <type_traits>
31 #include <memory>
32 #include <utility>
33 #include <memory.h>
34 #include <stdlib.h>
35 
36 
44 template< typename T >
45 inline T* SafeAlloc()
46 {
47  T* p = new T;
48  ::memset( p, 0, sizeof( T ) );
49 
50  return p;
51 }
52 
62 template< typename T >
63 inline T* SafeAllocArray( size_t size )
64 {
65  T* p = new T[ size ];
66  ::memset( p, 0, size * sizeof( T ) );
67 
68  return p;
69 }
70 
82 template< typename T >
83 inline void SafeDelete( T*& p )
84 {
85  delete p;
86  p = nullptr;
87 }
88 
96 template< typename T >
97 inline void SafeDeleteArray( T*& p )
98 {
99  delete[] p;
100  p = nullptr;
101 }
102 
110 template< typename T >
111 inline void SafeFree( T*& p )
112 {
113  ::free( p );
114  p = nullptr;
115 }
116 
123 template< typename T, typename F >
124 inline void SafeRelease( T*& p, F f )
125 {
126  /* We don't know if the function accepts
127  NULL pointers, like delete or free. */
128  if( NULL != p )
129  f( p );
130 
131  p = nullptr;
132 }
133 
134 /* make_unique code taken from Stephan T. Lavavej's proposal for adding to C++0x14 (N3656 revision) */
135 /* unique_ptr for single items -allan 7Mar16 */
136 template<class T, class... Args>
137 std::unique_ptr<T> make_unique(Args&&... args) {
138  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
139 }
140 
141 /* unique_ptr for arrays -allan 9Mar16 */
142 template<class T>
143 std::unique_ptr<T> make_uniquea(size_t n) {
144  //typedef typename remove_extent<T>::type U;
145  return std::unique_ptr<T>(new T[n]());
146 }
147 
148 template<class T>
149 class myPtr
150 {
151 public:
152  explicit myPtr(T* pointer) : ptr(pointer) { }
153  ~myPtr() { delete ptr; }
154  T* get() const { return ptr; }
155  T* operator->() const { return ptr; }
156  T* operator*() const { return *ptr; }
157 
158 private:
159  T* ptr;
160 };
161 
162 #endif /* !__UTILS__SAFE_MEM_H__INCL__ */
std::unique_ptr< T > make_unique(Args &&...args)
Definition: SafeMem.h:137
~myPtr()
Definition: SafeMem.h:153
void SafeRelease(T *&p, F f)
Releases pointer and nullifies it.
Definition: SafeMem.h:124
T * operator*() const
Definition: SafeMem.h:156
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
* args
T * operator->() const
Definition: SafeMem.h:155
void SafeFree(T *&p)
Frees and nullifies an array pointer.
Definition: SafeMem.h:111
T * ptr
Definition: SafeMem.h:159
std::unique_ptr< T > make_uniquea(size_t n)
Definition: SafeMem.h:143
Definition: SafeMem.h:149
T * SafeAllocArray(size_t size)
Allocates and zero-initializes an array.
Definition: SafeMem.h:63
T * SafeAlloc()
Allocates and zero-initializes memory.
Definition: SafeMem.h:45
void SafeDeleteArray(T *&p)
Deletes and nullifies an array pointer.
Definition: SafeMem.h:97
myPtr(T *pointer)
Definition: SafeMem.h:152
#define F(name, o, n)