1 #ifndef SASS_MEMORY_SHARED_PTR_H
2 #define SASS_MEMORY_SHARED_PTR_H
12 ///////////////////////////////////////////////////////////////////////////////
13 // Use macros for the allocation task, since overloading operator `new`
14 // has been proven to be flaky under certain compilers (see comment below).
15 ///////////////////////////////////////////////////////////////////////////////
17 #ifdef DEBUG_SHARED_PTR
19 #define SASS_MEMORY_NEW(Class, ...) \
20 ((new Class(__VA_ARGS__))->trace(__FILE__, __LINE__)) \
22 #define SASS_MEMORY_COPY(obj) \
23 ((obj)->copy(__FILE__, __LINE__)) \
25 #define SASS_MEMORY_CLONE(obj) \
26 ((obj)->clone(__FILE__, __LINE__)) \
30 #define SASS_MEMORY_NEW(Class, ...) \
31 new Class(__VA_ARGS__) \
33 #define SASS_MEMORY_COPY(obj) \
36 #define SASS_MEMORY_CLONE(obj) \
43 friend class SharedPtr;
44 friend class Memory_Manager;
45 #ifdef DEBUG_SHARED_PTR
46 static std::vector<SharedObj*> all;
54 #ifdef DEBUG_SHARED_PTR
58 #ifdef DEBUG_SHARED_PTR
59 static void dumpMemLeaks();
60 SharedObj* trace(std::string file, size_t line) {
67 #ifdef DEBUG_SHARED_PTR
68 std::string getDbgFile() {
74 void setDbg(bool dbg) {
78 static void setTaint(bool val) {
95 // the empty constructor
98 // the create constructor
99 SharedPtr(SharedObj* ptr);
100 // copy assignment operator
101 SharedPtr& operator=(const SharedPtr& rhs);
102 // move assignment operator
103 /* SharedPtr& operator=(SharedPtr&& rhs); */
104 // the copy constructor
105 SharedPtr(const SharedPtr& obj);
106 // the move constructor
107 /* SharedPtr(SharedPtr&& obj); */
111 SharedObj* obj () const {
114 SharedObj* operator-> () const {
120 bool isNull () const {
123 SharedObj* detach() const {
125 node->detached = true;
129 operator bool() const {
136 class SharedImpl : private SharedPtr {
139 : SharedPtr(NULL) {};
141 : SharedPtr(node) {};
143 SharedImpl(SharedImpl<U> obj)
144 : SharedPtr(static_cast<T*>(obj.ptr())) {}
146 : SharedPtr(node) {};
147 SharedImpl(const T& node)
148 : SharedPtr(node) {};
151 operator T*() const {
152 return static_cast<T*>(this->obj());
154 operator T&() const {
155 return *static_cast<T*>(this->obj());
157 T& operator* () const {
158 return *static_cast<T*>(this->obj());
160 T* operator-> () const {
161 return static_cast<T*>(this->obj());
164 return static_cast<T*>(this->obj());
167 if (this->obj() == NULL) return NULL;
168 return static_cast<T*>(SharedPtr::detach());
170 bool isNull() const {
171 return this->obj() == NULL;
173 bool operator<(const T& rhs) const {
174 return *this->ptr() < rhs;
176 operator bool() const {
177 return this->obj() != NULL;