Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_MEMORY_MANAGEMENT_H__
00009 #define __SP_MEMORY_MANAGEMENT_H__
00010
00011
00012 #include "Base/spInputOutputLog.hpp"
00013
00014 #include <exception>
00015 #include <vector>
00016 #include <list>
00017 #include <stdarg.h>
00018
00019
00020 namespace sp
00021 {
00022
00023
00025 namespace MemoryManager
00026 {
00027
00028
00029
00031 template <typename T> void deleteMemory(T* &Buffer)
00032 {
00033 delete Buffer;
00034 Buffer = 0;
00035 }
00036
00038 template <typename T> void deleteBuffer(T* &Buffer)
00039 {
00040 delete [] Buffer;
00041 Buffer = 0;
00042 }
00043
00044
00045
00047 template <typename T> T* createMemory(
00048 const io::stringc &Description = "Unknown") throw (std::bad_alloc)
00049 {
00050 try
00051 {
00052 return new T();
00053 }
00054 catch (std::bad_alloc &e)
00055 {
00056 io::Log::error("< Bad Allocation > exception thrown for \"" + Description + "\" memory");
00057 throw e;
00058 }
00059
00060 return 0;
00061 }
00062
00064 template <typename T> T* createBuffer(
00065 const s32 Count, const io::stringc &Description = "Unknown") throw (std::bad_alloc)
00066 {
00067 try
00068 {
00069 return new T[Count];
00070 }
00071 catch (std::bad_alloc &e)
00072 {
00073 io::Log::error("< Bad Allocation > exception thrown for \"" + Description + "\" buffer");
00074 throw e;
00075 }
00076
00077 return 0;
00078 }
00079
00080
00081
00090 template < class T, template < class TL, class Allocator = std::allocator<TL> > class L > bool removeElement(
00091 L<T*> &List, T* &Object, bool DeleteMemory = false, bool UniqueObject = true)
00092 {
00093 if (!Object)
00094 return false;
00095
00096 bool Result = false;
00097
00098 for (typename L< T*, std::allocator<T*> >::iterator it = List.begin(); it != List.end();)
00099 {
00100 if (*it == Object)
00101 {
00102 Result = true;
00103
00104 if (DeleteMemory)
00105 MemoryManager::deleteMemory(Object);
00106
00107 it = List.erase(it);
00108
00109 if (UniqueObject)
00110 break;
00111 }
00112 else
00113 ++it;
00114 }
00115
00116 return Result;
00117 }
00118
00119 template <class T> bool removeElement(
00120 std::vector<T*> &List, T* &Object, bool DeleteMemory = false, bool UniqueObject = true)
00121 {
00122 return removeElement<T, std::vector>(List, Object, DeleteMemory, UniqueObject);
00123 }
00124
00125 template <class T> bool removeElement(
00126 std::list<T*> &List, T* &Object, bool DeleteMemory = false, bool UniqueObject = true)
00127 {
00128 return removeElement<T, std::list>(List, Object, DeleteMemory, UniqueObject);
00129 }
00130
00131
00132
00133 template <class T> T getElement(std::list<T> &List, u32 Index)
00134 {
00135 typename std::list< T, std::allocator<T> >::iterator it = List.begin();
00136
00137 for (; it != List.end() && Index > 0; ++it)
00138 --Index;
00139
00140 return *it;
00141 }
00142
00143 template <class T> const T getElement(const std::list<T> &List, u32 Index)
00144 {
00145 typename std::list< T, std::allocator<T> >::const_iterator it = List.begin();
00146
00147 for (; it != List.end() && Index > 0; ++it)
00148 --Index;
00149
00150 return *it;
00151 }
00152
00153
00154
00155 template < class T, template < class TL, class Allocator = std::allocator<TL> > class L > bool hasElement(
00156 const L<T> &List, const T &Object)
00157 {
00158 for (typename L< T, std::allocator<T> >::const_iterator it = List.begin(); it != List.end(); ++it)
00159 {
00160 if (*it == Object)
00161 return true;
00162 }
00163 return false;
00164 }
00165
00166 template <class T> bool hasElement(const std::vector<T> &List, const T &Object)
00167 {
00168 return hasElement<T, std::vector>(List, Object);
00169 }
00170
00171 template <class T> bool hasElement(const std::list<T> &List, const T &Object)
00172 {
00173 return hasElement<T, std::list>(List, Object);
00174 }
00175
00176
00177
00182 template < class T, template < class TL, class Allocator = std::allocator<TL> > class L > void deleteList(L<T*> &List)
00183 {
00184 for (typename L< T*, std::allocator<T*> >::iterator it = List.begin(); it != List.end(); ++it)
00185 MemoryManager::deleteMemory(*it);
00186 List.clear();
00187 }
00188
00189 template <class T> void deleteList(std::vector<T*> &List)
00190 {
00191 deleteList<T, std::vector>(List);
00192 }
00193
00194 template <class T> void deleteList(std::list<T*> &List)
00195 {
00196 deleteList<T, std::list>(List);
00197 }
00198
00199 }
00200
00201
00202 }
00203
00204
00205 #endif
00206
00207
00208
00209