C12Adapter Opensource C++ Interface
MUniquePtr< Type > Class Template Reference

Compiler version independent unique pointer to a variable that is not an array. More...

Inheritance diagram for MUniquePtr< Type >:

Public Member Functions

 MUniquePtr (Type *ptr=NULL)
 Explicit initialization constructor. More...
 
 ~MUniquePtr ()
 Destructor. More...
 
void reset (Type *ptr=NULL)
 Destroy the current object and assign a given pointer. More...
 
- Public Member Functions inherited from MGenericNoncopyablePtr< Type >
Type * operator-> () const
 Field dereference operator.
 
Type & operator* () const
 Pointer dereference operator.
 
Type * get () const
 Get the underlying pointer.
 
Type * release ()
 Return the the underlying pointer while nullifying the unique pointer object.
 

Additional Inherited Members

- Public Types inherited from MGenericNoncopyablePtr< Type >
typedef Type element_type
 Type of the unique pointer.
 
- Protected Member Functions inherited from MGenericNoncopyablePtr< Type >
 MGenericNoncopyablePtr (Type *ptr=NULL)
 Protected explicit initialization constructor.
 
 ~MGenericNoncopyablePtr ()
 Protected destructor. More...
 
- Protected Attributes inherited from MGenericNoncopyablePtr< Type >
Type * m_pointer
 Pointer to object.
 

Detailed Description

template<typename Type>
class MUniquePtr< Type >

Compiler version independent unique pointer to a variable that is not an array.

This one avoids a problem with older compilers that do not support unique_ptr, while at the same time silences deprecation warnings of newer compilers such as GNU GCC 6.1.1 or later.

The class interface is a proper subset of classic auto_ptr, however it does not have a copy constructor at all, making it safe in the way unique_ptr is safe. The whole codebase can be moved to using unique_ptr once all the clients of the library upgrade their compilers from C++Builder 2007 and Visual C++ 2010.

One cannot give an array to unique pointer, and there is a static check for explicit array type. Notice, it is still possible to use the class improperly if a given pointer is not an array, but it points to an array.

See also
MUniqueArrayPtr - unique pointer to an array variable

Constructor & Destructor Documentation

template<typename Type>
MUniquePtr< Type >::MUniquePtr ( Type *  ptr = NULL)
inlineexplicit

Explicit initialization constructor.

Usage is compatible with both std::auto_ptr and std::unique_ptr

template<typename Type>
MUniquePtr< Type >::~MUniquePtr ( )
inline

Destructor.

Same as std::auto_ptr, and different from std::unique_ptr as it cannot handle arrays correctly. There is a compile time check that verifies the pointer is not declared as array.

Member Function Documentation

template<typename Type>
void MUniquePtr< Type >::reset ( Type *  ptr = NULL)
inline

Destroy the current object and assign a given pointer.

Usage is compatible with both std::auto_ptr and std::unique_ptr