Templates -- Meow  204.13.18
A C++ template contains kinds of interesting classes and functions
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  size_t counter_;
119  Body const* master_;
120 
121  Kernel(Body const* master):
122  data_(new Data( )), counter_(1), master_(master) {
123  }
124 
125  Kernel(Body const* master, Data const& d):
126  data_(new Data(d)), counter_(1), master_(master) {
127  }
128 
129  ~Kernel() {
130  }
131  };
132 
133  Kernel* pointer_;
134  size_t counter_;
135 
136  void clear() {
137  --(pointer_->counter_);
138  if (pointer_->counter_ <= 0) {
139  delete pointer_;
140  }
141  }
142  public:
143  Body( ): pointer_(new Kernel(this )), counter_(1) { }
144  Body(Data const& d): pointer_(new Kernel(this, d)), counter_(1) { }
145  Body(Body const& b): pointer_(b.pointer_ ), counter_(1) {
146  ++(pointer_->counter_);
147  }
148 
149  ~Body() {
150  clear();
151  }
152 
153  Body& copyFrom(Body const& b) {
154  clear();
155  pointer_ = b.pointer_;
156  ++(pointer_->counter_);
157  return *this;
158  }
159 
160  Data const* access() const {
161  return pointer_->data_;
162  }
163 
164  Data* modify() {
165  if (pointer_->counter_ > 1) {
166  --(pointer_->counter_);
167  Kernel* dupl = new Kernel(this, *pointer_->data_);
168  if (pointer_->master_ == this || pointer_->master_ == NULL) {
169  std::swap(pointer_->pointer_, dupl->pointer_);
170  pointer_->master_ = NULL;
171  }
172  pointer_ = dupl;
173  }
174  else if (pointer_->master_ == NULL) {
175  pointer_->master_ = this;
176  }
177  return pointer_->data_;
178  }
179 
180  int attach() {
181  return ++counter_;
182  }
183 
184  int detach() {
185  return --counter_;
186  }
187  };
188 
189  Body* body_;
190 
191  void clear() {
192  if (body_->detach() <= 0) {
193  delete body_;
194  }
195  }
196 public:
200  Self(): body_(new Body()) {
201  }
202 
208  Self(Data const& d): body_(new Body(d)) {
209  }
210 
217  Self(Self const& b, DuplicateType d) {
218  switch(d) {
219  case COPY_FROM:
220  body_ = new Body(*b.body_);
221  break;
222  case REFERENCE_FROM:
223  body_ = b.body_;
224  body_->attach();
225  break;
226  }
227  }
228 
230  Self(Self const& b);
231 
233  ~Self() {
234  clear();
235  }
236 
238  Data const* operator->() const {
239  return body_->access();
240  }
241 
245  Data* operator->() {
246  return body_->modify();
247  }
248 
250  Self& operator()() const {
251  return *((Self*)this);
252  }
253 
260  Self const& copyFrom(Self const& s) {
261  if (body_->access() != s.body_->access()) {
262  body_->copyFrom(*s.body_);
263  }
264  return *this;
265  }
266 
273  Self const& referenceFrom(Self const& s) {
274  if (body_ != s.body_) {
275  clear();
276  body_ = s.body_;
277  body_->attach();
278  }
279  return *this;
280  }
281 
289  Self const& duplicateFrom(Self const& s, DuplicateType t) {
290  switch(t) {
291  case COPY_FROM : return copyFrom(s);
292  case REFERENCE_FROM: return referenceFrom(s);
293  }
294  return *this;
295  }
296 
304  bool same(Self const& s) const {
305  return (body_ == s.body_);
306  }
307 
316  bool equal(Self const& s) const {
317  if (same(s) || body_->access() == s.body_->access()) return true;
318  return (*body_->access() == *s.body_->access());
319  }
320 
326  bool referenceLess(Self const& s) const {
327  return (body_ < s.body_);
328  }
329 
331  void operator=(Self const& a);
332 };
333 
334 } // meow
335 
336 #endif // Self_h__
Normal copy operation.
Definition: Self.h:110
Data const * operator->() const
Return the constant pointer to the data.
Definition: Self.h:238
Self const & copyFrom(Self const &s)
Copy the gived Self to myself.
Definition: Self.h:260
bool referenceLess(Self const &s) const
Order compare by reference pointer.
Definition: Self.h:326
Self(Self const &b, DuplicateType d)
constructor with given another Self
Definition: Self.h:217
void operator=(Self const &a)
Disallow default 'operator='.
Self()
constructor with a real entity
Definition: Self.h:200
Data * operator->()
Return the non-constant pointer to the data (COR's clone might occure here.
Definition: Self.h:245
~Self()
destructor
Definition: Self.h:233
bool same(Self const &s) const
Compare tht if the gived Self object is reference from the same object of me.
Definition: Self.h:304
bool equal(Self const &s) const
Compare that the data are the same.
Definition: Self.h:316
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:250
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:273
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:289
Self(Data const &d)
connstructor with a real entity with it using its copy constructor
Definition: Self.h:208