SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
RWTCountingPointer< Body, Counter > Class Template Reference

Defines a reference-counted pointer that provides reference counting semantics for types that do not directly support reference counting. More...

#include <rw/pointer/RWTCountingPointer.h>

Inheritance diagram for RWTCountingPointer< Body, Counter >:
RWTPointer< Body >

Public Member Functions

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

Additional Inherited Members

- Public Types inherited from RWTPointer< Body >
typedef Body BodyType
 
- Protected Member Functions inherited from RWTPointer< Body >
 RWTPointer (Body *bodyP)
 
 RWTPointer (const RWTPointer< Body > &second)
 
 RWTPointer (RWStaticCtor)
 
 RWTPointer (void)
 
Body * get (void) const
 
RWTPointer< Body > & operator= (Body *ptr)
 

Detailed Description

template<class Body, class Counter = RWAtomicCounter>
class RWTCountingPointer< Body, Counter >

Defines a reference-counted pointer that provides reference counting semantics for types that do not directly support reference counting. Body, the first template parameter, can be any type. For the second template parameter, Counter, you will generally use RWAtomicCounter.

A user-defined counter class must provide the following interface:

class Counter {
public:
// Constructs a counter instance with an initial count of x.
Counter(int x);
// 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/RWTCountingPointer.h>
int main() {
RWTCountingPointer<int> pointer = new int(10);
return 0;
}
Defines a reference-counted pointer that provides reference counting semantics for types that do not ...
Definition RWTCountingPointer.h:64
See also
RWAtomicCounter

Constructor & Destructor Documentation

◆ RWTCountingPointer() [1/3]

template<class Body , class Counter = RWAtomicCounter>
RWTCountingPointer< Body, Counter >::RWTCountingPointer ( RWStaticCtor )

Special constructor that performs no initialization.

◆ RWTCountingPointer() [2/3]

template<class Body , class Counter = RWAtomicCounter>
RWTCountingPointer< Body, Counter >::RWTCountingPointer ( Body * bodyP = rwnil)

Constructs a pointer instance that manages references for the specified Body instance. Parameter bodyP is the Body instance to be managed by this pointer.

◆ RWTCountingPointer() [3/3]

template<class Body , class Counter = RWAtomicCounter>
RWTCountingPointer< Body, Counter >::RWTCountingPointer ( const RWTCountingPointer< Body, Counter > & second)

Constructs a pointer instance that can access the body instance associated with another pointer instance, incrementing the underlying reference count for that instance by one.

◆ ~RWTCountingPointer()

template<class Body , class Counter = RWAtomicCounter>
RWTCountingPointer< Body, Counter >::~RWTCountingPointer ( void )

Decrements the reference counter and deletes the body instance, if there are no other references.

Member Function Documentation

◆ operator*()

template<class Body , class Counter = RWAtomicCounter>
Body & RWTCountingPointer< Body, Counter >::operator* ( void ) const

Dereferences the handle to get a reference to the body. Throws exception RWTHRInvalidPointer if pointer is invalid.

◆ operator->()

template<class Body , class Counter = RWAtomicCounter>
Body * RWTCountingPointer< Body, Counter >::operator-> ( void ) const

Dereferencing operator. This operator should always be used to dereference the handle as it will validate the pointer, throwing an exception if it is not valid. Returns a pointer to the body instance attached to this handle. Throws exception RWTHRInvalidPointer if no body instance has been attached to this handle instance.

◆ operator=() [1/2]

template<class Body , class Counter = RWAtomicCounter>
RWTCountingPointer< Body, Counter > & RWTCountingPointer< Body, Counter >::operator= ( Body * bodyP)

Detaches from the current body (if any), decrements its associated reference count, and deletes it if there are no other associated references. It then attaches to the pointer bodyP, and increments its associated reference count. The new attached pointer must point to the dynamically allocated object. Throws no exceptions.

◆ operator=() [2/2]

template<class Body , class Counter = RWAtomicCounter>
RWTCountingPointer< Body, Counter > & RWTCountingPointer< Body, Counter >::operator= ( const RWTCountingPointer< Body, Counter > & second)

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

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