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...
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.
struct Dummy
{
int a;
int b;
};
void allocateDummy( Dummy** ptr )
{
*ptr = new Dummy;
}
void acceptDummyPtr( const Dummy* ptr )
{
}
void incorrectUsage()
{
Dummy* dummyPtr = NULL;
scoped_ptr<Dummy> freeMe( dummyPtr );
allocateDummy( &dummyPtr );
}
void recommendedUsage()
{
scoped_ptr< Dummy > sptr;
allocatedDummy( &sptr );
sptr->a = 1;
acceptDummyPtr( sptr.get() );
}
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.
template<class T , class F = scoped_ptr_default_deallocator< T >>
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.