Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting Object. This pointer type follows intrusive pointer semantics. Reference counter is stored either in Object itself or in counter structure which is tied to Object instance tightly. In any case, all SmartPtr instances form single ownership group regardless how they were created which is unlike how std::shared_ptr class behaves. Converting raw pointer to SmartPtr is safe given there are other SmartPtr instances holding shared references to the same object. SmartPtr class instance can be in one of two states: shared pointer and weak pointer. To keep object alive, one should have count of shared references to it positive. Both weak and shared pointers can be used to access pointed object (to call methods, read or write fields, etc.), but weak pointers do not participate to shared pointer reference counting. Object is being deleted when the last 'shared' SmartPtr pointer to it is being destroyed. So, make sure that this doesn't happen when no other shared SmartPtr pointers to object exist, e. g. during object construction or destruction. Use System::Object::ThisProtector sentry objects (in C++ code) or CppCTORSelfReference or CppSelfReference attribute (in C# code being translated) to fix this issue. Similarily, make sure to break loop references by using System::WeakPtr pointer class or System::SmartPtrMode::Weak pointer mode (in C++ code) or CppWeakPtr attribute (in C# code being translated). If two or more objects reference each other using 'shared' pointers, they will never be deleted. If pointer type (weak or shared) should be switched in runtime, use System::SmartPtr<T>::set_Mode() method or System::DynamicWeakPtr class. SmartPtr class doesn't contain any virtual methods. You should only inherit it if you're creating a memory management strategy of your own. This type is a pointer to manage other object's deletion. It should be allocated on stack and passed to functions either by value or by const reference. More...

#include "fwd.h"

Inherited by System::Collections::BitArrayPtr, System::Collections::Generic::DictionaryPtr< T, V >, System::Collections::Generic::HashSetPtr< T >, System::Collections::Generic::ListPtr< T >, System::Collections::Generic::QueuePtr< T >, System::Collections::Generic::SortedDictionaryPtr< T, V >, System::Collections::Generic::StackPtr< T >, System::Collections::Specialized::StringCollectionPtr, System::DynamicWeakPtr< T, trunkMode, weakLeafs >, System::Security::Cryptography::X509Certificates::X509Certificate2CollectionPtr, System::Security::Cryptography::X509Certificates::X509CertificateCollectionPtr, System::Security::Cryptography::X509Certificates::X509ExtensionCollectionPtr, System::Text::RegularExpressions::GroupCollectionPtr, and System::WeakPtr< T >.

Classes

class  Data
 Internal data storage class which hides data members and enforces neccessary asserts around them. More...
 

Public Types

typedef T Pointee_
 Pointed type. More...
 
typedef SmartPtr< T > SmartPtr_
 Specialized smart pointer type. More...
 
typedef System::Details::ArrayTypeResolver< T >::type ArrayType
 Same as Pointee_, if it is a specialization of System::Array, and void otherwise. More...
 
using ValueType = typename System::Details::SelectType< typename System::Details::ArrayTypeResolver< T >::value_type >::type
 Storage type of pointed array. Only meaningful if T is a specialization of System::Array. More...
 

Public Member Functions

 SmartPtr (SmartPtrMode mode)
 Creates SmartPtr object of required mode. More...
 
 SmartPtr (std::nullptr_t=nullptr, SmartPtrMode mode=SmartPtrMode::Shared)
 Creates null-pointer SmartPtr object of required mode. More...
 
 SmartPtr (Pointee_ *object, SmartPtrMode mode=SmartPtrMode::Shared)
 Creates SmartPtr pointing to specified object, or converts raw pointer to SmartPtr. More...
 
 SmartPtr (const SmartPtr_ &ptr, SmartPtrMode mode=SmartPtrMode::Shared)
 Copy constructs SmartPtr object. Both pointers point to the same object afterwards. More...
 
template<class Q , typename = typename std::enable_if<System::detail::is_pointer_convertible<Q, Pointee_>::type::value>::type>
 SmartPtr (const SmartPtr< Q > &x, SmartPtrMode mode=SmartPtrMode::Shared)
 Copy constructs SmartPtr object. Both pointers point to the same object afterwards. Performs type conversion if allowed. More...
 
 SmartPtr (SmartPtr_ &&x, SmartPtrMode mode=SmartPtrMode::Shared) noexcept
 Move constructs SmartPtr object. Effectively, swaps two pointers, if they are both of same mode. x may be unusable after call. More...
 
template<typename Y >
 SmartPtr (const SmartPtr< Array< Y >> &src, SmartPtrMode mode=SmartPtrMode::Shared)
 Converts type of referenced array by creating a new array of different type. Useful if in C# there is an array type cast which is unsupported in C++. More...
 
template<typename Y , typename = typename std::enable_if<std::is_same<Y, EmptyArrayInitializer>::value, void>::type>
 SmartPtr (const Y &)
 Initializes empty array. Used to translate some C# code constructs. More...
 
template<typename P >
 SmartPtr (const SmartPtr< P > &ptr, Pointee_ *p, SmartPtrMode mode=SmartPtrMode::Shared)
 Constructs a SmartPtr which shares ownership information with the initial value of ptr, but holds an unrelated and unmanaged pointer p. More...
 
 ~SmartPtr ()
 Destroys SmartPtr object. If required, decreases pointed object's reference counter and deletes object. More...
 
SmartPtr_operator= (SmartPtr_ &&x) noexcept
 Move-assigns SmartPtr object. x becomes unusable. More...
 
SmartPtr_operator= (const SmartPtr_ &x)
 Copy-assigns SmartPtr object. More...
 
template<typename Q >
SmartPtr_operator= (const SmartPtr< Q > &x)
 Copy-assigns SmartPtr object. Does required type conversions. More...
 
SmartPtr_operator= (Pointee_ *p)
 Assigns raw pointer to SmartPtr object. More...
 
SmartPtr_operator= (std::nullptr_t)
 Sets pointer value to nullptr. More...
 
Pointee_operator-> () const
 Allows to access members of referenced object. More...
 
bool operator== (std::nullptr_t) const
 Checks if pointer points to nullptr. More...
 
Pointee_get () const
 Gets pointed object. More...
 
Pointee_get_shared () const
 Gets pointed object, but asserts that pointer is in shared mode. More...
 
void reset (Pointee_ *ptr)
 Sets pointed object. More...
 
void reset ()
 Makes pointer pointing to nullptr. More...
 
SmartPtrMode get_Mode () const
 Gets pointer mode. More...
 
bool IsShared () const
 Checks if pointer is in shared mode. More...
 
bool IsWeak () const
 Checks if pointer is in weak mode. More...
 
bool IsAliasingPtr () const
 Checks if pointer is pointed to another object than owned (created by an aliasing constructor). More...
 
ObjectGetObjectOwner () const
 Gets referenced object. More...
 
SmartPtr_ RemoveAliasing () const
 Removes aliasing (created by an aliasing constructor) from pointer, makes sure it manages (if shared) or tracks (if weak) the same object it points to. More...
 
void set_Mode (SmartPtrMode mode)
 Sets pointer mode. May alter referenced object's reference counts. More...
 
Pointee_operator* () const
 Gets reference to pointed object. Asserts that pointer is not null. More...
 
 operator bool () const noexcept
 Checks if pointer is not null. More...
 
bool operator! () const noexcept
 Checks if pointer is null. More...
 
template<class Y >
bool operator< (Y *p) const
 Provides less-compare semantics for SmartPtr class. More...
 
template<class Y >
bool operator< (SmartPtr< Y > const &x) const
 Provides less-compare semantics for SmartPtr class. More...
 
template<class Y >
SmartPtr< Y > static_pointer_cast () const
 Casts pointer to different type using static_cast on pointed object. More...
 
template<class Y >
SmartPtr< Y > dynamic_pointer_cast () const
 Casts pointer to different type using dynamic_cast on pointed object. More...
 
template<class Y >
SmartPtr< Y > const_pointer_cast () const
 Casts pointer to different type using const_cast on pointed object. More...
 
template<class Y , typename Check = std::false_type>
std::enable_if_t< std::is_same< Y, T >::value, SmartPtr< Y > > Cast () const
 Casts pointer to its type itself. More...
 
template<class Y , typename Check = std::false_type>
std::enable_if_t<!std::is_same< Y, T >::value &&std::is_base_of< Y, T >::value, SmartPtr< Y > > Cast () const
 Casts pointer to base type using static_cast. More...
 
template<class Y , typename Check = std::false_type>
std::enable_if_t< Check::value &&!std::is_same< Y, T >::value &&!std::is_base_of< Y, T >::value, SmartPtr< Y > > Cast () const
 Casts pointer to derived type dynamic_cast. More...
 
template<class Y , typename Check = std::false_type>
std::enable_if_t<!Check::value &&!std::is_same< Y, T >::value &&!std::is_base_of< Y, T >::value, SmartPtr< Y > > Cast () const
 Casts pointer to derived type dynamic_cast. More...
 
bool Is (const System::TypeInfo &target) const
 Checks if pointed object is of specific type or its child type. Follows C# 'is' semantics. More...
 
ObjectGetObjectOrNull () const
 Gets pointed object (if any) or nullptr. Same as get(). More...
 
SmartPtr< ObjectToObjectPtr () const
 Converts any pointer type to pointer to Object. Doesn't require Pointee_ type to be complete. More...
 
Pointee_GetPointer () const
 Gets pointed object (if any) or nullptr. Same as get(). More...
 
int get_shared_count () const
 Gets number of shared pointers existing to referenced object, including current one. Asserts current pointer being in shared mode. More...
 
void SetContainedTemplateWeakPtr (uint32_t argument) const
 Calls SetTemplateWeakPtr() method on pointed object (if any). More...
 
template<typename Q = T>
auto begin () -> decltype(std::declval< Q >().begin())
 Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization type with begin() method. More...
 
template<typename Q = T>
auto end () -> decltype(std::declval< Q >().end())
 Accessor for end() method of an underling collection. Only compiles if SmartPtr_ is specialization type with end() method. More...
 
template<typename Q = T>
auto begin () const -> decltype(std::declval< const Q >().begin())
 Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization type with begin() method. More...
 
template<typename Q = T>
auto end () const -> decltype(std::declval< const Q >().end())
 Accessor for end() method of an underling collection. Only compiles if SmartPtr_ is specialization type with end() method. More...
 
template<typename Q = T>
auto cbegin () const -> decltype(std::declval< const Q >().cbegin())
 Accessor for cbegin() method of an underling collection. Only compiles if SmartPtr_ is specialization type with cbegin() method. More...
 
template<typename Q = T>
auto cend () const -> decltype(std::declval< const Q >().cend())
 Accessor for cend() method of an underling collection. Only compiles if SmartPtr_ is specialization type with cend() method. More...
 
int GetHashCode () const
 Calls GetHashCode() on pointed object. More...
 
T * GetObjectNotNull () const
 Gets currently referenced object (if any) or throws. More...
 

Static Public Member Functions

static const System::TypeInfoType ()
 Shortcut to get System::TypeInfo object for the Pointee_ type. More...
 

Public Attributes

template<typename IdxType >
decltype(System::Details::GetByIndex(std::declval< const SmartPtr_ * >(), std::declval< IdxType >()) operator[] )(IdxType idx) const
 Accessor for array elements. Only compiles if SmartPtr_ is specialization of System::Array. More...
 

Protected Types

typedef Object SharedRefReleaser
 Type to use to release shared pointers. Depends on whether external refcount is on or off. More...
 

Protected Member Functions

void Lock (Pointee_ *object)
 Sets pointee object. Increments shared or weak reference count, depending on pointer mode. More...
 
template<class Q >
void Lock (const SmartPtr< Q > &ptr)
 Sets pointee object. Increments shared or weak reference count, depending on pointer mode. More...
 
template<typename Q >
void LockSharedFromShared (const SmartPtr< Q > &ptr)
 Sets pointee object. Asserts that both current object and ptr are in shared mode. More...
 
template<typename Q >
void LockSharedFromWeak (const SmartPtr< Q > &ptr)
 Sets pointee object. Asserts that current object is in shared mode and ptr is in weak mode. More...
 
template<typename Q >
void LockWeakFromShared (const SmartPtr< Q > &ptr)
 Sets pointee object. Asserts that current object is in weak mode and ptr is in shared mode. More...
 
template<typename Q >
void LockWeakFromWeak (const SmartPtr< Q > &ptr)
 Sets pointee object. Asserts that both current object and ptr are in weak mode. More...
 
ObjectReleaseAndGetObjectToDelete ()
 Decrements currently referenced object's shared or weak pointer counter, depending on current pointer mode. More...
 
SharedRefReleaserGetSharedReleaser () const
 Gets object to use to release shared pointer to. More...
 
ObjectReleaseSharedAndGetObjectToDelete ()
 Decrements currently referenced object's shared pointer counter. More...
 
void ReleaseWeak ()
 Decrements currently referenced object's weak pointer counter. More...
 
void MoveSharedFromWeak (SmartPtr &&x)
 Implements move semantics. Asserts that current object is in shared mode and x is in weak mode. More...
 
void MoveWeakFromShared (SmartPtr &&x)
 Implements move semantics. Asserts that current object is in weak mode and x is in shared mode. More...
 
template<typename Q , typename R = decltype(std::declval<Q*>()->GetHashCode())>
GetHashCodeImpl (Q *) const
 Calls into GetHashCode() method if it is available on Pointee_ type (which is true if it is a complete type). More...
 
int GetHashCodeImpl (void *) const
 Calls GetHashCode() method from Object if it is not available on Pointee_ type (e. g. if it is incomplete). More...
 
template<typename Q >
void Assign (const SmartPtr< Q > &x)
 Copy-assigns SmartPtr object. Does type conversions, if required. More...
 

Static Protected Member Functions

static ObjectReleaseSharedAndGetObjectToDelete (SharedRefReleaser *releaser)
 Removes shared pointer of a specific object, possibly deleting it. More...
 
static void ReleaseWeak (System::Detail::SmartPtrCounter *counter)
 Decrements weak pointer counter. More...
 
template<typename X , typename Y >
static void InitArray (SmartPtr< Array< X >> *ptr, const SmartPtr< Array< Y >> &src)
 Performs actual array copying on cast constructor calls. More...
 

Protected Attributes

class System::SmartPtr::Data m_data
 An instance of Data class. More...
 

Detailed Description

template<class T>
class System::SmartPtr< T >

Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting Object. This pointer type follows intrusive pointer semantics. Reference counter is stored either in Object itself or in counter structure which is tied to Object instance tightly. In any case, all SmartPtr instances form single ownership group regardless how they were created which is unlike how std::shared_ptr class behaves. Converting raw pointer to SmartPtr is safe given there are other SmartPtr instances holding shared references to the same object. SmartPtr class instance can be in one of two states: shared pointer and weak pointer. To keep object alive, one should have count of shared references to it positive. Both weak and shared pointers can be used to access pointed object (to call methods, read or write fields, etc.), but weak pointers do not participate to shared pointer reference counting. Object is being deleted when the last 'shared' SmartPtr pointer to it is being destroyed. So, make sure that this doesn't happen when no other shared SmartPtr pointers to object exist, e. g. during object construction or destruction. Use System::Object::ThisProtector sentry objects (in C++ code) or CppCTORSelfReference or CppSelfReference attribute (in C# code being translated) to fix this issue. Similarily, make sure to break loop references by using System::WeakPtr pointer class or System::SmartPtrMode::Weak pointer mode (in C++ code) or CppWeakPtr attribute (in C# code being translated). If two or more objects reference each other using 'shared' pointers, they will never be deleted. If pointer type (weak or shared) should be switched in runtime, use System::SmartPtr<T>::set_Mode() method or System::DynamicWeakPtr class. SmartPtr class doesn't contain any virtual methods. You should only inherit it if you're creating a memory management strategy of your own. This type is a pointer to manage other object's deletion. It should be allocated on stack and passed to functions either by value or by const reference.

Template Parameters
TType of the pointed object. Must be either System::Object or subclass of it.
#include "system/object.h"
#include "system/smart_ptr.h"
#include <iostream>
// The forward declaration of ChildNode.
class ChildNode;
// This class contains SharedPtr to the ChildNode-class instance.
class ParentNode: public System::Object
{
public:
};
// This class stores an int32_t value and contains WeakPtr to the ParentNode-class instance to exclude circular
// dependencies.
class ChildNode: public System::Object
{
int32_t m_value;
public:
explicit ChildNode(int32_t value) : m_value(value) {}
int32_t getValue() const
{
return m_value;
}
};
// The function constructs instances and creates dependencies between instances of the ParentNode and ChildNode classes.
{
auto parent = System::MakeObject<ParentNode>();
auto child = System::MakeObject<ChildNode>(16);
parent->childNode = child;
child->parentNode = parent;
return parent;
}
// This function prints information about instances.
void PrintInfo(System::WeakPtr<ChildNode> &childNode)
{
std::cout << "The pointer to an instance of the ChildNode class has expired: " << (childNode.expired() ? "True" : "False") << std::endl;
if (!childNode.expired())
{
std::cout << "Stored value: " << childNode->getValue() << std::endl;
std::cout << "The pointer to a parent node has expired: " << (childNode->parentNode.expired() ? "True" : "False") << std::endl;
}
}
int main()
{
{
auto parent = getParenNode();
child = parent->childNode;
PrintInfo(child);
std::cout << "------" << std::endl;
}
PrintInfo(child);
return 0;
}
/*
This code example produces the following output:
The pointer to an instance of the ChildNode class has expired: False
Stored value: 16
The pointer to a parent node has expired: False
------
The pointer to an instance of the ChildNode class has expired: True
*/

Member Typedef Documentation

◆ ArrayType

template<class T>
typedef System::Details::ArrayTypeResolver<T>::type System::SmartPtr< T >::ArrayType

Same as Pointee_, if it is a specialization of System::Array, and void otherwise.

◆ Pointee_

template<class T>
typedef T System::SmartPtr< T >::Pointee_

Pointed type.

◆ SharedRefReleaser

template<class T>
typedef Object System::SmartPtr< T >::SharedRefReleaser
protected

Type to use to release shared pointers. Depends on whether external refcount is on or off.

◆ SmartPtr_

template<class T>
typedef SmartPtr<T> System::SmartPtr< T >::SmartPtr_

Specialized smart pointer type.

◆ ValueType

template<class T>
using System::SmartPtr< T >::ValueType = typename System::Details::SelectType<typename System::Details::ArrayTypeResolver<T>::value_type>::type

Storage type of pointed array. Only meaningful if T is a specialization of System::Array.

Constructor & Destructor Documentation

◆ SmartPtr() [1/9]

template<class T>
System::SmartPtr< T >::SmartPtr ( SmartPtrMode  mode)
inline

Creates SmartPtr object of required mode.

Parameters
modePointer mode.

◆ SmartPtr() [2/9]

template<class T>
System::SmartPtr< T >::SmartPtr ( std::nullptr_t  = nullptr,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inline

Creates null-pointer SmartPtr object of required mode.

Parameters
modePointer mode.

◆ SmartPtr() [3/9]

template<class T>
System::SmartPtr< T >::SmartPtr ( Pointee_ object,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inline

Creates SmartPtr pointing to specified object, or converts raw pointer to SmartPtr.

Parameters
objectPointee.
modePointer mode.

◆ SmartPtr() [4/9]

template<class T>
System::SmartPtr< T >::SmartPtr ( const SmartPtr_ ptr,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inline

Copy constructs SmartPtr object. Both pointers point to the same object afterwards.

Parameters
ptrPointer to copy.
modePointer mode.

◆ SmartPtr() [5/9]

template<class T>
template<class Q , typename = typename std::enable_if<System::detail::is_pointer_convertible<Q, Pointee_>::type::value>::type>
System::SmartPtr< T >::SmartPtr ( const SmartPtr< Q > &  x,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inline

Copy constructs SmartPtr object. Both pointers point to the same object afterwards. Performs type conversion if allowed.

Template Parameters
QType of object pointed by x.
Parameters
xPointer to copy.
modePointer mode.

◆ SmartPtr() [6/9]

template<class T>
System::SmartPtr< T >::SmartPtr ( SmartPtr_ &&  x,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inlinenoexcept

Move constructs SmartPtr object. Effectively, swaps two pointers, if they are both of same mode. x may be unusable after call.

Parameters
xPointer to move.
modePointer mode.

◆ SmartPtr() [7/9]

template<class T>
template<typename Y >
System::SmartPtr< T >::SmartPtr ( const SmartPtr< Array< Y >> &  src,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inlineexplicit

Converts type of referenced array by creating a new array of different type. Useful if in C# there is an array type cast which is unsupported in C++.

Template Parameters
YType of source array.
Parameters
srcPointer to array to create a copy of, but with different type of elements.
modePointer mode.

◆ SmartPtr() [8/9]

template<class T>
template<typename Y , typename = typename std::enable_if<std::is_same<Y, EmptyArrayInitializer>::value, void>::type>
System::SmartPtr< T >::SmartPtr ( const Y &  )
inlineexplicit

Initializes empty array. Used to translate some C# code constructs.

Template Parameters
YPlaceholder of EmptyArrayInitializer type.

◆ SmartPtr() [9/9]

template<class T>
template<typename P >
System::SmartPtr< T >::SmartPtr ( const SmartPtr< P > &  ptr,
Pointee_ p,
SmartPtrMode  mode = SmartPtrMode::Shared 
)
inline

Constructs a SmartPtr which shares ownership information with the initial value of ptr, but holds an unrelated and unmanaged pointer p.

Parameters
ptrAnother smart pointer to share the ownership to the ownership from.
pPointer to an object to manage.
modePointer mode.
#include "system/object.h"
#include "system/smart_ptr.h"
#include <iostream>
// This class contains a field that will be printed.
class Foo : public System::Object
{
public:
std::string value = "Hello, world!";
};
// This class contains an instance of the Foo class.
class Bar : public System::Object
{
public:
Foo data;
};
// Used to print a string from the Foo-class instance.
void PrintMessage(const System::SharedPtr<Foo> &foo)
{
std::cout << foo->value << std::endl;
}
// Prints the number of shared pointers pointing to the object.
void PrintSharedCount(const System::SharedPtr<Bar> &ptr)
{
std::cout << "Number of shared pointers: " << ptr.get_shared_count() << std::endl;
}
int main()
{
// Create SharedPtr to an instance of the Bar class.
auto bar = System::MakeObject<Bar>();
PrintSharedCount(bar);
// Create SharedPtr that will point to the field of the Bar-class instance.
auto foo = System::SharedPtr<Foo>(bar, &bar->data);
PrintSharedCount(bar);
// Make the 'bar' pointer pointing to nullptr.
bar.reset();
PrintSharedCount(bar);
// bar->data still exists and the 'foo' pointer is valid.
PrintMessage(foo);
return 0;
}
/*
This code example produces the following output:
Number of shared pointers: 1
Number of shared pointers: 2
Number of shared pointers: 0
Hello, world!
*/

◆ ~SmartPtr()

template<class T>
System::SmartPtr< T >::~SmartPtr ( )
inline

Destroys SmartPtr object. If required, decreases pointed object's reference counter and deletes object.

Member Function Documentation

◆ Assign()

template<class T>
template<typename Q >
void System::SmartPtr< T >::Assign ( const SmartPtr< Q > &  x)
inlineprotected

Copy-assigns SmartPtr object. Does type conversions, if required.

Template Parameters
QType of object pointed by x.
Parameters
xPointer to copy-assign.

◆ begin() [1/2]

template<class T>
template<typename Q = T>
auto System::SmartPtr< T >::begin ( ) -> decltype(std::declval<Q>().begin())
inline

Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization type with begin() method.

Returns
iterator to the begin of collection

◆ begin() [2/2]

template<class T>
template<typename Q = T>
auto System::SmartPtr< T >::begin ( ) const -> decltype(std::declval<const Q>().begin())
inline

Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization type with begin() method.

Returns
iterator to the begin of collection

◆ Cast() [1/4]

template<class T>
template<class Y , typename Check = std::false_type>
std::enable_if_t<std::is_same<Y, T>::value, SmartPtr<Y> > System::SmartPtr< T >::Cast ( ) const
inline

Casts pointer to its type itself.

Template Parameters
YTarget type of pointed object.
CheckFlags to throw exception if no cast available.
Returns
Pointer of changed type which is always in shared mode.

◆ Cast() [2/4]

template<class T>
template<class Y , typename Check = std::false_type>
std::enable_if_t<!std::is_same<Y, T>::value && std::is_base_of<Y, T>::value, SmartPtr<Y> > System::SmartPtr< T >::Cast ( ) const
inline

Casts pointer to base type using static_cast.

Template Parameters
YTarget type of pointed object.
CheckFlags to throw exception if no cast available.
Returns
Pointer of changed type which is always in shared mode.

◆ Cast() [3/4]

template<class T>
template<class Y , typename Check = std::false_type>
std::enable_if_t<Check::value && !std::is_same<Y, T>::value && !std::is_base_of<Y, T>::value, SmartPtr<Y> > System::SmartPtr< T >::Cast ( ) const
inline

Casts pointer to derived type dynamic_cast.

Template Parameters
YTarget type of pointed object.
CheckFlags to throw exception if no cast available.
Returns
Pointer of changed type which is always in shared mode. Throws InvalidCastException if no conversion available.

◆ Cast() [4/4]

template<class T>
template<class Y , typename Check = std::false_type>
std::enable_if_t<!Check::value && !std::is_same<Y, T>::value && !std::is_base_of<Y, T>::value, SmartPtr<Y> > System::SmartPtr< T >::Cast ( ) const
inline

Casts pointer to derived type dynamic_cast.

Template Parameters
YTarget type of pointed object.
CheckFlags to throw exception if no cast available.
Returns
Pointer of changed type which is always in shared mode. Returns nullptr if no conversion available.

◆ cbegin()

template<class T>
template<typename Q = T>
auto System::SmartPtr< T >::cbegin ( ) const -> decltype(std::declval<const Q>().cbegin())
inline

Accessor for cbegin() method of an underling collection. Only compiles if SmartPtr_ is specialization type with cbegin() method.

Returns
iterator to the begin of collection

◆ cend()

template<class T>
template<typename Q = T>
auto System::SmartPtr< T >::cend ( ) const -> decltype(std::declval<const Q>().cend())
inline

Accessor for cend() method of an underling collection. Only compiles if SmartPtr_ is specialization type with cend() method.

Returns
iterator to the end of collection

◆ const_pointer_cast()

template<class T>
template<class Y >
SmartPtr<Y> System::SmartPtr< T >::const_pointer_cast ( ) const
inline

Casts pointer to different type using const_cast on pointed object.

Template Parameters
YTarget type of pointed object.
Returns
Pointer of changed type which is always in shared mode.

◆ dynamic_pointer_cast()

template<class T>
template<class Y >
SmartPtr<Y> System::SmartPtr< T >::dynamic_pointer_cast ( ) const
inline

Casts pointer to different type using dynamic_cast on pointed object.

Template Parameters
YTarget type of pointed object.
Returns
Pointer of changed type which is always in shared mode.

◆ end() [1/2]

template<class T>
template<typename Q = T>
auto System::SmartPtr< T >::end ( ) -> decltype(std::declval<Q>().end())
inline

Accessor for end() method of an underling collection. Only compiles if SmartPtr_ is specialization type with end() method.

Returns
iterator to the end of collection

◆ end() [2/2]

template<class T>
template<typename Q = T>
auto System::SmartPtr< T >::end ( ) const -> decltype(std::declval<const Q>().end())
inline

Accessor for end() method of an underling collection. Only compiles if SmartPtr_ is specialization type with end() method.

Returns
iterator to the end of collection

◆ get()

template<class T>
Pointee_* System::SmartPtr< T >::get ( ) const
inline

Gets pointed object.

Returns
Raw pointer to referenced object.

◆ get_Mode()

template<class T>
SmartPtrMode System::SmartPtr< T >::get_Mode ( ) const
inline

Gets pointer mode.

Returns
Mode of pointer object.

◆ get_shared()

template<class T>
Pointee_* System::SmartPtr< T >::get_shared ( ) const
inline

Gets pointed object, but asserts that pointer is in shared mode.

Returns
Raw pointer to referenced object.

◆ get_shared_count()

template<class T>
int System::SmartPtr< T >::get_shared_count ( ) const
inline

Gets number of shared pointers existing to referenced object, including current one. Asserts current pointer being in shared mode.

Returns
Number of shared pointers existing to referenced object, if any. If pointer is null, returns 0.

◆ GetHashCode()

template<class T>
int System::SmartPtr< T >::GetHashCode ( ) const
inline

Calls GetHashCode() on pointed object.

Returns
Result of GetHashCode() call on referenced object (if any) or 0.

◆ GetHashCodeImpl() [1/2]

template<class T>
template<typename Q , typename R = decltype(std::declval<Q*>()->GetHashCode())>
R System::SmartPtr< T >::GetHashCodeImpl ( Q *  ) const
inlineprotected

Calls into GetHashCode() method if it is available on Pointee_ type (which is true if it is a complete type).

Template Parameters
QSame as Pointee_, needed for template magic here.
RResult of GetHashCode() call, should be int.

◆ GetHashCodeImpl() [2/2]

template<class T>
int System::SmartPtr< T >::GetHashCodeImpl ( void *  ) const
inlineprotected

Calls GetHashCode() method from Object if it is not available on Pointee_ type (e. g. if it is incomplete).

◆ GetObjectNotNull()

template<class T>
T* System::SmartPtr< T >::GetObjectNotNull ( ) const
inline

Gets currently referenced object (if any) or throws.

Returns
Raw pointer to referenced object.
Exceptions
System::NullReferenceExceptionThrown if called on null-pointer.

◆ GetObjectOrNull()

template<class T>
Object* System::SmartPtr< T >::GetObjectOrNull ( ) const
inline

Gets pointed object (if any) or nullptr. Same as get().

Returns
Raw pointer to referenced object (if any) or nullptr.

◆ GetObjectOwner()

template<class T>
Object* System::SmartPtr< T >::GetObjectOwner ( ) const
inline

Gets referenced object.

Returns
Return raw pointer to referenced object or nullptr.

◆ GetPointer()

template<class T>
Pointee_* System::SmartPtr< T >::GetPointer ( ) const
inline

Gets pointed object (if any) or nullptr. Same as get().

Returns
Raw pointer to referenced object (if any) or nullptr.

◆ GetSharedReleaser()

template<class T>
SharedRefReleaser* System::SmartPtr< T >::GetSharedReleaser ( ) const
inlineprotected

Gets object to use to release shared pointer to.

Returns
Pointer to owned object.

◆ InitArray()

template<class T>
template<typename X , typename Y >
static void System::SmartPtr< T >::InitArray ( SmartPtr< Array< X >> *  ptr,
const SmartPtr< Array< Y >> &  src 
)
inlinestaticprotected

Performs actual array copying on cast constructor calls.

Template Parameters
XTarget array element type.
YSource array element type.
Parameters
ptrTarget array pointer.
srcSource array pointer.

◆ Is()

template<typename T >
bool System::SmartPtr< T >::Is ( const System::TypeInfo target) const

Checks if pointed object is of specific type or its child type. Follows C# 'is' semantics.

Implementation.

Parameters
targetSpecifies target type to check against.
Returns
True if C# 'is'-style check is positive and false otherwise.

◆ IsAliasingPtr()

template<class T>
bool System::SmartPtr< T >::IsAliasingPtr ( ) const
inline

Checks if pointer is pointed to another object than owned (created by an aliasing constructor).

Returns
True if pointer has a difference in pointed and owned objects, false otherwise.

◆ IsShared()

template<class T>
bool System::SmartPtr< T >::IsShared ( ) const
inline

Checks if pointer is in shared mode.

Returns
True if pointer is in shared mode, false otherwise.

◆ IsWeak()

template<class T>
bool System::SmartPtr< T >::IsWeak ( ) const
inline

Checks if pointer is in weak mode.

Returns
True if pointer is in weak mode, false otherwise.

◆ Lock() [1/2]

template<class T>
void System::SmartPtr< T >::Lock ( Pointee_ object)
inlineprotected

Sets pointee object. Increments shared or weak reference count, depending on pointer mode.

Parameters
objectObject to lock.

◆ Lock() [2/2]

template<class T>
template<class Q >
void System::SmartPtr< T >::Lock ( const SmartPtr< Q > &  ptr)
inlineprotected

Sets pointee object. Increments shared or weak reference count, depending on pointer mode.

Template Parameters
QSource object type.
Parameters
ptrPointer to object to lock.

◆ LockSharedFromShared()

template<class T>
template<typename Q >
void System::SmartPtr< T >::LockSharedFromShared ( const SmartPtr< Q > &  ptr)
inlineprotected

Sets pointee object. Asserts that both current object and ptr are in shared mode.

Template Parameters
QSource object type.
Parameters
ptrPointer to object to lock.

◆ LockSharedFromWeak()

template<class T>
template<typename Q >
void System::SmartPtr< T >::LockSharedFromWeak ( const SmartPtr< Q > &  ptr)
inlineprotected

Sets pointee object. Asserts that current object is in shared mode and ptr is in weak mode.

Template Parameters
QSource object type.
Parameters
ptrPointer to object to lock.

◆ LockWeakFromShared()

template<class T>
template<typename Q >
void System::SmartPtr< T >::LockWeakFromShared ( const SmartPtr< Q > &  ptr)
inlineprotected

Sets pointee object. Asserts that current object is in weak mode and ptr is in shared mode.

Template Parameters
QSource object type.
Parameters
ptrPointer to object to lock.

◆ LockWeakFromWeak()

template<class T>
template<typename Q >
void System::SmartPtr< T >::LockWeakFromWeak ( const SmartPtr< Q > &  ptr)
inlineprotected

Sets pointee object. Asserts that both current object and ptr are in weak mode.

Template Parameters
QSource object type.
Parameters
ptrPointer to object to lock.

◆ MoveSharedFromWeak()

template<class T>
void System::SmartPtr< T >::MoveSharedFromWeak ( SmartPtr< T > &&  x)
inlineprotected

Implements move semantics. Asserts that current object is in shared mode and x is in weak mode.

Parameters
xPointer to move value from.

◆ MoveWeakFromShared()

template<class T>
void System::SmartPtr< T >::MoveWeakFromShared ( SmartPtr< T > &&  x)
inlineprotected

Implements move semantics. Asserts that current object is in weak mode and x is in shared mode.

Parameters
xPointer to move value from.

◆ operator bool()

template<class T>
System::SmartPtr< T >::operator bool ( ) const
inlineexplicitnoexcept

Checks if pointer is not null.

Returns
False if pointer is null, true otherwise.

◆ operator!()

template<class T>
bool System::SmartPtr< T >::operator! ( ) const
inlinenoexcept

Checks if pointer is null.

Returns
True if pointer is null, false otherwise.

◆ operator*()

template<class T>
Pointee_& System::SmartPtr< T >::operator* ( ) const
inline

Gets reference to pointed object. Asserts that pointer is not null.

Returns
Reference to pointed object.

◆ operator->()

template<class T>
Pointee_* System::SmartPtr< T >::operator-> ( ) const
inline

Allows to access members of referenced object.

Returns
Raw pointer to referenced object.
Exceptions
System::NullReferenceExceptionIf pointer is null.

◆ operator<() [1/2]

template<class T>
template<class Y >
bool System::SmartPtr< T >::operator< ( Y *  p) const
inline

Provides less-compare semantics for SmartPtr class.

Template Parameters
YType of pointer to compare current one to.
Parameters
pPointer to compare current one to.
Returns
True if the object referenced by SmartPtr is 'less' than p and false otherwise.

◆ operator<() [2/2]

template<class T>
template<class Y >
bool System::SmartPtr< T >::operator< ( SmartPtr< Y > const &  x) const
inline

Provides less-compare semantics for SmartPtr class.

Template Parameters
YType of pointer to compare current one to.
Parameters
xPointer to compare current one to.
Returns
True if the object referenced by SmartPtr is 'less' than x and false otherwise.

◆ operator=() [1/5]

template<class T>
SmartPtr_& System::SmartPtr< T >::operator= ( SmartPtr_ &&  x)
inlinenoexcept

Move-assigns SmartPtr object. x becomes unusable.

Parameters
xPointer to move-assign.
Returns
Reference to this object.

◆ operator=() [2/5]

template<class T>
SmartPtr_& System::SmartPtr< T >::operator= ( const SmartPtr_ x)
inline

Copy-assigns SmartPtr object.

Parameters
xPointer to copy-assign.
Returns
Reference to this object.

◆ operator=() [3/5]

template<class T>
template<typename Q >
SmartPtr_& System::SmartPtr< T >::operator= ( const SmartPtr< Q > &  x)
inline

Copy-assigns SmartPtr object. Does required type conversions.

Template Parameters
QType of object pointed by x.
Parameters
xPointer to copy-assign.
Returns
Reference to this object.

◆ operator=() [4/5]

template<class T>
SmartPtr_& System::SmartPtr< T >::operator= ( Pointee_ p)
inline

Assigns raw pointer to SmartPtr object.

Parameters
pPointer value to assign.
Returns
Reference to this object.

◆ operator=() [5/5]

template<class T>
SmartPtr_& System::SmartPtr< T >::operator= ( std::nullptr_t  )
inline

Sets pointer value to nullptr.

Returns
Reference to this object.

◆ operator==()

template<class T>
bool System::SmartPtr< T >::operator== ( std::nullptr_t  ) const
inline

Checks if pointer points to nullptr.

Returns
True if pointer points to nullptr and false otherwise.

◆ ReleaseAndGetObjectToDelete()

template<class T>
Object* System::SmartPtr< T >::ReleaseAndGetObjectToDelete ( )
inlineprotected

Decrements currently referenced object's shared or weak pointer counter, depending on current pointer mode.

◆ ReleaseSharedAndGetObjectToDelete() [1/2]

template<typename T >
Object * System::SmartPtr< T >::ReleaseSharedAndGetObjectToDelete ( SharedRefReleaser releaser)
staticprotected

Removes shared pointer of a specific object, possibly deleting it.

Implementation.

Parameters
releaserObject to release.

◆ ReleaseSharedAndGetObjectToDelete() [2/2]

template<class T>
Object* System::SmartPtr< T >::ReleaseSharedAndGetObjectToDelete ( )
inlineprotected

Decrements currently referenced object's shared pointer counter.

◆ ReleaseWeak() [1/2]

template<class T>
static void System::SmartPtr< T >::ReleaseWeak ( System::Detail::SmartPtrCounter *  counter)
inlinestaticprotected

Decrements weak pointer counter.

Parameters
counterCounter to decrement.

◆ ReleaseWeak() [2/2]

template<class T>
void System::SmartPtr< T >::ReleaseWeak ( )
inlineprotected

Decrements currently referenced object's weak pointer counter.

◆ RemoveAliasing()

template<class T>
SmartPtr_ System::SmartPtr< T >::RemoveAliasing ( ) const
inline

Removes aliasing (created by an aliasing constructor) from pointer, makes sure it manages (if shared) or tracks (if weak) the same object it points to.

Returns
The pointer to the same object this pointer points to which looks after the lifetime of the same object.

◆ reset() [1/2]

template<class T>
void System::SmartPtr< T >::reset ( Pointee_ ptr)
inline

Sets pointed object.

Parameters
ptrRaw pointer to new referenced object.

◆ reset() [2/2]

template<class T>
void System::SmartPtr< T >::reset ( )
inline

Makes pointer pointing to nullptr.

◆ set_Mode()

template<class T>
void System::SmartPtr< T >::set_Mode ( SmartPtrMode  mode)
inline

Sets pointer mode. May alter referenced object's reference counts.

Parameters
modeNew mode of pointer.
#include "system/smart_ptr.h"
#include <iostream>
class Item final : public System::Object
{
public:
~Item() final
{
std::cout << "~Item()" << std::endl;
}
};
using ItemPtr = System::SmartPtr<Item>;
void PrintSharedCount(ItemPtr &ptr)
{
std::cout << "Number of shared pointers: " << ptr.get_shared_count() << std::endl;
}
void ChangeModeToWeak(ItemPtr &ptr)
{
std::cout << "The mode will be changed to System::SmartPtrMode::Weak" << std::endl;
std::cout << "The mode has been changed to System::SmartPtrMode::Weak" << std::endl;
}
int main()
{
ItemPtr ptr1 = System::MakeObject<Item>();
ItemPtr ptr2{ptr1, System::SmartPtrMode::Weak};
PrintSharedCount(ptr1);
PrintSharedCount(ptr1);
ChangeModeToWeak(ptr1);
ChangeModeToWeak(ptr2);
std::cout <<
"The pointer to an instance of the Item class expired: " <<
(static_cast<System::WeakPtr<ItemPtr::Pointee_>>(ptr1).expired() ? "True" : "False") <<
std::endl;
return 0;
}
/*
This code example produces the following output:
Number of shared pointers: 1
Number of shared pointers: 2
The mode will be changed to System::SmartPtrMode::Weak
The mode has been changed to System::SmartPtrMode::Weak
The mode will be changed to System::SmartPtrMode::Weak
~Item()
The mode has been changed to System::SmartPtrMode::Weak
The pointer to an instance of the Item class expired: True
*/

◆ SetContainedTemplateWeakPtr()

template<typename T >
void System::SmartPtr< T >::SetContainedTemplateWeakPtr ( uint32_t  argument) const

Calls SetTemplateWeakPtr() method on pointed object (if any).

Implementation.

Parameters
argumentArgument of SetTemplateWeakPtr method called on referenced object.

◆ static_pointer_cast()

template<class T>
template<class Y >
SmartPtr<Y> System::SmartPtr< T >::static_pointer_cast ( ) const
inline

Casts pointer to different type using static_cast on pointed object.

Template Parameters
YTarget type of pointed object.
Returns
Pointer of changed type which is always in shared mode.

◆ ToObjectPtr()

template<typename T >
SmartPtr< Object > System::SmartPtr< T >::ToObjectPtr ( ) const

Converts any pointer type to pointer to Object. Doesn't require Pointee_ type to be complete.

Implementation.

Returns
Raw pointer to referenced object (if any) or nullptr.

◆ Type()

template<class T>
static const System::TypeInfo& System::SmartPtr< T >::Type ( )
inlinestatic

Shortcut to get System::TypeInfo object for the Pointee_ type.

Returns
Const reference to TypeInfo structure which describes Pointee_ type.

Member Data Documentation

◆ m_data

template<class T>
class System::SmartPtr::Data System::SmartPtr< T >::m_data
protected

An instance of Data class.

◆ operator[]

template<class T>
template<typename IdxType >
decltype(System::Details::GetByIndex(std::declval<const SmartPtr_*>(), std::declval<IdxType>()) System::SmartPtr< T >::operator[]) (IdxType idx) const
inline

Accessor for array elements. Only compiles if SmartPtr_ is specialization of System::Array.

Template Parameters
IdxTypeType of index (assumed integral).
Parameters
idxIndex in array.
Returns
Array value at idx position.