120 Kernel( ): data_( ), counter_(1) { }
121 Kernel(Data
const& data): data_(data), counter_(1) { }
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_;
138 pointer_ = b.pointer_;
139 ++(pointer_->counter_);
143 --(pointer_->counter_);
144 if (pointer_->counter_ <= 0) {
148 int attach() {
return ++counter_; }
149 int detach() {
return --counter_; }
150 Data
const* access()
const {
151 return &(pointer_->data_);
154 if (pointer_->counter_ > 1) {
155 --pointer_->counter_;
156 pointer_ =
new Kernel(pointer_->data_);
158 return &(pointer_->data_);
165 if (body_->detach() <= 0) {
181 Self(Data
const& d): body_(new Body(d)) {
193 body_ =
new Body(*b.body_);
212 return body_->access();
219 return body_->modify();
224 return *((
Self*)
this);
234 if (body_->access() != s.body_->access()) {
235 body_->copyFrom(*s.body_);
247 if (body_ != s.body_) {
278 return (body_ == s.body_);
290 if (
same(s) || body_->access() == s.body_->access())
return true;
291 return (*body_->access() == *s.body_->access());
300 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