rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWTCountedPointer< Body > Class Template Reference
[Smart Pointers]

A smart pointer handle to a reference-counting body. More...

#include <rw/pointer/RWTCountedPointer.h>

Inheritance diagram for RWTCountedPointer< Body >:
RWTPointer< Body >

List of all members.

Public Types

typedef Body BodyType

Public Member Functions

 RWTCountedPointer (RWStaticCtor)
 RWTCountedPointer (Body *bodyP=rwnil)
 RWTCountedPointer (const RWTCountedPointer< Body > &second)
 ~RWTCountedPointer (void)
RWTCountedPointer< Body > & operator= (const RWTCountedPointer< Body > &second)
RWTCountedPointer< Body > & operator= (Body *ptr)
Body * operator-> (void) const
Body & operator* (void) const
void orphan (void)

Protected Member Functions

void transfer (Body *bodyP=rwnil)

Detailed Description

template<class Body>
class RWTCountedPointer< Body >

RWTCountedPointer<Body> is a smart pointer class intended for use as a handle to a reference-counting body. Each time an instance of this class is bound to a body instance, it increments the reference count maintained by that body. Each time it detaches from a body instance, it decrements the body's reference count; and if the reference count reaches zero, it deletes the body instance. The reference counting relieves clients of the burden of tracking when it is safe to delete a body instance.

Any class used for the template parameter Body must derive from class RWTCountingBody or provide the following interface:

 class Body {
   public:

     // Increments the reference count by one.
     void addReference();

     // Decrements the reference count by one.
     // Must return 0 after the last reference count is removed,
     // indicating that it is safe to deallocate the body instance.
     int removeReference();
 };

Examples

 #include <rw/pointer/RWTCountedPointer.h>
 #include <rw/pointer/RWTCountingBody.h>
 #include <rw/sync/RWMutexLock.h>
 #include <iostream>

 class Foo : public RWTCountingBody<RWMutexLock> {
 public:
   void bar(void)
   {
     std::cout << "I'm a foo example" << std::endl;
   }
 };
 
 typedef RWTCountedPointer<Foo> FooPointer;
 
 int main(void)
 {
    try
    {
       FooPointer p1(new Foo);
       p1->bar();
    }
    catch(const RWxmsg& msg)
    {
       std::cout << msg.why() << std::endl;
    }
    return 0;
 }
See also:
RWTCountingBody

Member Typedef Documentation

template<class Body>
typedef Body RWTCountedPointer< Body >::BodyType

The type of the Body.

Reimplemented from RWTPointer< Body >.


Constructor & Destructor Documentation

template<class Body>
RWTCountedPointer< Body >::RWTCountedPointer ( RWStaticCtor   ) 

Special constructor that performs no initialization. Throws no exceptions.

template<class Body>
RWTCountedPointer< Body >::RWTCountedPointer ( Body *  bodyP = rwnil  ) 

Attaches to and increments the reference count on a body.

template<class Body>
RWTCountedPointer< Body >::RWTCountedPointer ( const RWTCountedPointer< Body > &  second  ) 

Attaches to and increments the reference count on the body of second.

template<class Body>
RWTCountedPointer< Body >::~RWTCountedPointer ( void   ) 

Decrements the body's reference count, and deletes it if there are no other references.


Member Function Documentation

template<class Body>
Body& RWTCountedPointer< Body >::operator* ( void   )  const

Dereferences the handle to get a reference to the body. Throws an RWTHRInvalidPointer exception.

template<class Body>
Body* RWTCountedPointer< Body >::operator-> ( void   )  const

Dereferences the handle. Always use this operator to dereference the handle, since it validates the pointer. Throws an RWTHRInvalidPointer exception if invalid.

template<class Body>
RWTCountedPointer<Body>& RWTCountedPointer< Body >::operator= ( Body *  ptr  ) 

Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references. It then attaches to pointer body ptr, and increments its reference count.

Reimplemented from RWTPointer< Body >.

template<class Body>
RWTCountedPointer<Body>& RWTCountedPointer< Body >::operator= ( const RWTCountedPointer< Body > &  second  ) 

Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references. It then attaches to the body of second, and increments its reference count.

template<class Body >
void RWTCountedPointer< Body >::orphan ( void   )  [inline]

Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references.

template<class Body>
void RWTCountedPointer< Body >::transfer ( Body *  bodyP = rwnil  )  [inline, protected]

Detaches from the current body (if any), decrements its reference count, and deletes it if there are no other references. Then it attaches to the new body, incrementing its reference count.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.