Oxygine  1
2g game engine
Object.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include "ref_counter.h"
4 #include <string>
5 #include <vector>
6 
7 namespace oxygine
8 {
9  class PoolObject
10  {
11  public:
12 #if OBJECT_POOL_ALLOCATOR
13  void* operator new(size_t size);
14  void operator delete(void* data, size_t size);
15 #endif
16  };
17 
19  class ObjectBase: public PoolObject
20  {
21  public:
22  ObjectBase(const ObjectBase& src);
23  ObjectBase(bool assignID = true);
24  virtual ~ObjectBase();
25 
26  const std::string& getName() const;
27  const void* getUserData() const {return __userData;}
28  int getObjectID()const {return __id;}
29  bool isName(const std::string& name) const;
30  bool isName(const char* name) const;
31 
32 
33 
34  void setName(const std::string& name);
35  void setUserData(const void* data) {__userData = data;}
36 
37  void dumpObject() const;
38 
40  static void dumpCreatedObjects();
42  static void showAssertInCtor(int id);
44  static void showAssertInDtor(int id);
45 
46  //debug functions
47  typedef std::vector<ObjectBase*> __createdObjects;
48  static __createdObjects& __getCreatedObjects();
49 
50  static void __startTracingLeaks();
51  static void __stopTracingLeaks();
52  void __removeFromDebugList();
53 
54 
55  protected:
56  static void __addToDebugList(ObjectBase* base);
57  static void __removeFromDebugList(ObjectBase* base);
58  void __generateID();
59 
60 #if DYNAMIC_OBJECT_NAME
61  //some objects dont need name
62  mutable std::string* __name;
63 #else
64  mutable std::string __name;
65 #endif
66  std::string* __getName() const;
67  void __freeName() const;
68 
69  int __id;
70  const void* __userData;
71 
72 
73 #ifdef OXYGINE_DEBUG_TRACE_LEAKS
74  bool __traceLeak;
75 #endif
76 
77  static int _lastID;
78  static int _assertCtorID;
79  static int _assertDtorID;
80  };
81 
82  DECLARE_SMART(Object, spObject);
83  class Object: public ref_counter, public ObjectBase
84  {
85  public:
86  Object(const Object& src);
87  Object(bool assignUniqueID = true);
88 
89  protected:
90 #ifdef OX_DEBUG
91  unsigned int __check;
92  void __doCheck();
93 #else
94  void __doCheck() {}
95 #endif
96  };
97 
98 
99  template <class dest, class src>
100  dest safeCast(src ptr)
101  {
102  if (!ptr)
103  return 0;
104 #ifdef OXYGINE_DEBUG_SAFECAST
105  dest cast = dynamic_cast<dest>(ptr);
106  OX_ASSERT(cast && "can't cast pointer");
107  return cast;
108 #else
109  return static_cast<dest>(ptr);
110 #endif
111  }
112 
113  template<class T, class U>
114  intrusive_ptr<T> safeSpCast(intrusive_ptr<U> const& p)
115  {
116  if (!p)
117  return 0;
118 #ifdef OXYGINE_DEBUG_SAFECAST
119  intrusive_ptr<T> t = dynamic_cast<T*>(p.get());
120  OX_ASSERT(t && "can't cast pointer");
121 #endif
122  return static_cast<T*>(p.get());
123  }
124 }
125 
126 EDITOR_INCLUDE(Object);
static void dumpCreatedObjects()
Definition: Object.h:19
static void showAssertInDtor(int id)
Definition: Object.h:9
Definition: ref_counter.h:8
–oxgl-end–!
Definition: Actor.h:14
static void showAssertInCtor(int id)
Definition: Object.h:83