logog
logger optimized for games
object.hpp
Go to the documentation of this file.
1 
5 #ifndef __LOGOG_OBJECT_HPP__
6 #define __LOGOG_OBJECT_HPP__
7 
8 namespace logog
9 {
10 
11 #ifdef LOGOG_LEAK_DETECTION
12 
16 typedef void *PointerType;
17 
21 typedef LOGOG_UNORDERED_MAP< PointerType, size_t > AllocationsType;
22 
24 extern AllocationsType s_Allocations;
25 
30 extern void LockAllocationsMutex();
31 
34 extern void UnlockAllocationsMutex();
35 #endif // LOGOG_LEAK_DETECTION
36 
37 #ifdef new
38 #define LOGOG_PREVIOUS_DEFINITION_OF_NEW new
39 #undef new
40 #endif
41 #ifdef delete
42 #define LOGOG_PREVIOUS_DEFINITION_OF_DELETE delete
43 #undef delete
44 #endif //delete
45 
47 class Object
48 {
49 public:
50  Object();
51  /* Some builds complain about ~Object() being virtual... sorry lint :( */
52  virtual ~Object();
54  void *operator new( size_t nSize );
56  void *operator new[](size_t nSize);
57 
58  /* There's a wonderful behavior in Windows MFC in debug builds that causes it
59  * to attempt to redefine NEW with a macro if we're compiling in debug mode
60  * and we're using Gdiplus as well. In that case, Windows attempts to redefine
61  * new with a macro for everything that has a new. This wonderful behavior
62  * is worked around here. See http://support.microsoft.com/kb/317799/EN-US/
63  * and http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/0df13145-670e-4070-b0a1-61794b20dff7
64  * for more exciting information.
65  */
66 #ifdef _DEBUG
67 #ifdef LOGOG_FLAVOR_WINDOWS
68  void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
69  void* operator new[](size_t nSize, LPCSTR lpszFileName, int nLine);
70  void operator delete(void* ptr, LPCSTR lpszFileName, int nLine);
71  void operator delete[](void* ptr, LPCSTR lpszFileName, int nLine);
72 #endif // LOGOG_FLAVOR_WINDOWS
73 #endif // _DEBUG
74 
76  void operator delete( void *ptr );
78  void operator delete[]( void *ptr );
79 
83  static void *Allocate( size_t nSize );
84 
86  static void Deallocate( void *ptr );
87  };
88 
89 #ifdef LOGOG_PREVIOUS_DEFINITION_OF_NEW
90 #define new LOGOG_PREVIOUS_DEFINITION_OF_NEW
91 #endif
92 
93 #ifdef LOGOG_PREVIOUS_DEFINITION_OF_DELETE
94 #define delete GA_PREVIOUS_DEFINITION_OF_DELETE
95 #endif
96 
98 template <class T>
99 class Allocator
100 {
101 public:
103  typedef size_t size_type;
105  typedef ptrdiff_t difference_type;
107  typedef T *pointer;
109  typedef const T *const_pointer;
111  typedef T &reference;
113  typedef const T &const_reference;
115  typedef T value_type;
116 
119  Allocator( const Allocator & ) {}
120 
122  pointer allocate( size_type n, const void * = 0 )
123  {
124  T *t = ( T * ) Object::Allocate( n * sizeof( value_type ) );
125  return t;
126  }
127 
129  void deallocate( void *p, size_type )
130  {
131  if ( p )
132  {
133  Object::Deallocate( p );
134  }
135  }
136 
138  pointer address( reference x ) const
139  {
140  return &x;
141  }
143  const_pointer address( const_reference x ) const
144  {
145  return &x;
146  }
149  {
150  return *this;
151  }
153  void construct( pointer p, const T &val )
154  {
155  new(( T * ) p ) T( val );
156  }
158  void destroy( pointer p )
159  {
160 #ifdef LOGOG_FLAVOR_WINDOWS
161  // MSVC tends to complain unless we reference this pointer here.
162  p;
163 #endif // LOGOG_FLAVOR_WINDOWS
164  p->~T();
165  }
166 
168  size_type max_size() const
169  {
170  return size_t( -1 );
171  }
172 
175  template <class U>
176  struct rebind
177  {
180  };
181 
183  template <class U>
184  Allocator( const Allocator<U>& ) {}
185 
187  template <class U>
189  {
190  return *this;
191  }
192 };
193 
194 /* All specializations of this allocator are interchangeable. */
195 template <class T1, class T2>
197  const Allocator<T2>& )
198 {
199  return true;
200 }
201 template <class T1, class T2>
203  const Allocator<T2>& )
204 {
205  return false;
206 }
207 
208 //
209 
213 extern int MemoryAllocations();
214 
218 extern int ReportMemoryAllocations();
219 
220 }
221 #endif // __LOGOG_OBJECT_HPP
[Thread]
Definition: api.hpp:8
int ReportMemoryAllocations()
Definition: object.hpp:99
const T * const_pointer
Definition: object.hpp:109
void destroy(pointer p)
Definition: object.hpp:158
T * pointer
Definition: object.hpp:107
int MemoryAllocations()
pointer allocate(size_type n, const void *=0)
Definition: object.hpp:122
void deallocate(void *p, size_type)
Definition: object.hpp:129
LOGOG_UNORDERED_MAP< PointerType, size_t > AllocationsType
Definition: object.hpp:21
static void * Allocate(size_t nSize)
bool operator!=(const Allocator< T1 > &, const Allocator< T2 > &)
Definition: object.hpp:202
void * PointerType
Definition: object.hpp:16
void construct(pointer p, const T &val)
Definition: object.hpp:153
void UnlockAllocationsMutex()
virtual ~Object()
ptrdiff_t difference_type
Definition: object.hpp:105
T value_type
Definition: object.hpp:115
Allocator(const Allocator &)
Definition: object.hpp:119
pointer address(reference x) const
Definition: object.hpp:138
Allocator()
Definition: object.hpp:117
size_t size_type
Definition: object.hpp:103
AllocationsType s_Allocations
Definition: object.hpp:176
Allocator< T > & operator=(const Allocator &)
Definition: object.hpp:148
Allocator & operator=(const Allocator< U > &)
Definition: object.hpp:188
Definition: object.hpp:47
const T & const_reference
Definition: object.hpp:113
size_type max_size() const
Definition: object.hpp:168
static void Deallocate(void *ptr)
T & reference
Definition: object.hpp:111
void LockAllocationsMutex()
Allocator(const Allocator< U > &)
Definition: object.hpp:184
Allocator< U > other
Definition: object.hpp:179
bool operator==(const Allocator< T1 > &, const Allocator< T2 > &)
Definition: object.hpp:196
const_pointer address(const_reference x) const
Definition: object.hpp:143