Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / memory / SharedPtr.hpp
1 #ifndef SASS_MEMORY_SHARED_PTR_H
2 #define SASS_MEMORY_SHARED_PTR_H
3
4 #include "sass/base.h"
5
6 #include <vector>
7
8 namespace Sass {
9
10   class SharedPtr;
11
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   ///////////////////////////////////////////////////////////////////////////////
16
17   #ifdef DEBUG_SHARED_PTR
18
19     #define SASS_MEMORY_NEW(Class, ...) \
20       ((new Class(__VA_ARGS__))->trace(__FILE__, __LINE__)) \
21
22     #define SASS_MEMORY_COPY(obj) \
23       ((obj)->copy(__FILE__, __LINE__)) \
24
25     #define SASS_MEMORY_CLONE(obj) \
26       ((obj)->clone(__FILE__, __LINE__)) \
27
28   #else
29
30     #define SASS_MEMORY_NEW(Class, ...) \
31       new Class(__VA_ARGS__) \
32
33     #define SASS_MEMORY_COPY(obj) \
34       ((obj)->copy()) \
35
36     #define SASS_MEMORY_CLONE(obj) \
37       ((obj)->clone()) \
38
39   #endif
40
41   class SharedObj {
42   protected:
43   friend class SharedPtr;
44   friend class Memory_Manager;
45     #ifdef DEBUG_SHARED_PTR
46       static std::vector<SharedObj*> all;
47       std::string file;
48       size_t line;
49     #endif
50     static bool taint;
51     long refcounter;
52     // long refcount;
53     bool detached;
54     #ifdef DEBUG_SHARED_PTR
55       bool dbg;
56     #endif
57   public:
58     #ifdef DEBUG_SHARED_PTR
59       static void dumpMemLeaks();
60       SharedObj* trace(std::string file, size_t line) {
61         this->file = file;
62         this->line = line;
63         return this;
64       }
65     #endif
66     SharedObj();
67     #ifdef DEBUG_SHARED_PTR
68       std::string getDbgFile() {
69         return file;
70       }
71       size_t getDbgLine() {
72         return line;
73       }
74       void setDbg(bool dbg) {
75         this->dbg = dbg;
76       }
77     #endif
78     static void setTaint(bool val) {
79       taint = val;
80     }
81     virtual ~SharedObj();
82     long getRefCount() {
83       return refcounter;
84     }
85   };
86
87
88   class SharedPtr {
89   private:
90     SharedObj* node;
91   private:
92     void decRefCount();
93     void incRefCount();
94   public:
95     // the empty constructor
96     SharedPtr()
97     : node(NULL) {};
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); */
108     // destructor
109     ~SharedPtr();
110   public:
111     SharedObj* obj () const {
112       return node;
113     };
114     SharedObj* operator-> () const {
115       return node;
116     };
117     bool isNull () {
118       return node == NULL;
119     };
120     bool isNull () const {
121       return node == NULL;
122     };
123     SharedObj* detach() const {
124       if (node) {
125         node->detached = true;
126       }
127       return node;
128     };
129     operator bool() const {
130       return node != NULL;
131     };
132
133   };
134
135   template < class T >
136   class SharedImpl : private SharedPtr {
137   public:
138     SharedImpl()
139     : SharedPtr(NULL) {};
140     SharedImpl(T* node)
141     : SharedPtr(node) {};
142     template < class U >
143     SharedImpl(SharedImpl<U> obj)
144     : SharedPtr(static_cast<T*>(obj.ptr())) {}
145     SharedImpl(T&& node)
146     : SharedPtr(node) {};
147     SharedImpl(const T& node)
148     : SharedPtr(node) {};
149     ~SharedImpl() {};
150   public:
151     operator T*() const {
152       return static_cast<T*>(this->obj());
153     }
154     operator T&() const {
155       return *static_cast<T*>(this->obj());
156     }
157     T& operator* () const {
158       return *static_cast<T*>(this->obj());
159     };
160     T* operator-> () const {
161       return static_cast<T*>(this->obj());
162     };
163     T* ptr () const {
164       return static_cast<T*>(this->obj());
165     };
166     T* detach() const {
167       if (this->obj() == NULL) return NULL;
168       return static_cast<T*>(SharedPtr::detach());
169     }
170     bool isNull() const {
171       return this->obj() == NULL;
172     }
173     bool operator<(const T& rhs) const {
174       return *this->ptr() < rhs;
175     };
176     operator bool() const {
177       return this->obj() != NULL;
178     };
179   };
180
181 }
182
183 #endif