Teamcenter C++ API Reference  2312
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
Teamcenter::scoped_ptr< T, F > Class Template Reference

Template class that stores a pointer to an object and ensures that the object gets destroyed automatically when instance of this class goes out of scope. More...

#include <ScopedPtr.hxx>

Public Member Functions

T * get () const
 
T *& getReference ()
 
const T * getString () const
 
 operator T * () const
 
bool operator! () const
 
bool operator!= (const T *t) const
 
T ** operator& ()
 
T & operator* () const
 
T * operator-> () const
 
scoped_ptroperator= (T *ptr)
 
bool operator== (const T *t) const
 
T * release ()
 
 scoped_ptr (T *b=0)
 
virtual ~scoped_ptr ()
 

Protected Attributes

T * bucket
 

Private Member Functions

scoped_ptroperator= (const scoped_ptr &)
 
 scoped_ptr (const scoped_ptr &)
 

Detailed Description

template<class T, class F = scoped_ptr_default_deallocator< T >>
class Teamcenter::scoped_ptr< T, F >

Template class that stores a pointer to an object and ensures that the object gets destroyed automatically when instance of this class goes out of scope.

Things to be aware of when using scoped_ptr.


Allocation scheme used: By default, scoped_ptr provides support for object that was allocated using 'operator new'.
If any other mechanism of allocation is used, then the custom deallocator needs to be supplied. Use scoped_ptr_default_deallocator for reference when providing a custom deallocator.

Example.

// example.
struct Dummy
{
int a;
int b;
};
void allocateDummy( Dummy** ptr )
{
*ptr = new Dummy;
}
// does nothing. purpose here is to only use this function's signature
// as an example.
void acceptDummyPtr( const Dummy* ptr )
{
}
// Incorrect usage pattern.
void incorrectUsage()
{
Dummy* dummyPtr = NULL;
scoped_ptr<Dummy> freeMe( dummyPtr ); // as 'aptr' is NULL, the scoped_ptr owns a pointer with NULL value.
allocateDummy( &dummyPtr ); // the scoped_ptr is unaware about this allocation and leads to a memory leak.
}
void recommendedUsage()
{
// scoped_ptr can be passed around like a normal pointer.
// Its best to avoid using two named variables (one for the normal pointer and other scoped_ptr) and
// use the scoped_ptr just like normal pointer.
scoped_ptr< Dummy > sptr;
allocatedDummy( &sptr );
// use like a normal pointer.
sptr->a = 1;
// pass the contained pointer using get() method.
// Note: conversion is explicit intentianally. Implicit conversion can have some issues.
acceptDummyPtr( sptr.get() );
}

scoped_ptr vs. std::auto_ptr


Most of the capabilities of scoped_ptr are provided by std::auto_ptr.
The reason we provided our own class is due to strictness. If an auto_ptr is copied, the source loses the reference. This ensures that only one auto_ptr is responsible for the object's lifetime. However, this can lead to subtle issues (say during implicit copying , etc.). scoped_ptr is more strict and doesn't allow such copying.

Definition at line 97 of file ScopedPtr.hxx.

Constructor & Destructor Documentation

template<class T , class F = scoped_ptr_default_deallocator< T >>
Teamcenter::scoped_ptr< T, F >::scoped_ptr ( T *  b = 0)
inlineexplicit

Default constructor. It stores the pointer to an object.

Definition at line 104 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
virtual Teamcenter::scoped_ptr< T, F >::~scoped_ptr ( )
inlinevirtual

Default destructor. It also deletes the object to which it points.

Definition at line 109 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
Teamcenter::scoped_ptr< T, F >::scoped_ptr ( const scoped_ptr< T, F > &  )
private

Member Function Documentation

template<class T , class F = scoped_ptr_default_deallocator< T >>
T* Teamcenter::scoped_ptr< T, F >::get ( ) const
inline

Returns the pointer to the object.

Definition at line 131 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
T*& Teamcenter::scoped_ptr< T, F >::getReference ( )
inline

Returns the reference of pointed memory.

Definition at line 126 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
const T* Teamcenter::scoped_ptr< T, F >::getString ( ) const
inline

Safe getter of the string of elements of type T.

It returns the memory pointed by this object, or a pointer to a default constructed element if the object does not point to any memory.
This is particularly useful if the object manages memory of a C-string, because it can be used for a NULL-safe assignment to a std::string.

Definition at line 141 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
Teamcenter::scoped_ptr< T, F >::operator T * ( ) const
inline

Definition at line 120 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
bool Teamcenter::scoped_ptr< T, F >::operator! ( ) const
inline

Definition at line 151 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
bool Teamcenter::scoped_ptr< T, F >::operator!= ( const T *  t) const
inline

Definition at line 149 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
T** Teamcenter::scoped_ptr< T, F >::operator& ( )
inline

Definition at line 116 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
T& Teamcenter::scoped_ptr< T, F >::operator* ( ) const
inline

Definition at line 117 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
T* Teamcenter::scoped_ptr< T, F >::operator-> ( ) const
inline

Definition at line 118 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
scoped_ptr& Teamcenter::scoped_ptr< T, F >::operator= ( T *  ptr)
inline

Deletes the pointer being held currently and then holds the input pointer.

Definition at line 114 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
scoped_ptr& Teamcenter::scoped_ptr< T, F >::operator= ( const scoped_ptr< T, F > &  )
private
template<class T , class F = scoped_ptr_default_deallocator< T >>
bool Teamcenter::scoped_ptr< T, F >::operator== ( const T *  t) const
inline

Definition at line 148 of file ScopedPtr.hxx.

template<class T , class F = scoped_ptr_default_deallocator< T >>
T* Teamcenter::scoped_ptr< T, F >::release ( )
inline

Releases the pointer without deleting the object to which it points.

Definition at line 146 of file ScopedPtr.hxx.

Member Data Documentation

template<class T , class F = scoped_ptr_default_deallocator< T >>
T* Teamcenter::scoped_ptr< T, F >::bucket
protected

Contains the pointer to an object.

Definition at line 160 of file ScopedPtr.hxx.


The documentation for this class was generated from the following file: