Templates -- Meow  2.0.-1
A C++ template contains kinds of interesting classes and functions
meow::Self< SelfType > Class Template Reference

An implementation for the reference mechanism. More...

#include "self.h"

Public Member Functions

 Self ()
 Creates a new one. More...
 
 Self (SelfType const &arg_body)
 Creates a new one with specifying a initial value for SelfType object. More...
 
 Self (Self const &arg_another_self)
 References from another one. More...
 
 ~Self ()
 Detatches. More...
 
Self const & ReferenceFrom (Self const &arg_another_self)
 References from another Self object. More...
 
Self const & CopyFrom (Self const &arg_another_self)
 Copies the data in SelfType object from another Self object. More...
 
Self Copy () const
 Creates a copy one of itself. More...
 
bool Is (Self const &arg_another_self) const
 Checks whether the gived instance of Self references from the same SelfType with me or not. More...
 
SelfType * operator-> ()
 Access the instance of SelfType by address. More...
 
SelfType const * operator-> () const
 Access the instance of SelfType by constant address. More...
 
Selfoperator() () const
 Access itself in non-constant mode. More...
 
Selfoperator= (Self const &b)
 Disallows the "operator=" so develops need to explicitly use CopyFrom/RefernceFrom. More...
 

Detailed Description

template<typename SelfType>
class meow::Self< SelfType >

An implementation for the reference mechanism.

Some example code:

class A {
private:
// Data members of this class. You can also move the methods of class A
// into this structure if you like.
struct DataMember {
int var1;
int var2;
int counter;
// The constructor "Self<DataMember>::Self()" will call the constructor
// "DataMember::DataMember()"
DataMember() : var1(0), var2(0), counter(0) {}
// The constructor "Self<DataMember>::Self(DataMember const&)" will call
// the constructor "DataMember::DataMember(DataMember const&)"
DataMember(DataMember const& b) :
var1(b.var1), var2(b.var2), counter(0) {}
// Customize constructor.
DataMember(int var1_init_value) :
var1(var1_init_value), var2(0), counter(0) {}
// Destructor, will be called when nobody references to it.
~DataMember() {}
// The "Self<DataMember>::CopyFrom(Self<DataMember> const&)" will call
// "DataMember::CopyFrom(DataMember const&)" to copy data.
DataMember const& CopyFrom(DataMember const& b) {
var1 = b.var1;
var2 = b.var2;
}
};
meow::Self<DataMember> const self_; // Use constant type to reduce the
// protential error caused by typing
// wrong.
public:
// Here the "Self<DataMember>::Self()" will be called.
A() {}
// Notice! "Self<DataMember>::Self(Self const& another_self)" will let
// itself reference to the gived object instead of creating a new one.
// So here the copy constructor will not copy from the gived object, it
// will just reference from it.
A(A const& another_class_a) : self_(another_class_a.self_) {}
// Customize constructor.
A(int var1_init_value) : self_(DataMember(var1_init_value)) {}
// You don't need to call some extra function in destructor, because
// there's a counter in the Self class.
~A() {}
// A constant method.
int GetVar1() const {
return self_->var1; // Use the operator "->" to access the address of
// the DataMember. Because we declare self_ be a
// constant type, here "self_->var1" will also be
// a constant.
}
// A non-constant method.
void SetVar1(int new_value) {
int old_value = self_->var1;
self_()->var1 = new_value; // Use operator "()" (i.e. self_()) to
// access the object with non-constant type,
// so here "self_()->var1" will be a
// non-constant variable.
if (old_value != new_value) {
self_()->var2 = old_value;
//self_->var2 = old_value; // !! It cause an error because operator "()"
// is missed if you want to modify the member
// in the DataMember.
}
}
int GetVar2() const {
self_()->counter += 1; // !! It will not cause an error. Actually,
// the "const" keyword of a method will become
// more meanless, because inside the method, you
// can just use self_-> or self_()-> to determind
// whether you want to access the DataMember in
// constant mode or not. It might be dangerous
// but giving developer more flexable.
return self_->var2;
}
};

Definition at line 117 of file self.h.

Constructor & Destructor Documentation

template<typename SelfType>
meow::Self< SelfType >::Self ( )
inline

Creates a new one.

Definition at line 152 of file self.h.

template<typename SelfType>
meow::Self< SelfType >::Self ( SelfType const &  arg_body)
inline

Creates a new one with specifying a initial value for SelfType object.

Parameters
arg_bodyThe initial value of the SelfType object.

Definition at line 159 of file self.h.

template<typename SelfType>
meow::Self< SelfType >::Self ( Self< SelfType > const &  arg_another_self)
inline

References from another one.

Parameters
arg_another_selfAnother Self object.

Definition at line 166 of file self.h.

template<typename SelfType>
meow::Self< SelfType >::~Self ( )
inline

Detatches.

It will automatically clear the SelfType object when no one refernece from it.

Definition at line 174 of file self.h.

Member Function Documentation

template<typename SelfType>
Self meow::Self< SelfType >::Copy ( ) const
inline

Creates a copy one of itself.

Definition at line 198 of file self.h.

template<typename SelfType>
Self const& meow::Self< SelfType >::CopyFrom ( Self< SelfType > const &  arg_another_self)
inline

Copies the data in SelfType object from another Self object.

Parameters
arg_another_selfAnother Self object.

Definition at line 190 of file self.h.

template<typename SelfType>
bool meow::Self< SelfType >::Is ( Self< SelfType > const &  arg_another_self) const
inline

Checks whether the gived instance of Self references from the same SelfType with me or not.

Parameters
arg_another_selfAnother instance of Self.
Returns
true if we references from the same thing.

Definition at line 206 of file self.h.

template<typename SelfType>
Self& meow::Self< SelfType >::operator() ( ) const
inline

Access itself in non-constant mode.

Definition at line 227 of file self.h.

template<typename SelfType>
SelfType* meow::Self< SelfType >::operator-> ( )
inline

Access the instance of SelfType by address.

Definition at line 213 of file self.h.

template<typename SelfType>
SelfType const* meow::Self< SelfType >::operator-> ( ) const
inline

Access the instance of SelfType by constant address.

Definition at line 220 of file self.h.

template<typename SelfType>
Self& meow::Self< SelfType >::operator= ( Self< SelfType > const &  b)

Disallows the "operator=" so develops need to explicitly use CopyFrom/RefernceFrom.

template<typename SelfType>
Self const& meow::Self< SelfType >::ReferenceFrom ( Self< SelfType > const &  arg_another_self)
inline

References from another Self object.

Parameters
arg_another_selfAnother Self object.

Definition at line 180 of file self.h.


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