aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/Self.h
blob: 94db352961875713910579344771069f98aa2b99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef   Self_h__
#define   Self_h__

#include <cstdlib>
#include <algorithm>

namespace meow {

/*!
 *@brief 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.
 *
 * Sample code:
 *@code{.cpp}
 * class A {
 * private:
 *   struct Myself {
 *     int data;
 *
 *     Myself() {                               // Necessary
 *       data = 0;
 *     }
 *
 *     Myself(Myself const& b): data(b.data) {  // Necessary, copy constructor
 *     }
 *
 *     ~Myself() {
 *     }
 *
 *     bool operator==(Myself const& b) const { // Optional (this method will
 *                                              // be called only if you use
 *                                              // Self::equal() method)
 *       return (data == b.data);
 *     }
 *   };
 *
 *   Self<Myself> const self;          // Here we use 'constant' data type in
 *                                     // order to have a coutious coding style
 *                                     // and allow the COR mechanism to clone
 *                                     // data only when we really want to
 *                                     // modify them.
 * public:
 *   A(): self() { }                            // Default constructor
 *
 *   A(A const& a): self(a.self, COPY_FROM) { } // Copy constructor. You must
 *                                              // tell me which way of
 *                                              // duplicating should I use.
 *                                              // It strongly recommended you
 *                                              // use COYP_FROM for keeping the
 *                                              // C++'s original behavior.
 *   ~A() { }
 *
 *   int getMemember(int wh) const {
 *     return self->data;   // Use 'operator->()' to get the pointer of the data
 *                          // The pointer is constant or not will depend on
 *                          // whether the left side variable of '->' is
 *                          // constant or not.
 *                          // If we just want to read the data, use
 *                          // 'self' instead of 'self()'
 *   }
 *   void setMemeber(int k) {
 *     self()->data = k;    // As a result of 'self()' returning a non-constant
 *                          // reference of itself, here we get the permission
 *                          // for modiying data.
 *                          // So now we can observe that if you type
 *                          // 'Self<Myself> self' instead of the one above,
 *                          // 'self' and 'self()' will become the same one and
 *                          // both of them allow you using '->' for getting
 *                          // writing permission. At the same time, the COR
 *                          // machanism will become useless because everytime
 *                          // you want to access the date, Self will copy the
 *                          // data to prevent you to modify it no matter that
 *                          // you might just want to read it.
 *   }
 *
 *   A referenceFrom(A const& a) {
 *     self.referenceFrom(a.self);
 *   }
 *
 *   A copyFrom(A const& a) {
 *     self.copyFrom(a.self);
 *   }
 *
 *   A& operator=(A const& b) { // If you really like to use operator=, it
 *                              // strongly recommended you use 'copyFrom()' for
 *                              // keeping C++'s original behavior.
 *     copyFrom(b);
 *   }
 * };
 *@endcode
 * Note that 'referenceFrom()' will cause the two object become the same one,
 * Which means that if you do something like '\c a.referenceFrom(b);
 * \c a.copyFrom(c); ', the result is that the value of \c a,b,c will all the
 * same one.
 *
 *@author cathook
 *
 *@warning This class disabled the method \c operator= and copy constructor
 *         in order to prevent upexplicit default behavior, so if you want
 *         to have one of them (or both), you must implement yourself
 */
template<class Data>
class Self {
public:
  /*!
   * @brief Kind of ways of duplicating
   */
  enum DuplicateType {
    COPY_FROM,          //!< Normal copy operation
    REFERENCE_FROM      //!< By reference, much like pointer's copy operation
  };
private:
  class Body {
  private:
    struct Kernel {
      Data*          data_;
      size_t      counter_;
      Body const*  master_;

      Kernel(Body const* master):
      data_(new Data( )), counter_(1), master_(master) {
      }

      Kernel(Body const* master, Data const& d):
      data_(new Data(d)), counter_(1), master_(master) {
      }

      ~Kernel() {
      }
    };

    Kernel* pointer_;
    size_t  counter_;

    void clear() {
      --(pointer_->counter_);
      if (pointer_->counter_ <= 0) {
        delete pointer_;
      }
    }
  public:
    Body(             ): pointer_(new Kernel(this   )), counter_(1) { }
    Body(Data const& d): pointer_(new Kernel(this, d)), counter_(1) { }
    Body(Body const& b): pointer_(b.pointer_         ), counter_(1) {
      ++(pointer_->counter_);
    }

    ~Body() {
      clear();
    }

    Body& copyFrom(Body const& b) {
      clear();
      pointer_ = b.pointer_;
      ++(pointer_->counter_);
      return *this;
    }

    Data const* access() const {
      return pointer_->data_;
    }

    Data* modify() {
      if (pointer_->counter_ > 1) {
        --(pointer_->counter_);
        Kernel* dupl = new Kernel(this, *pointer_->data_);
        if (pointer_->master_ == this || pointer_->master_ == NULL) {
          std::swap(pointer_->data_, dupl->data_);
          pointer_->master_ = NULL;
        }
        pointer_ = dupl;
      }
      else if (pointer_->master_ == NULL) {
        pointer_->master_ = this;
      }
      return pointer_->data_;
    }

    int attach() {
      return ++counter_;
    }

    int detach() {
      return --counter_;
    }
  };

  Body* body_;

  void clear() {
    if (body_->detach() <= 0) {
      delete body_;
    }
  }
public:
  /*!
   * @brief constructor with a real entity
   */
  Self(): body_(new Body()) {
  }

  /*!
   * @brief connstructor with a real entity with it using its copy constructor
   *
   * @param [in] d Inital data
   */
  Self(Data const& d): body_(new Body(d)) {
  }

  /*!
   * @brief constructor with given another Self
   *
   * @param [in] b Another Self object.
   * @param [in] d To indicate type of way of duplicating
   */
  Self(Self const& b, DuplicateType d) {
    switch(d) {
    case COPY_FROM:
      body_ = new Body(*b.body_);
      break;
    case REFERENCE_FROM:
      body_ = b.body_;
      body_->attach();
      break;
    }
  }

  //! @brief Disallow copy constructor
  Self(Self const& b);

  //! @brief destructor
  ~Self() {
    clear();
  }

  //! @brief Return the constant pointer to the data
  Data const* operator->() const {
    return body_->access();
  }

  /*! @brief Return the non-constant pointer to the data (COR's clone might
   *         occure here.
   */
  Data* operator->() {
    return body_->modify();
  }

  //! @brief Return the non-constant reference of \c *this
  Self& operator()() const {
    return *((Self*)this);
  }

  /*!
   * @brief Copy the gived \c Self to myself
   *
   * @param [in] s gived \c Self
   * @return *this
   */
  Self const& copyFrom(Self const& s) {
    if (body_->access() != s.body_->access()) {
      body_->copyFrom(*s.body_);
    }
    return *this;
  }

  /*!
   * @brief Reference myself from given \c Self object.
   *
   * @param [in] s given \c Self
   * @return *this
   */
  Self const& referenceFrom(Self const& s) {
    if (body_ != s.body_) {
      clear();
      body_ = s.body_;
      body_->attach();
    }
    return *this;
  }

  /*!
   * @brief call \c copyFrom() or \c referenceFrom() depend on your instruction
   *
   * @param [in] s gived \c Self object
   * @param [in] t instruction
   * @return *this
   */
  Self const& duplicateFrom(Self const& s, DuplicateType t) {
    switch(t) {
    case COPY_FROM     : return      copyFrom(s);
    case REFERENCE_FROM: return referenceFrom(s);
    }
    return *this;
  }

  /*!
   * @brief Compare tht if the gived \c Self object is reference from the same
   *        object of me
   *
   * @param [in] s gived \c Self object
   * @return \c true if we are referenced to the same object.
   */
  bool same(Self const& s) const {
    return (body_ == s.body_);
  }

  /*!
   * @brief Compare that the data are the same.
   *
   * @param [in] s another \c Self object
   * @return \c true if the data are same.
   *
   * @note This will need the method 'Data::equal()'
   */
  bool equal(Self const& s) const {
    if (same(s) || body_->access() == s.body_->access()) return true;
    return (*body_->access() == *s.body_->access());
  }

  /*!
   * @brief Order compare by reference pointer.
   *
   * @param [in] s another \c Self object
   */
  bool referenceLess(Self const& s) const {
    return (body_ < s.body_);
  }

  //! @brief Disallow default \c 'operator='
  void operator=(Self const& a);
};

} // meow

#endif // Self_h__