Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

ref Class Template Reference

Reference-counted pointers. More...

#include <ref.h>

Collaboration diagram for ref:

Collaboration graph
[legend]
List of all members.

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

Toperator * () const
 ref<T> objects can be dereferenced like a pointer. More...

Toperator-> () 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...

Tt_
unsigned int * n_

Detailed Description

template<class T>
class ref< T >

Reference-counted pointers.

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.


Constructor & Destructor Documentation

template<class T>
ref< T >::ref [inline]
 

default constructor makes null ref<T>.

Definition at line 40 of file ref.h.

00040 : t_(0), n_(new unsigned int(1)) {}

template<class T>
ref< T >::ref const ref< T > & r [inline]
 

copy constructor increments reference count.

Definition at line 42 of file ref.h.

00042 : t_(r.t_), n_(r.n_) { ++*n_; }

template<class T>
ref< T >::~ref [inline]
 

destructor decrements reference count or deletes underlying pointer.

Definition at line 44 of file ref.h.

00044 { if (--*n_) return; delete t_; delete n_; }

template<class T>
ref< T >::ref T * t [inline, protected]
 

only T objects can encapsulate a (new'ed) pointer into a ref<T>.

Definition at line 85 of file ref.h.

00085 : t_(t), n_(new unsigned int(1)) {}


Member Function Documentation

template<class T>
ref< T >::operator bool const [inline]
 

is this a null ref<T>?

Definition at line 47 of file ref.h.

00047 { return (t_!=0); }

template<class T>
T& ref< T >::operator * const [inline]
 

ref<T> objects can be dereferenced like a pointer.

Definition at line 49 of file ref.h.

00049 { return *t_; }

template<class T>
T* ref< T >::operator-> const [inline]
 

ref<T> objects support -> like pointers.

Definition at line 51 of file ref.h.

00051 { return t_; }    

template<class T>
ref<T>& ref< T >::operator= const ref< T > & r [inline]
 

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              }

template<class T>
bool ref< T >::operator== const ref< T > & r const [inline]
 

== 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         }

template<class T>
bool ref< T >::operator< const ref< T > & r const [inline]
 

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


Member Data Documentation

template<class T>
friend ref::T [private]
 

only T can produce non-null ref<T> objects.

Definition at line 37 of file ref.h.

template<class T>
T* ref::t_ [private]
 

Definition at line 87 of file ref.h.

template<class T>
unsigned int* ref::n_ [private]
 

Definition at line 88 of file ref.h.


The documentation for this class was generated from the following file:
FSM [ August, 2001]