Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
Self.h
Go to the documentation of this file.
1 #ifndef Self_h__
2 #define Self_h__
3 
4 #include <cstdlib>
5 
6 namespace meow {
7 
103 template<class Data>
104 class Self {
105 public:
112  };
113 private:
114  class Body {
115  private:
116  struct Kernel {
117  Data data_;
118  int counter_;
119 
120  Kernel( ): data_( ), counter_(1) { }
121  Kernel(Data const& data): data_(data), counter_(1) { }
122  ~Kernel() { }
123  };
124 
125  Kernel* pointer_;
126  int counter_;
127  public:
128  Body( ): pointer_(new Kernel( )), counter_(1) { }
129  Body(Data const& d): pointer_(new Kernel(d)), counter_(1) { }
130  Body(Body const& b): pointer_(b.pointer_ ), counter_(1) {
131  ++pointer_->counter_;
132  }
133  ~Body() {
134  clear();
135  }
136  Body& copyFrom(Body const& b) {
137  clear();
138  pointer_ = b.pointer_;
139  ++(pointer_->counter_);
140  return *this;
141  }
142  void clear() {
143  --(pointer_->counter_);
144  if (pointer_->counter_ <= 0) {
145  delete pointer_;
146  }
147  }
148  int attach() { return ++counter_; }
149  int detach() { return --counter_; }
150  Data const* access() const {
151  return &(pointer_->data_);
152  }
153  Data* modify() {
154  if (pointer_->counter_ > 1) {
155  --pointer_->counter_;
156  pointer_ = new Kernel(pointer_->data_);
157  }
158  return &(pointer_->data_);
159  }
160  };
161 
162  Body* body_;
163 
164  void clear() {
165  if (body_->detach() <= 0) {
166  delete body_;
167  }
168  }
169 public:
173  Self(): body_(new Body()) {
174  }
175 
181  Self(Data const& d): body_(new Body(d)) {
182  }
183 
190  Self(Self const& b, DuplicateType d) {
191  switch(d) {
192  case COPY_FROM:
193  body_ = new Body(*b.body_);
194  break;
195  case REFERENCE_FROM:
196  body_ = b.body_;
197  body_->attach();
198  break;
199  }
200  }
201 
203  Self(Self const& b);
204 
206  ~Self() {
207  clear();
208  }
209 
211  Data const* operator->() const {
212  return body_->access();
213  }
214 
218  Data* operator->() {
219  return body_->modify();
220  }
221 
223  Self& operator()() const {
224  return *((Self*)this);
225  }
226 
233  Self const& copyFrom(Self const& s) {
234  if (body_->access() != s.body_->access()) {
235  body_->copyFrom(*s.body_);
236  }
237  return *this;
238  }
239 
246  Self const& referenceFrom(Self const& s) {
247  if (body_ != s.body_) {
248  clear();
249  body_ = s.body_;
250  body_->attach();
251  }
252  return *this;
253  }
254 
262  Self const& duplicateFrom(Self const& s, DuplicateType t) {
263  switch(t) {
264  case COPY_FROM : return copyFrom(s);
265  case REFERENCE_FROM: return referenceFrom(s);
266  }
267  return *this;
268  }
269 
277  bool same(Self const& s) const {
278  return (body_ == s.body_);
279  }
280 
289  bool equal(Self const& s) const {
290  if (same(s) || body_->access() == s.body_->access()) return true;
291  return (*body_->access() == *s.body_->access());
292  }
293 
299  bool referenceLess(Self const& s) const {
300  return (body_ < s.body_);
301  }
302 
304  void operator=(Self const& a);
305 };
306 
307 } // meow
308 
309 #endif // Self_h__
Normal copy operation.
Definition: Self.h:110
Data const * operator->() const
Return the constant pointer to the data.
Definition: Self.h:211
Self const & copyFrom(Self const &s)
Copy the gived Self to myself.
Definition: Self.h:233
bool referenceLess(Self const &s) const
Order compare by reference pointer.
Definition: Self.h:299
Self(Self const &b, DuplicateType d)
constructor with given another Self
Definition: Self.h:190
void operator=(Self const &a)
Disallow default 'operator='.
Self()
constructor with a real entity
Definition: Self.h:173
Data * operator->()
Return the non-constant pointer to the data (COR's clone might occure here.
Definition: Self.h:218
~Self()
destructor
Definition: Self.h:206
bool same(Self const &s) const
Compare tht if the gived Self object is reference from the same object of me.
Definition: Self.h:277
bool equal(Self const &s) const
Compare that the data are the same.
Definition: Self.h:289
By reference, much like pointer's copy operation.
Definition: Self.h:111
Self & operator()() const
Return the non-constant reference of *this.
Definition: Self.h:223
DuplicateType
Kind of ways of duplicating.
Definition: Self.h:109
Self const & referenceFrom(Self const &s)
Reference myself from given Self object.
Definition: Self.h:246
A little class use for packing the data part of another class. With this technique, it can achieve Copy-On-Write(COR) mechanism at background and have a reference mechanism which much more flexible then the one C++ has.
Definition: Self.h:104
Self const & duplicateFrom(Self const &s, DuplicateType t)
call copyFrom() or referenceFrom() depend on your instruction
Definition: Self.h:262
Self(Data const &d)
connstructor with a real entity with it using its copy constructor
Definition: Self.h:181