lkptr - simple reference LinKed PoinTeR:
Public Types | Public Member Functions | Private Attributes | Friends
array_lkptr< X > Class Template Reference

Similar to lkptr<>, but points to an array instead of a single object. More...

#include <lkptr.h>

List of all members.

Public Types

typedef X element_type
 Standard name for the pointed object.
typedef X value_type
 Standard name for the pointed object.
typedef X * pointer
 Standard name for the pointer to the object.

Public Member Functions

X & operator[] (int i)
 V[i].
 array_lkptr (X *p=0) throw ()
template<typename Y >
 array_lkptr (Y *p) throw ()
 array_lkptr (const array_lkptr &r) throw ()
template<typename Y >
 array_lkptr (const array_lkptr< Y > &r) throw ()
 ~array_lkptr ()
 Destructor.
array_lkptroperator= (const array_lkptr &r)
 Copy operator: share the object with other pointers.
template<typename Y >
array_lkptroperator= (const array_lkptr< Y > &r)
 Copy operator from a derived clase: share the object with other pointers.
void swap (array_lkptr &r) throw ()
 Swaps with r.
X * get () const throw ()
X & value () const throw ()
X & operator* () const throw ()
X * operator-> () const throw ()
bool isNull () const throw ()
bool unique () const throw ()
int use_count () const throw ()
void release ()
 Releases the object.
void reset (X *p=0)
 Resets the pointer so that it will point to "p".
void reset (const array_lkptr &r)
 (*this) = r.
template<typename Y >
void reset (const array_lkptr< Y > &r)
 (*this) = r [ from a derived class ].
bool ok () const throw ()

Private Attributes

base_lkptr< X > b
 Contains the 3 pointers for the array_lkptr<>.

Friends

class array_lkptr
class base_lkptr
 Test array_lkptr<X>.
template<typename Y >
bool check_ok (const array_lkptr< Y > &p) throw ()

Detailed Description

template<typename X>
class array_lkptr< X >

Similar to lkptr<>, but points to an array instead of a single object.

Definition at line 727 of file lkptr.h.


Member Typedef Documentation

template<typename X>
typedef X array_lkptr< X >::element_type

Standard name for the pointed object.

Definition at line 732 of file lkptr.h.

template<typename X>
typedef X array_lkptr< X >::value_type

Standard name for the pointed object.

Definition at line 733 of file lkptr.h.

template<typename X>
typedef X* array_lkptr< X >::pointer

Standard name for the pointer to the object.

Definition at line 734 of file lkptr.h.


Constructor & Destructor Documentation

template<typename X>
array_lkptr< X >::array_lkptr ( X *  p = 0) throw () [inline, explicit]

Default constructor and constructor from a regular pointer.

.

Definition at line 742 of file lkptr.h.

template<typename X>
template<typename Y >
array_lkptr< X >::array_lkptr ( Y *  p) throw () [inline, explicit]

Constructor from a pointer to a derived class.

  • Check that the pointer is not null when types are not compatible.
  • Make sure the destructor for the base class is virtual.
.

Definition at line 746 of file lkptr.h.

template<typename X>
array_lkptr< X >::array_lkptr ( const array_lkptr< X > &  r) throw () [inline]

Copy constructor: share the object with other pointers.

.

Definition at line 749 of file lkptr.h.

template<typename X>
template<typename Y >
array_lkptr< X >::array_lkptr ( const array_lkptr< Y > &  r) throw () [inline, explicit]

Copy constructor from a derived clase: share the object with other pointers.

  • Check that the pointer is not null when types are not compatible.
  • Make sure the destructor for the base class is virtual.
.

Definition at line 753 of file lkptr.h.

template<typename X>
array_lkptr< X >::~array_lkptr ( ) [inline]

Destructor.

Definition at line 755 of file lkptr.h.


Member Function Documentation

template<typename X>
X& array_lkptr< X >::operator[] ( int  i) [inline]

V[i].

Definition at line 737 of file lkptr.h.

template<typename X>
array_lkptr& array_lkptr< X >::operator= ( const array_lkptr< X > &  r) [inline]

Copy operator: share the object with other pointers.

Definition at line 765 of file lkptr.h.

template<typename X>
template<typename Y >
array_lkptr& array_lkptr< X >::operator= ( const array_lkptr< Y > &  r) [inline]

Copy operator from a derived clase: share the object with other pointers.

  • Will not copy when types are not compatible.
  • Make sure the destructor for the base class is virtual.

Definition at line 801 of file lkptr.h.

template<typename X >
void array_lkptr< X >::swap ( array_lkptr< X > &  r) throw ()

Swaps with r.

Definition at line 917 of file lkptr.h.

template<typename X>
X* array_lkptr< X >::get ( ) const throw () [inline]

Returns a pointer to referenced object.

.

Definition at line 811 of file lkptr.h.

template<typename X>
X& array_lkptr< X >::value ( ) const throw () [inline]

Returns the referenced object.

.

Definition at line 812 of file lkptr.h.

template<typename X>
X& array_lkptr< X >::operator* ( ) const throw () [inline]

Returns the referenced object.

.

Definition at line 813 of file lkptr.h.

template<typename X>
X* array_lkptr< X >::operator-> ( ) const throw () [inline]

Returns a pointer to referenced object.

.

Definition at line 814 of file lkptr.h.

template<typename X>
bool array_lkptr< X >::isNull ( ) const throw () [inline]

Returns true if the object pointed is null and false otherwise.

.

Definition at line 817 of file lkptr.h.

template<typename X>
bool array_lkptr< X >::unique ( ) const throw () [inline]

Return "true" when only one pointer references the object.

.

Definition at line 818 of file lkptr.h.

template<typename X>
int array_lkptr< X >::use_count ( ) const throw () [inline]

Returns how many pointers are sharing the object referenced by "this".

{{  // test::null_3x1
    lkptr<X> A, B, C;
    A = B = C;
    assertTrue( A.ok() );   assertTrue( check_ok(A) );
    assertTrue( B.ok() );   assertTrue( check_ok(C) );
    assertTrue( C.ok() );   assertTrue( check_ok(C) );

    assertTrue( A.use_count() == 3 );
    assertTrue( B.use_count() == 3 );
    assertTrue( C.use_count() == 3 );

    assertTrue( A == B );
    assertTrue( B == C );
    assertTrue( A.get() == 0 && A.isNull() );

    assertTrue( A.use_count() == 3 );
    assertTrue( B.use_count() == 3 );
    assertTrue( C.use_count() == 3 );
}}

.

Definition at line 819 of file lkptr.h.

template<typename X>
void array_lkptr< X >::release ( ) [inline]

Releases the object.

  • Before: If this was the last reference, it also deletes the referenced object.
  • After: The pointer becomes NULL.
  • After: Equivalent to reset(0).

Definition at line 825 of file lkptr.h.

template<typename X>
void array_lkptr< X >::reset ( X *  p = 0) [inline]

Resets the pointer so that it will point to "p".

  • Before: If this was the last reference, it also deletes the referenced object.
  • Before: "p==0" is a valid argument (NULL is ok).
  • After: The object pointed by "p" gets to be own by "this".
  • [DANGER] V.reset( r.get() ) almost always is an error because both V and r will destroy the referenced object. Use V = r.
    struct Point                          { virtual int val() const { return 0; } };
      struct Circle      : public Point   { virtual int val() const { return 1; } };
        struct Cilinder  : public Circle  { virtual int val() const { return 2; } };
      struct Square      : public Point   { virtual int val() const { return 3; } };
        struct Rectangle : public Square  { virtual int val() const { return 4; } };
    
    {{  // test::inheritance
        lkptr<Point> VEC[5];
        VEC[0].reset(  new Point()         );
        VEC[1].reset(    new Circle()      );
        VEC[2].reset(      new Cilinder()  );
        VEC[3].reset(    new Square()      );
        VEC[4].reset(      new Rectangle() );
    
        for (int i=0; i<int(DIM(VEC)); ++i) {
            assertTrue( VEC[i]->val() == i );
            assertTrue( VEC[i].ok() );
        }
    }}
    

Definition at line 850 of file lkptr.h.

template<typename X>
void array_lkptr< X >::reset ( const array_lkptr< X > &  r) [inline]

(*this) = r.

Definition at line 861 of file lkptr.h.

template<typename X>
template<typename Y >
void array_lkptr< X >::reset ( const array_lkptr< Y > &  r) [inline]

(*this) = r [ from a derived class ].

Definition at line 891 of file lkptr.h.

template<typename X>
bool array_lkptr< X >::ok ( ) const throw () [inline]

Verifies the invariant for class lkptr<X>.

  • Returns "true" when "p" contains a correct value.
  • It could return "true" even when it's value is corrupted.
  • it could never return if the value in "p" is corrupted.
Rep: Description with words.
  • Field "ptr" is the pointer to the referenced object.
  • All pointers that point to the same object are linked together.
  • The list is double linked to allow for O(1) insertion and removal.
  • There is no "first" or "last" element in the list. What it is important is to be "in" the list, not which position in it a particular lkptr<X> occupies.
Rep: Class diagram.
    <=================================>     <=============>
    |      p1        p2       p3      |     |      p4     |
    |    +-+-+     +-+-+     +-+-+    |     |    +-+-+    |
    <===>|*|*|<===>|*|*|<===>|*|*|<===>     <===>|*|*|<===>
         +-+-+     +-+-+     +-+-+               +-+-+
         | * |     | * |     | * |               | * |
         +-|-+     +-|-+     +-|-+               +-|-+
           |         |         |                   |
          \|/       \|/       \|/                 \|/
         +----------------------+      +----------------------+
         |     Shared object    |      |    Lonely Object     |
         +----------------------+      +----------------------+
     +--------+-------+  "ptr" points to the object
     |  prev  |       |
     +--------+  ptr  +
     |  next  |       |  "next" is next in chain
     +--------+-------+  "prev" is previous in chain
The invariant: Description with words.
.

Definition at line 900 of file lkptr.h.


Friends And Related Function Documentation

template<typename X>
friend class array_lkptr [friend]

Definition at line 730 of file lkptr.h.

template<typename X>
friend class base_lkptr [friend]

Test array_lkptr<X>.

Definition at line 904 of file lkptr.h.

template<typename X>
template<typename Y >
bool check_ok ( const array_lkptr< Y > &  p) throw () [friend]

Member Data Documentation

template<typename X>
base_lkptr<X> array_lkptr< X >::b [private]

Contains the 3 pointers for the array_lkptr<>.

Definition at line 728 of file lkptr.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines