DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
DotNetDupe::System::SmartPointer< T > Class Template Reference

A unified smart pointer that supports both unique and shared ownership semantics. More...

#include <SmartPointer.h>

Public Member Functions

 SmartPointer ()
 Default constructor. For concrete types: Automatically allocates a new instance of T. For abstract types: Initializes to nullptr.
 SmartPointer (bool bIsShared)
 Constructor with ownership mode flag. Automatically allocates a new instance of T.
 SmartPointer (const SmartPointer &objOther)
 Copy constructor. Only permitted if the source is in Shared mode.
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
 SmartPointer (const SmartPointer< U > &objOther)
 SmartPointer (SmartPointer &&objOther) noexcept
 Move constructor. Transfers ownership from the source.
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
 SmartPointer (SmartPointer< U > &&objOther) noexcept
 SmartPointer (std::nullptr_t)
 Explicit null constructor.
 SmartPointer (T *pPtr)
 Constructor for explicit raw pointer attachment.
 SmartPointer (T *pPtr, bool bIsShared)
 Constructor that specifies ownership mode for a raw pointer.
 ~SmartPointer ()
 Destructor. Cleans up the managed object based on ownership mode.
void Attach (T *pPtr, bool bIsShared=false)
 Attaches a new raw pointer to the SmartPointer.
template<typename U>
SmartPointer< U > ConstCast () const
 Const casts the managed pointer to another type U and returns a new SmartPointer sharing ownership.
T * Detach ()
 Detaches the managed object and returns it. The SmartPointer will no longer own the object.
template<typename U>
SmartPointer< U > DynamicCast () const
 Dynamically casts the managed pointer to another type U and returns a new SmartPointer sharing ownership.
T * Get () const noexcept
 Gets the raw pointer.
int GetRefCount () const noexcept
 Gets the current reference count. Returns 0 for Unique or Null pointers.
bool IsNull () const noexcept
 Checks if the SmartPointer is null.
 operator bool () const noexcept
bool operator!= (const SmartPointer &other) const noexcept
template<typename U>
bool operator!= (const SmartPointer< U > &other) const noexcept
template<typename U>
bool operator!= (const U *pOther) const noexcept
bool operator!= (std::nullptr_t) const noexcept
T & operator* () const
T * operator-> () const noexcept
SmartPointeroperator= (const SmartPointer &objOther)
 Copy assignment operator. Only permitted if the source is in Shared mode.
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
SmartPointeroperator= (const SmartPointer< U > &objOther)
SmartPointeroperator= (SmartPointer &&objOther) noexcept
 Move assignment operator. Transfers ownership from the source.
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
SmartPointeroperator= (SmartPointer< U > &&objOther) noexcept
bool operator== (const SmartPointer &other) const noexcept
template<typename U>
bool operator== (const SmartPointer< U > &other) const noexcept
template<typename U>
bool operator== (const U *pOther) const noexcept
bool operator== (std::nullptr_t) const noexcept
void Reset (T *pPtr, bool bIsShared)
 Resets the SmartPointer with a specific ownership mode.
void Reset (T *pPtr=nullptr)
 Resets the SmartPointer to null or a new object in Unique mode.
template<typename U>
SmartPointer< U > StaticCast () const
 Statically casts the managed pointer to another type U and returns a new SmartPointer sharing ownership.

Static Public Member Functions

template<typename From>
static SmartPointer< T > ConstCast (const SmartPointer< From > &sp)
template<typename From>
static SmartPointer< T > DynamicCast (const SmartPointer< From > &sp)
static SmartPointer< T > MakeShared ()
 Alias for NewShared (default construction).
template<typename Arg1, typename... Args>
static SmartPointer< T > MakeShared (Arg1 &&arg1, Args &&... args)
 Alias for NewShared. Provided for compatibility.
static SmartPointer< T > MakeUnique ()
 Alias for NewUnique (default construction).
template<typename Arg1, typename... Args>
static SmartPointer< T > MakeUnique (Arg1 &&arg1, Args &&... args)
 Alias for NewUnique. Provided for compatibility.
static SmartPointer< T > New ()
 Creates a new SmartPointer (default construction).
template<typename Arg1, typename... Args>
static SmartPointer< T > New (Arg1 &&arg1, Args &&... args)
 Creates a new SmartPointer with variadic arguments for T's constructor.
static SmartPointer< T > NewShared ()
 Creates a Shared SmartPointer, default constructing T.
template<typename Arg1, typename... Args>
static SmartPointer< T > NewShared (Arg1 &&arg1, Args &&... args)
 Creates a Shared SmartPointer, forwarding arguments to T's constructor.
static SmartPointer< T > NewUnique ()
 Creates a Unique SmartPointer, default constructing T.
template<typename Arg1, typename... Args>
static SmartPointer< T > NewUnique (Arg1 &&arg1, Args &&... args)
 Creates a Unique SmartPointer, forwarding arguments to T's constructor.
template<typename From>
static SmartPointer< T > StaticCast (const SmartPointer< From > &sp)

Detailed Description

template<typename T>
class DotNetDupe::System::SmartPointer< T >

A unified smart pointer that supports both unique and shared ownership semantics.

Provides high-performance automatic lifetime management without raw pointer leaks:

  • Unique mode: Zero-overhead RAII ownership (default).
  • Shared mode: Atomic reference counting with intrusive or external reference counter blocks. Thread-safe for reference count increments and decrements in shared mode.
Note
Conforms to DotNetDupe RAII Memory Management Standards (Quality Gate 8 & 11).
See also
Object, EnableSharedFromThis

Definition at line 72 of file SmartPointer.h.

Constructor & Destructor Documentation

◆ SmartPointer() [1/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( )
inline

Default constructor. For concrete types: Automatically allocates a new instance of T. For abstract types: Initializes to nullptr.

Definition at line 85 of file SmartPointer.h.

◆ SmartPointer() [2/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( bool bIsShared)
inlineexplicit

Constructor with ownership mode flag. Automatically allocates a new instance of T.

Parameters
bIsSharedIf true, enables reference counting (Shared mode).

Definition at line 105 of file SmartPointer.h.

◆ SmartPointer() [3/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( T * pPtr)
inlineexplicit

Constructor for explicit raw pointer attachment.

Parameters
pPtrThe raw pointer to take ownership of.

Definition at line 127 of file SmartPointer.h.

◆ SmartPointer() [4/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( T * pPtr,
bool bIsShared )
inline

Constructor that specifies ownership mode for a raw pointer.

Parameters
pPtrThe raw pointer to take ownership of.
bIsSharedIf true, enables reference counting (Shared mode).

Definition at line 134 of file SmartPointer.h.

◆ SmartPointer() [5/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( std::nullptr_t )
inline

Explicit null constructor.

Definition at line 143 of file SmartPointer.h.

◆ ~SmartPointer()

template<typename T>
DotNetDupe::System::SmartPointer< T >::~SmartPointer ( )
inline

Destructor. Cleans up the managed object based on ownership mode.

Definition at line 148 of file SmartPointer.h.

◆ SmartPointer() [6/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( const SmartPointer< T > & objOther)
inline

Copy constructor. Only permitted if the source is in Shared mode.

Exceptions
SystemExceptionIf the source pointer is in Unique mode.

Definition at line 158 of file SmartPointer.h.

◆ SmartPointer() [7/9]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( const SmartPointer< U > & objOther)
inline

Definition at line 170 of file SmartPointer.h.

◆ SmartPointer() [8/9]

template<typename T>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( SmartPointer< T > && objOther)
inlinenoexcept

Move constructor. Transfers ownership from the source.

Definition at line 218 of file SmartPointer.h.

◆ SmartPointer() [9/9]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
DotNetDupe::System::SmartPointer< T >::SmartPointer ( SmartPointer< U > && objOther)
inlinenoexcept

Definition at line 225 of file SmartPointer.h.

Member Function Documentation

◆ Attach()

template<typename T>
void DotNetDupe::System::SmartPointer< T >::Attach ( T * pPtr,
bool bIsShared = false )
inline

Attaches a new raw pointer to the SmartPointer.

Parameters
pPtrThe new pointer to manage.
bIsSharedOwnership mode for the new pointer.

Definition at line 345 of file SmartPointer.h.

◆ ConstCast() [1/2]

template<typename T>
template<typename U>
SmartPointer< U > DotNetDupe::System::SmartPointer< T >::ConstCast ( ) const
inline

Const casts the managed pointer to another type U and returns a new SmartPointer sharing ownership.

Definition at line 419 of file SmartPointer.h.

◆ ConstCast() [2/2]

template<typename T>
template<typename From>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::ConstCast ( const SmartPointer< From > & sp)
inlinestatic

Definition at line 436 of file SmartPointer.h.

◆ Detach()

template<typename T>
T * DotNetDupe::System::SmartPointer< T >::Detach ( )
inline

Detaches the managed object and returns it. The SmartPointer will no longer own the object.

Returns
The raw pointer to the object.

Definition at line 378 of file SmartPointer.h.

◆ DynamicCast() [1/2]

template<typename T>
template<typename U>
SmartPointer< U > DotNetDupe::System::SmartPointer< T >::DynamicCast ( ) const
inline

Dynamically casts the managed pointer to another type U and returns a new SmartPointer sharing ownership.

Definition at line 399 of file SmartPointer.h.

Referenced by DotNetDupe::System::Text::TextEncoding::UTF8().

◆ DynamicCast() [2/2]

template<typename T>
template<typename From>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::DynamicCast ( const SmartPointer< From > & sp)
inlinestatic

Definition at line 426 of file SmartPointer.h.

◆ Get()

◆ GetRefCount()

template<typename T>
int DotNetDupe::System::SmartPointer< T >::GetRefCount ( ) const
inlinenoexcept

Gets the current reference count. Returns 0 for Unique or Null pointers.

Definition at line 443 of file SmartPointer.h.

◆ IsNull()

◆ MakeShared() [1/2]

template<typename T>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::MakeShared ( )
inlinestatic

Alias for NewShared (default construction).

Definition at line 326 of file SmartPointer.h.

◆ MakeShared() [2/2]

template<typename T>
template<typename Arg1, typename... Args>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::MakeShared ( Arg1 && arg1,
Args &&... args )
inlinestatic

Alias for NewShared. Provided for compatibility.

Definition at line 334 of file SmartPointer.h.

◆ MakeUnique() [1/2]

template<typename T>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::MakeUnique ( )
inlinestatic

Alias for NewUnique (default construction).

Definition at line 311 of file SmartPointer.h.

◆ MakeUnique() [2/2]

template<typename T>
template<typename Arg1, typename... Args>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::MakeUnique ( Arg1 && arg1,
Args &&... args )
inlinestatic

Alias for NewUnique. Provided for compatibility.

Definition at line 319 of file SmartPointer.h.

◆ New() [1/2]

template<typename T>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::New ( )
inlinestatic

Creates a new SmartPointer (default construction).

Definition at line 294 of file SmartPointer.h.

Referenced by DotNetDupe::System::Text::Json::JsonElement::operator=().

◆ New() [2/2]

template<typename T>
template<typename Arg1, typename... Args>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::New ( Arg1 && arg1,
Args &&... args )
inlinestatic

Creates a new SmartPointer with variadic arguments for T's constructor.

Definition at line 302 of file SmartPointer.h.

◆ NewShared() [1/2]

template<typename T>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::NewShared ( )
inlinestatic

Creates a Shared SmartPointer, default constructing T.

Definition at line 277 of file SmartPointer.h.

Referenced by DotNetDupe::System::Threading::ConditionVariable::ConditionVariable(), DotNetDupe::Extensions::Logging::FileLoggerProvider::FileLoggerProvider(), DotNetDupe::Extensions::Logging::FileLoggerProvider::FileLoggerProvider(), DotNetDupe::WebAppCore::Http::HttpContext::HttpContext(), DotNetDupe::System::Data::SqlClient::SqlCommand::SqlCommand(), DotNetDupe::System::Data::SqlClient::SqlCommand::SqlCommand(), DotNetDupe::System::Threading::Tasks::Task::Task(), DotNetDupe::System::Data::Common::DbParameterCollection::AddWithValue(), DotNetDupe::System::Data::Common::DbParameterCollection::AddWithValue(), DotNetDupe::System::Data::Common::DbParameterCollection::AddWithValue(), DotNetDupe::WebAppCore::Builder::WebApplicationBuilder::Build(), DotNetDupe::Extensions::Logging::LogManager::Configure(), DotNetDupe::WebAppCore::Builder::WebApplication::CreateBuilder(), DotNetDupe::System::Data::SqlClient::SqlConnection::CreateCommand(), DotNetDupe::Extensions::Logging::ConsoleLoggerProvider::CreateLogger(), DotNetDupe::Extensions::Logging::FileLoggerProvider::CreateLogger(), DotNetDupe::Extensions::Logging::LoggerFactory::CreateLogger(), DotNetDupe::System::Diagnostics::SystemMetrics::CreateProcessStreamer(), DotNetDupe::Extensions::DependencyInjection::ServiceScopeFactory::CreateScope(), DotNetDupe::Extensions::DependencyInjection::ServiceDescriptor::Describe(), DotNetDupe::System::Diagnostics::EnumerateWin32Processes(), DotNetDupe::System::Data::SqlClient::SqlCommand::ExecuteReader(), DotNetDupe::System::Diagnostics::Process::GetProcessById(), DotNetDupe::Extensions::DependencyInjection::ServiceProvider::GetService(), DotNetDupe::System::TimeProvider::GetSystem(), DotNetDupe::Extensions::DependencyInjection::InstantiateService(), DotNetDupe::System::IdentityModel::Tokens::Jwt::JWTToken::Parse(), DotNetDupe::System::Net::Http::RestClient< TResource >::Post(), DotNetDupe::System::Net::Http::RestClient< TResource >::PostAndReturn(), DotNetDupe::WebAppCore::Builder::ProcessWsSession(), DotNetDupe::System::Net::Http::RestClient< TResource >::Put(), DotNetDupe::System::Data::SqlClient::RegisterSelectedBackend(), DotNetDupe::System::Net::Http::FileDownloader::Resume(), DotNetDupe::System::Threading::Tasks::Task::Run(), DotNetDupe::System::Diagnostics::Process::Start(), DotNetDupe::System::Net::Http::FileDownloader::Start(), DotNetDupe::System::Threading::AutoResetEvent::TryOpenExisting(), DotNetDupe::System::Threading::EventWaitHandle::TryOpenExisting(), DotNetDupe::System::Threading::ManualResetEvent::TryOpenExisting(), DotNetDupe::System::Threading::Mutex::TryOpenExisting(), DotNetDupe::System::Threading::Semaphore::TryOpenExisting(), and DotNetDupe::System::Text::TextEncoding::UTF8().

◆ NewShared() [2/2]

template<typename T>
template<typename Arg1, typename... Args>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::NewShared ( Arg1 && arg1,
Args &&... args )
inlinestatic

Creates a Shared SmartPointer, forwarding arguments to T's constructor.

Definition at line 285 of file SmartPointer.h.

◆ NewUnique() [1/2]

template<typename T>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::NewUnique ( )
inlinestatic

Creates a Unique SmartPointer, default constructing T.

Definition at line 262 of file SmartPointer.h.

◆ NewUnique() [2/2]

template<typename T>
template<typename Arg1, typename... Args>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::NewUnique ( Arg1 && arg1,
Args &&... args )
inlinestatic

Creates a Unique SmartPointer, forwarding arguments to T's constructor.

Definition at line 270 of file SmartPointer.h.

◆ operator bool()

template<typename T>
DotNetDupe::System::SmartPointer< T >::operator bool ( ) const
inlineexplicitnoexcept

Definition at line 451 of file SmartPointer.h.

◆ operator!=() [1/4]

template<typename T>
bool DotNetDupe::System::SmartPointer< T >::operator!= ( const SmartPointer< T > & other) const
inlinenoexcept

Definition at line 457 of file SmartPointer.h.

◆ operator!=() [2/4]

template<typename T>
template<typename U>
bool DotNetDupe::System::SmartPointer< T >::operator!= ( const SmartPointer< U > & other) const
inlinenoexcept

Definition at line 467 of file SmartPointer.h.

◆ operator!=() [3/4]

template<typename T>
template<typename U>
bool DotNetDupe::System::SmartPointer< T >::operator!= ( const U * pOther) const
inlinenoexcept

Definition at line 493 of file SmartPointer.h.

◆ operator!=() [4/4]

template<typename T>
bool DotNetDupe::System::SmartPointer< T >::operator!= ( std::nullptr_t ) const
inlinenoexcept

Definition at line 475 of file SmartPointer.h.

◆ operator*()

template<typename T>
T & DotNetDupe::System::SmartPointer< T >::operator* ( ) const
inline

Definition at line 449 of file SmartPointer.h.

◆ operator->()

template<typename T>
T * DotNetDupe::System::SmartPointer< T >::operator-> ( ) const
inlinenoexcept

Definition at line 450 of file SmartPointer.h.

◆ operator=() [1/4]

template<typename T>
SmartPointer & DotNetDupe::System::SmartPointer< T >::operator= ( const SmartPointer< T > & objOther)
inline

Copy assignment operator. Only permitted if the source is in Shared mode.

Definition at line 184 of file SmartPointer.h.

◆ operator=() [2/4]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
SmartPointer & DotNetDupe::System::SmartPointer< T >::operator= ( const SmartPointer< U > & objOther)
inline

Definition at line 200 of file SmartPointer.h.

◆ operator=() [3/4]

template<typename T>
SmartPointer & DotNetDupe::System::SmartPointer< T >::operator= ( SmartPointer< T > && objOther)
inlinenoexcept

Move assignment operator. Transfers ownership from the source.

Definition at line 234 of file SmartPointer.h.

◆ operator=() [4/4]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
SmartPointer & DotNetDupe::System::SmartPointer< T >::operator= ( SmartPointer< U > && objOther)
inlinenoexcept

Definition at line 246 of file SmartPointer.h.

◆ operator==() [1/4]

template<typename T>
bool DotNetDupe::System::SmartPointer< T >::operator== ( const SmartPointer< T > & other) const
inlinenoexcept

Definition at line 453 of file SmartPointer.h.

◆ operator==() [2/4]

template<typename T>
template<typename U>
bool DotNetDupe::System::SmartPointer< T >::operator== ( const SmartPointer< U > & other) const
inlinenoexcept

Definition at line 462 of file SmartPointer.h.

◆ operator==() [3/4]

template<typename T>
template<typename U>
bool DotNetDupe::System::SmartPointer< T >::operator== ( const U * pOther) const
inlinenoexcept

Definition at line 488 of file SmartPointer.h.

◆ operator==() [4/4]

template<typename T>
bool DotNetDupe::System::SmartPointer< T >::operator== ( std::nullptr_t ) const
inlinenoexcept

Definition at line 471 of file SmartPointer.h.

◆ Reset() [1/2]

template<typename T>
void DotNetDupe::System::SmartPointer< T >::Reset ( T * pPtr,
bool bIsShared )
inline

Resets the SmartPointer with a specific ownership mode.

Parameters
pPtrThe raw pointer to manage.
bIsSharedIf true, enables reference counting.

Definition at line 366 of file SmartPointer.h.

◆ Reset() [2/2]

template<typename T>
void DotNetDupe::System::SmartPointer< T >::Reset ( T * pPtr = nullptr)
inline

Resets the SmartPointer to null or a new object in Unique mode.

Parameters
pPtrOptional new raw pointer to manage.

Definition at line 355 of file SmartPointer.h.

◆ StaticCast() [1/2]

template<typename T>
template<typename U>
SmartPointer< U > DotNetDupe::System::SmartPointer< T >::StaticCast ( ) const
inline

Statically casts the managed pointer to another type U and returns a new SmartPointer sharing ownership.

Definition at line 409 of file SmartPointer.h.

◆ StaticCast() [2/2]

template<typename T>
template<typename From>
SmartPointer< T > DotNetDupe::System::SmartPointer< T >::StaticCast ( const SmartPointer< From > & sp)
inlinestatic

Definition at line 431 of file SmartPointer.h.


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