121 Kernel(Body
const* master):
122 data_(
new Data( )), counter_(1), master_(master) {
125 Kernel(Body
const* master, Data
const& d):
126 data_(
new Data(d)), counter_(1), master_(master) {
137 --(pointer_->counter_);
138 if (pointer_->counter_ <= 0) {
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_);
155 pointer_ = b.pointer_;
156 ++(pointer_->counter_);
160 Data
const* access()
const {
161 return pointer_->data_;
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;
174 else if (pointer_->master_ == NULL) {
175 pointer_->master_ =
this;
177 return pointer_->data_;
192 if (body_->detach() <= 0) {
208 Self(Data
const& d): body_(new Body(d)) {
220 body_ =
new Body(*b.body_);
239 return body_->access();
246 return body_->modify();
251 return *((
Self*)
this);
261 if (body_->access() != s.body_->access()) {
262 body_->copyFrom(*s.body_);
274 if (body_ != s.body_) {
305 return (body_ == s.body_);
317 if (
same(s) || body_->access() == s.body_->access())
return true;
318 return (*body_->access() == *s.body_->access());
327 return (body_ < s.body_);
Data const * operator->() const
Return the constant pointer to the data.
Self const & copyFrom(Self const &s)
Copy the gived Self to myself.
bool referenceLess(Self const &s) const
Order compare by reference pointer.
Self(Self const &b, DuplicateType d)
constructor with given another Self
void operator=(Self const &a)
Disallow default 'operator='.
Self()
constructor with a real entity
Data * operator->()
Return the non-constant pointer to the data (COR's clone might occure here.
bool same(Self const &s) const
Compare tht if the gived Self object is reference from the same object of me.
bool equal(Self const &s) const
Compare that the data are the same.
By reference, much like pointer's copy operation.
Self & operator()() const
Return the non-constant reference of *this.
DuplicateType
Kind of ways of duplicating.
Self const & referenceFrom(Self const &s)
Reference myself from given Self object.
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.
Self const & duplicateFrom(Self const &s, DuplicateType t)
call copyFrom() or referenceFrom() depend on your instruction
Self(Data const &d)
connstructor with a real entity with it using its copy constructor