SourcePro® API Reference Guide

 
List of all members | Public Types | Public Member Functions | Protected Member Functions
RWTCountedPointer< Body > Class Template Reference

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

#include <rw/pointer/RWTCountedPointer.h>

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

Public Types

typedef Body BodyType
 
- Public Types inherited from RWTPointer< Body >
typedef Body BodyType
 

Public Member Functions

 RWTCountedPointer (RWStaticCtor)
 
 RWTCountedPointer (Body *bodyP=rwnil)
 
 RWTCountedPointer (const RWTCountedPointer< Body > &second)
 
 ~RWTCountedPointer (void)
 
Body & operator* (void) const
 
Body * operator-> (void) const
 
RWTCountedPointer< Body > & operator= (const RWTCountedPointer< Body > &second)
 
RWTCountedPointer< Body > & operator= (Body *ptr)
 
void orphan (void)
 
- Public Member Functions inherited from RWTPointer< Body >
 ~RWTPointer (void)
 
bool isValid (void) const
 
bool operator!= (const RWTPointer< Body > &second) const
 
bool operator!= (const Body *ptr) const
 
bool operator== (const RWTPointer< Body > &second) const
 
bool operator== (const Body *ptr) const
 
void swapWith (RWTPointer< Body > &second)
 
void validate (void) const
 

Protected Member Functions

void transfer (Body *bodyP=rwnil)
 
- Protected Member Functions inherited from RWTPointer< Body >
 RWTPointer (void)
 
 RWTPointer (RWStaticCtor)
 
 RWTPointer (Body *bodyP)
 
 RWTPointer (const RWTPointer< Body > &second)
 
Body * get (void) const
 
RWTPointer< Body > & operator= (Body *ptr)
 

Detailed Description

template<class Body>
class RWTCountedPointer< Body >

RWTCountedPointer 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();
};
Example
#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.

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

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)
inlineprotected

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.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.