#include <ref.h>
Collaboration diagram for ref:
Public Methods | |
ref () | |
default constructor makes null ref<T>. More... | |
ref (const ref< T > &r) | |
copy constructor increments reference count. More... | |
~ref () | |
destructor decrements reference count or deletes underlying pointer. More... | |
operator bool () const | |
is this a null ref<T>? More... | |
T & | operator * () const |
ref<T> objects can be dereferenced like a pointer. More... | |
T * | operator-> () const |
ref<T> objects support -> like pointers. More... | |
ref< T > & | operator= (const ref< T > &r) |
assignment decerements reference count of lhs, increases count for rhs. More... | |
bool | operator== (const ref< T > &r) const |
== compares underlying T objects. More... | |
bool | operator< (const ref< T > &r) const |
< compares underlying T objects. More... | |
Protected Methods | |
ref (T *t) | |
only T objects can encapsulate a (new'ed) pointer into a ref<T>. More... | |
Private Attributes | |
friend | T |
only T can produce non-null ref<T> objects. More... | |
T * | t_ |
unsigned int * | n_ |
Example usage:
#include <iostream> #include <string> #include <dvutil/ref.h> using Dv::ref; class Huge { friend class ref<Huge>; public: friend ostream& operator<<(ostream& os, const Huge& h) { return os << h._s; } const string& s() const { return _s; } static Dv::ref<Huge> create(const char *s) { return ref<Huge>(new Huge(s)); } private: string _s; Huge(const char* s): _s(s) {} ~Huge(); }; int main(int argc,char *argv[]) { ref<Huge> r = Huge::create("c++"); ref<Huge> p = r; cout << *p << p->s() << endl; }
Definition at line 35 of file ref.h.
|
default constructor makes null ref<T>.
|
|
copy constructor increments reference count.
|
|
destructor decrements reference count or deletes underlying pointer.
Definition at line 44 of file ref.h. 00044 { if (--*n_) return; delete t_; delete n_; } |
|
only T objects can encapsulate a (new'ed) pointer into a ref<T>.
|
|
is this a null ref<T>?
Definition at line 47 of file ref.h. 00047 { return (t_!=0); } |
|
ref<T> objects can be dereferenced like a pointer.
Definition at line 49 of file ref.h. 00049 { return *t_; } |
|
ref<T> objects support -> like pointers.
Definition at line 51 of file ref.h. 00051 { return t_; } |
|
assignment decerements reference count of lhs, increases count for rhs.
Definition at line 53 of file ref.h. 00053 { 00054 if (this==&r) return *this; 00055 if (--*n_==0) { 00056 delete t_; delete n_; 00057 } 00058 t_ = r.t_; n_=r.n_; ++*n_; return *this; 00059 } |
|
== compares underlying T objects.
Definition at line 61 of file ref.h. 00061 { 00062 if (t_ == r.t_) return true; 00063 if (t_) 00064 if (r.t_) 00065 return (*t_ == *r); 00066 else 00067 return false; 00068 // r.t_ != t_ == 0 00069 return false; 00070 } |
|
< compares underlying T objects.
Definition at line 72 of file ref.h. 00072 { 00073 // null is minimal element in this implementation 00074 if (t_ == r.t_) return false; 00075 if (t_) 00076 if (r.t_) 00077 return (*t_ < *r); 00078 else 00079 return false; 00080 // t_ == 0, r.t_ != 0 00081 return true; 00082 } |
|
only T can produce non-null ref<T> objects.
|
|
|
|
|