Templates -- Meow  204.13.18
A C++ template contains kinds of interesting classes and functions
meow::Self< Data > Class Template Reference

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. More...

#include "Self.h"

Public Types

enum  DuplicateType { COPY_FROM, REFERENCE_FROM }
 Kind of ways of duplicating. More...
 

Public Member Functions

 Self ()
 constructor with a real entity More...
 
 Self (Data const &d)
 connstructor with a real entity with it using its copy constructor More...
 
 Self (Self const &b, DuplicateType d)
 constructor with given another Self More...
 
 Self (Self const &b)
 Disallow copy constructor. More...
 
 ~Self ()
 destructor More...
 
Data const * operator-> () const
 Return the constant pointer to the data. More...
 
Data * operator-> ()
 Return the non-constant pointer to the data (COR's clone might occure here. More...
 
Selfoperator() () const
 Return the non-constant reference of *this. More...
 
Self const & copyFrom (Self const &s)
 Copy the gived Self to myself. More...
 
Self const & referenceFrom (Self const &s)
 Reference myself from given Self object. More...
 
Self const & duplicateFrom (Self const &s, DuplicateType t)
 call copyFrom() or referenceFrom() depend on your instruction More...
 
bool same (Self const &s) const
 Compare tht if the gived Self object is reference from the same object of me. More...
 
bool equal (Self const &s) const
 Compare that the data are the same. More...
 
bool referenceLess (Self const &s) const
 Order compare by reference pointer. More...
 
void operator= (Self const &a)
 Disallow default 'operator='. More...
 

Detailed Description

template<class Data>
class meow::Self< Data >

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:

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.
}
};

Note that 'referenceFrom()' will cause the two object become the same one, Which means that if you do something like 'a.referenceFrom(b); a.copyFrom(c); ', the result is that the value of a,b,c will all the same one.

Author
cathook
Warning
This class disabled the method 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

Definition at line 104 of file Self.h.

Member Enumeration Documentation

template<class Data>
enum meow::Self::DuplicateType

Kind of ways of duplicating.

Enumerator
COPY_FROM 

Normal copy operation.

REFERENCE_FROM 

By reference, much like pointer's copy operation.

Definition at line 109 of file Self.h.

Constructor & Destructor Documentation

template<class Data>
meow::Self< Data >::Self ( )
inline

constructor with a real entity

Definition at line 200 of file Self.h.

template<class Data>
meow::Self< Data >::Self ( Data const &  d)
inline

connstructor with a real entity with it using its copy constructor

Parameters
[in]dInital data

Definition at line 208 of file Self.h.

template<class Data>
meow::Self< Data >::Self ( Self< Data > const &  b,
DuplicateType  d 
)
inline

constructor with given another Self

Parameters
[in]bAnother Self object.
[in]dTo indicate type of way of duplicating

Definition at line 217 of file Self.h.

template<class Data>
meow::Self< Data >::Self ( Self< Data > const &  b)

Disallow copy constructor.

template<class Data>
meow::Self< Data >::~Self ( )
inline

destructor

Definition at line 233 of file Self.h.

Member Function Documentation

template<class Data>
Self const& meow::Self< Data >::copyFrom ( Self< Data > const &  s)
inline

Copy the gived Self to myself.

Parameters
[in]sgived Self
Returns
*this

Definition at line 260 of file Self.h.

template<class Data>
Self const& meow::Self< Data >::duplicateFrom ( Self< Data > const &  s,
DuplicateType  t 
)
inline

call copyFrom() or referenceFrom() depend on your instruction

Parameters
[in]sgived Self object
[in]tinstruction
Returns
*this

Definition at line 289 of file Self.h.

template<class Data>
bool meow::Self< Data >::equal ( Self< Data > const &  s) const
inline

Compare that the data are the same.

Parameters
[in]sanother Self object
Returns
true if the data are same.
Note
This will need the method 'Data::equal()'

Definition at line 316 of file Self.h.

template<class Data>
Self& meow::Self< Data >::operator() ( ) const
inline

Return the non-constant reference of *this.

Definition at line 250 of file Self.h.

template<class Data>
Data const* meow::Self< Data >::operator-> ( ) const
inline

Return the constant pointer to the data.

Definition at line 238 of file Self.h.

template<class Data>
Data* meow::Self< Data >::operator-> ( )
inline

Return the non-constant pointer to the data (COR's clone might occure here.

Definition at line 245 of file Self.h.

template<class Data>
void meow::Self< Data >::operator= ( Self< Data > const &  a)

Disallow default 'operator='.

template<class Data>
Self const& meow::Self< Data >::referenceFrom ( Self< Data > const &  s)
inline

Reference myself from given Self object.

Parameters
[in]sgiven Self
Returns
*this

Definition at line 273 of file Self.h.

template<class Data>
bool meow::Self< Data >::referenceLess ( Self< Data > const &  s) const
inline

Order compare by reference pointer.

Parameters
[in]sanother Self object

Definition at line 326 of file Self.h.

template<class Data>
bool meow::Self< Data >::same ( Self< Data > const &  s) const
inline

Compare tht if the gived Self object is reference from the same object of me.

Parameters
[in]sgived Self object
Returns
true if we are referenced to the same object.

Definition at line 304 of file Self.h.


The documentation for this class was generated from the following file: