-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartMemory.h
113 lines (95 loc) · 2.94 KB
/
SmartMemory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* MIT License
*
* Copyright (c) 2018 Aykut Akin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/****************************************************************************
* Developer: Aykut Akin *
* *
* The code is implemented under Windows 7 OS with Visual C++ 2010 Express *
* The code also tested under Ubuntu 10.10 OS *
* *
* Description: Smart pointer library for C++ *
* *
****************************************************************************/
#ifndef __SMART_MEMORY_H__
#define __SMART_MEMORY_H__
#include <cstring>
template<class T> class SmartMemory {
private:
T* ptr; // Pointer's address
int size; // Size
T* allocMem(int nSize) { // Allocates memory according to size
if(nSize == 0)
return (0);
else if(nSize == 1)
return (new T);
else
return (new T[nSize]);
}
void changeMemSize(int nSize, int first=0) {
T* nPtr = allocMem(nSize); // Allocate new memory space
if(nSize > 0)
std::memcpy(nPtr, ptr+first, size); // Copy old memory values
freeMem(true); // Release old memory
ptr = nPtr; // Set ptr
size = nSize; // Set size
}
public:
void init(int nSize) {
ptr = allocMem(nSize); // Allocate memory
size = nSize; // Set size
}
void freeMem(bool isNew) { // Delete memory according to size
if(isNew) {
if(size == 0);
else if(size == 1)
delete ptr;
else
delete [] ptr;
}
}
int getSize() const { // Return size
return size;
}
T* getPtr() const { // Return pointed address
return ptr;
}
void setSize(int nSize) { // Set size
size = nSize;
}
void setPtr(T* nPtr) { // Set ptr
ptr = nPtr;
}
void extendMem(int nSize) { // Extend memory
changeMemSize(nSize);
}
void shrinkMem(int nSize, int first) { // Shrink memory
changeMemSize(nSize, first);
}
bool isNull() { // Return if pointer is null or not
if(ptr == 0)
return true;
else
return false;
}
};
#endif