IRC_SERVER
By @hyunjunk (hyunjun2372@gmail.com)
Loading...
Searching...
No Matches
IRCCore::SharedPtr< T > Class Template Reference

Shared pointer custom implementation for C++98 standard. More...

#include <SharedPtr.hpp>

Public Member Functions

 SharedPtr ()
 
 SharedPtr (T *ptr)
 
 SharedPtr (const SharedPtr< T > &rhs)
 
 SharedPtr (detail::ControlBlock< T > *controlBlock)
 Create a shared pointer from the control block.
 
 ~SharedPtr ()
 
SharedPtr< T > & operator= (const SharedPtr< T > &rhs)
 
T & operator* () const
 
T * operator-> () const
 
bool operator== (const SharedPtr< T > &rhs) const
 
bool operator== (const T *rhs) const
 
bool operator!= (const T *rhs) const
 
bool operator!= (const SharedPtr< T > &rhs) const
 
T * Get () const
 
void Reset ()
 
size_t UseCount () const
 
void Swap (SharedPtr< T > &rhs)
 
detail::ControlBlock< T > * GetControlBlock () const
 Get the control block of the shared pointer.
 

Private Attributes

detail::ControlBlock< T > * mControlBlock
 

Friends

class WeakPtr< T >
 

Detailed Description

template<typename T>
class IRCCore::SharedPtr< T >

Shared pointer custom implementation for C++98 standard.

Example usage:

SharedPtr<int> A(new int(5)); //< Create a new resource using MakeShared function
{
WeakPtr<int> C = B;
std::cout << *A.get() << std::endl; //< 5
std::cout << *B.get() << std::endl; //< 5
if (C.expired())
{
std::cout << "C is expired" << std::endl;
}
else
{
// Lock the weak pointer to access the resource.
// If the C is expired while executing the lock() method, the locked_C.get() will be NULL.
SharedPtr<int> locked_C = C.lock();
std::cout << *locked_C.get() << std::endl; //< 5
} //< The locked_C will be released after the scope ends.
A.reset(); //< Release the resource
} //< B will be release and resource will be deallocated.
Shared pointer custom implementation for C++98 standard.
Definition SharedPtr.hpp:164
Weak pointer custom implementation for C++98 standard.
Definition WeakPtr.hpp:18
See also
MakeSharedGroup
Template Parameters
TType of the object to be managed by the shared pointer. (Not support Array type. e.g. int[], char[])
Warning
Not thread-safe

Constructor & Destructor Documentation

◆ SharedPtr() [1/4]

template<typename T >
IRCCore::SharedPtr< T >::SharedPtr ( )
inline

◆ SharedPtr() [2/4]

template<typename T >
IRCCore::SharedPtr< T >::SharedPtr ( T * ptr)
inline

◆ SharedPtr() [3/4]

template<typename T >
IRCCore::SharedPtr< T >::SharedPtr ( const SharedPtr< T > & rhs)
inline

◆ SharedPtr() [4/4]

template<typename T >
IRCCore::SharedPtr< T >::SharedPtr ( detail::ControlBlock< T > * controlBlock)
inlineexplicit

Create a shared pointer from the control block.

Warning
Don't use without a knowledge of the internal implementation.
See also
SharedPtr::GetControlBlock

◆ ~SharedPtr()

template<typename T >
IRCCore::SharedPtr< T >::~SharedPtr ( )
inline

Member Function Documentation

◆ operator=()

template<typename T >
SharedPtr< T > & IRCCore::SharedPtr< T >::operator= ( const SharedPtr< T > & rhs)
inline

◆ operator*()

template<typename T >
T & IRCCore::SharedPtr< T >::operator* ( ) const
inline

◆ operator->()

template<typename T >
T * IRCCore::SharedPtr< T >::operator-> ( ) const
inline

◆ operator==() [1/2]

template<typename T >
bool IRCCore::SharedPtr< T >::operator== ( const SharedPtr< T > & rhs) const
inline

◆ operator==() [2/2]

template<typename T >
bool IRCCore::SharedPtr< T >::operator== ( const T * rhs) const
inline

◆ operator!=() [1/2]

template<typename T >
bool IRCCore::SharedPtr< T >::operator!= ( const T * rhs) const
inline

◆ operator!=() [2/2]

template<typename T >
bool IRCCore::SharedPtr< T >::operator!= ( const SharedPtr< T > & rhs) const
inline

◆ Get()

template<typename T >
T * IRCCore::SharedPtr< T >::Get ( ) const
inline

◆ Reset()

template<typename T >
void IRCCore::SharedPtr< T >::Reset ( )
inline

◆ UseCount()

template<typename T >
size_t IRCCore::SharedPtr< T >::UseCount ( ) const
inline

◆ Swap()

template<typename T >
void IRCCore::SharedPtr< T >::Swap ( SharedPtr< T > & rhs)
inline

◆ GetControlBlock()

template<typename T >
detail::ControlBlock< T > * IRCCore::SharedPtr< T >::GetControlBlock ( ) const
inline

Get the control block of the shared pointer.

Attention
Don't use without a knowledge of the internal implementation.

[한국어]

SharedPtr는 SharedPtr이나 WeakPtr 서로에 대한 복사만 가능하다.
SharedPtr::Get() 등을 통해 직접적으로 데이터의 포인터를 얻은 뒤 해당 주소로 SharedPtr을 생성한다면
기존과 같은 데이터를 공유하는 것이 아닌 완전히 새로운 데이터와 ControlBlock를 생성하게 된다.

하지만, 데이터의 주소가 아닌 ControlBlock의 주소를 얻은 뒤 이를 통해 SharedPtr을 생성한다면
기존의 데이터와 ControlBlock을 공유할 수 있다.
특수한 경우에만 사용해야 하며, ControlBlock의 주소를 얻고 이를 통해 SharedPtr을 생성할 때 까지
해당 ControlBlock이 삭제되지 않도록 주의해야 한다.

Friends And Related Symbol Documentation

◆ WeakPtr< T >

template<typename T >
friend class WeakPtr< T >
friend

Member Data Documentation

◆ mControlBlock

template<typename T >
detail::ControlBlock<T>* IRCCore::SharedPtr< T >::mControlBlock
private

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