nlib
nn::nlib::UniquePtr< T, DEL > Class Template Referencefinal

UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor using the method specified by DEL. More...

#include "nn/nlib/UniquePtr.h"

Inherits nn::nlib::detail::UniquePtrBase< T, DEL, DEL_EMPTY >.

Public Types

typedef T * pointer_type
 Defines T* using typedef.
 
typedef DEL deleter_type
 Defines DEL using typedef.
 
typedef T element_type
 Defines T using typedef.
 

Public Member Functions

void reset (T *p=0) noexcept
 Replaces a pointer. The replaced pointer is deleted. More...
 
T * get () const noexcept
 Gets the pointer that is being stored.
 
T * release () noexcept
 Release ownership of the pointer and returns the pointer. More...
 
void swap (UniquePtr &rhs) noexcept
 Swaps the pointer that is being stored. More...
 
size_t GetHash () const
 Computes the hash value for the pointer and returns the calculation result.
 
DEL & get_deleter ()
 Gets the deleter.
 
const DEL & get_deleter () const
 Gets the deleter.
 
 operator bool () const
 Returns true if holding a valid pointer.
 
Basic Member Functions
constexpr UniquePtr () noexcept
 Instantiates the object with default parameters (default constructor).
 
constexpr UniquePtr (nullptr_t) noexcept
 This function is called if nullptr has been passed to a parameter.
 
 UniquePtr (T *p) noexcept
 Sets a pointer. More...
 
 UniquePtr (T *p, const DEL &del)
 Sets a pointer and a deleter. More...
 
UniquePtrassign (UniquePtr &rhs, move_tag)
 Assigns the object by using swap for a move.
 
 UniquePtr (UniquePtr &rhs, move_tag)
 Instantiates the object by using swap for a move.
 
 UniquePtr (UniquePtr &&rhs)
 Instantiates the object (move constructor). This function is useful when using C++11.
 
UniquePtroperator= (UniquePtr &&rhs)
 Move assignment operator. This function is useful when using C++11.
 
 ~UniquePtr () noexcept
 Destructor. If the pointer is not NULL, executes the deleter and deletes.
 
AddRef< T >::type operator* () const
 Dereferences the pointer.
 
T * operator-> () const noexcept
 Returns the pointer.
 
UniquePtroperator= (nullptr_t) noexcept
 Substitutes for nullptr.
 

Detailed Description

template<class T, class DEL = DefaultDelete<T>>
class nn::nlib::UniquePtr< T, DEL >

UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor using the method specified by DEL.

Template Parameters
TThe object type.
DELThe deleter type. The default execution is delete/delete[].
Description
Write your code in the following way.
UniquePtr<MyObject> myObjPtr(new MyObject());
UniquePtr<char[]> myArrayPtr(new char[1024]);
UniquePtr<void, void (*)(void*)> myMallocFreePtr(nlib_malloc(1024), nlib_free);
operator== and operator!= functions have been implemented for comparing UniquePtr objects. If a pointer to an array is set, you can use operator[].
In the C++11 environment (which supports template aliases), std:: unique_ptr is made a template alias.
Internal Implementation Memo
In the current implementation, a UniquePtr derived class cannot be substituted for the UniquePtr base class.
Examples:
exi/multithread/multithread.cpp, exi/script/script.cpp, exi/simple1/simple1.cpp, exi/simple2/simple2.cpp, exi/textparser/textparser.cpp, misc/threading/future/future.cpp, msgpack/json/json.cpp, msgpack/jsonrpc/jsonrpc.cpp, msgpack/jsonrpc/server.cpp, msgpack/msgpack1/msgpack1.cpp, msgpack/msgpack2/msgpack2.cpp, oss/binarypatch/binarypatch.cpp, and succinct/ngc/ngc.cpp.

Definition at line 109 of file UniquePtr.h.

Constructor & Destructor Documentation

◆ UniquePtr() [1/2]

template<class T, class DEL = DefaultDelete<T>>
nn::nlib::UniquePtr< T, DEL >::UniquePtr ( T *  p)
inlineexplicitnoexcept

Sets a pointer.

Parameters
[in]pA pointer.

Definition at line 119 of file UniquePtr.h.

◆ UniquePtr() [2/2]

template<class T, class DEL = DefaultDelete<T>>
nn::nlib::UniquePtr< T, DEL >::UniquePtr ( T *  p,
const DEL &  del 
)
inline

Sets a pointer and a deleter.

Parameters
[in]pA pointer.
[in]delA deleter.

Definition at line 120 of file UniquePtr.h.

Member Function Documentation

◆ release()

template<class T, class DEL = DefaultDelete<T>>
nn::nlib::UniquePtr< T, DEL >::release ( )
inlinenoexcept

Release ownership of the pointer and returns the pointer.

Returns
The pointer.

Definition at line 146 of file UniquePtr.h.

◆ reset()

template<class T, class DEL = DefaultDelete<T>>
nn::nlib::UniquePtr< T, DEL >::reset ( T *  p = 0)
inlinenoexcept

Replaces a pointer. The replaced pointer is deleted.

Parameters
[in]pA pointer.
Description
If the parameter is omitted, NULL is set for p.
Examples:
succinct/ngc/ngc.cpp.

Definition at line 128 of file UniquePtr.h.

◆ swap()

template<class T, class DEL = DefaultDelete<T>>
nn::nlib::UniquePtr< T, DEL >::swap ( UniquePtr< T, DEL > &  rhs)
inlinenoexcept

Swaps the pointer that is being stored.

Parameters
[in,out]rhsThe UniquePtr object being swapped.

Definition at line 151 of file UniquePtr.h.


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