Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Dv::Util::ref< T > Class Template Reference

Reference-counted pointers. More...

#include <ref.h>

Collaboration diagram for Dv::Util::ref< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ref ()
 Default constructor makes null ref<T>.
 ref (const ref< T > &r)
 copy constructor increments reference count
template<typename X>  ref (const ref< X > &r)
 ~ref ()
 destructor decrements reference count or deletes underlying pointer
unsigned int * n () const
 return pointer to counter
 operator bool () const
 is this a null ref<T>?
T & operator * () const
 ref<T> objects can be dereferenced like a pointer
T * operator-> () const
 ref<T> objects support -> like pointers
ref< T > & operator= (const ref< T > &r)
 assignment decrements reference count of lhs, increases count for rhs
bool operator== (const ref< T > &r) const
 == compares underlying T objects
bool operator< (const ref< T > &r) const
 < compares underlying T objects
 ref (T *t)
 only T objects can encapsulate a (new'ed) pointer into a ref<T>
template<typename X>  operator ref () const

Private Attributes

T * t_
unsigned int * n_

Detailed Description

template<class T>
class Dv::Util::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 43 of file ref.h.


Constructor & Destructor Documentation

template<class T>
Dv::Util::ref< T >::ref  )  [inline]
 

Default constructor makes null ref<T>.

Definition at line 49 of file ref.h.

References Dv::Util::ref< T >::n_, and Dv::Util::ref< T >::t_.

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

copy constructor increments reference count

Definition at line 51 of file ref.h.

References Dv::Util::ref< T >::n(), Dv::Util::ref< T >::n_, and Dv::Util::ref< T >::t_.

template<class T>
template<typename X>
Dv::Util::ref< T >::ref const ref< X > &  r  )  [inline, explicit]
 

Definition at line 54 of file ref.h.

References Dv::Util::ref< T >::n(), Dv::Util::ref< T >::n_, and Dv::Util::ref< T >::t_.

template<class T>
Dv::Util::ref< T >::~ref  )  [inline]
 

destructor decrements reference count or deletes underlying pointer

Definition at line 56 of file ref.h.

References Dv::Util::ref< T >::n_, and Dv::Util::ref< T >::t_.

template<class T>
Dv::Util::ref< T >::ref T *  t  )  [inline, explicit]
 

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

Definition at line 102 of file ref.h.

References Dv::Util::ref< T >::n_, and Dv::Util::ref< T >::t_.


Member Function Documentation

template<class T>
unsigned int* Dv::Util::ref< T >::n  )  const [inline]
 

return pointer to counter

Definition at line 60 of file ref.h.

References Dv::Util::ref< T >::n_.

Referenced by Dv::Util::ref< T >::ref().

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

is this a null ref<T>?

Definition at line 62 of file ref.h.

References Dv::Util::ref< T >::t_.

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

ref<T> objects can be dereferenced like a pointer

Definition at line 64 of file ref.h.

References Dv::Util::ref< T >::t_.

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

ref<T> objects support -> like pointers

Definition at line 66 of file ref.h.

References Dv::Util::ref< T >::t_.

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

assignment decrements reference count of lhs, increases count for rhs

Definition at line 68 of file ref.h.

References Dv::Util::ref< T >::n_, and Dv::Util::ref< T >::t_.

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

== compares underlying T objects

Definition at line 77 of file ref.h.

References Dv::Util::ref< T >::t_.

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

< compares underlying T objects

Definition at line 88 of file ref.h.

References Dv::Util::ref< T >::t_.

template<class T>
template<typename X>
Dv::Util::ref< T >::operator ref  )  const [inline]
 

Definition at line 105 of file ref.h.


Member Data Documentation

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

Definition at line 109 of file ref.h.

Referenced by Dv::Util::ref< T >::operator *(), Dv::Util::ref< T >::operator bool(), Dv::Util::ref< T >::operator->(), Dv::Util::ref< T >::operator<(), Dv::Util::ref< T >::operator=(), Dv::Util::ref< T >::operator==(), Dv::Util::ref< T >::ref(), and Dv::Util::ref< T >::~ref().

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

Definition at line 110 of file ref.h.

Referenced by Dv::Util::ref< T >::n(), Dv::Util::ref< T >::operator=(), Dv::Util::ref< T >::ref(), and Dv::Util::ref< T >::~ref().


The documentation for this class was generated from the following file:
dvutil-0.13.15 [30 December, 2004]