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...
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::SortedSetPtr< T >, 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 >.
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... | |
Object * | GetObjectOwner () 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... | |
Object * | GetObjectOrNull () const |
Gets pointed object (if any) or nullptr. Same as get(). More... | |
SmartPtr< Object > | ToObjectPtr () 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::TypeInfo & | Type () |
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... | |
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.
T | Type 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. { public: System::SharedPtr<ChildNode> childNode; }; // This class stores an int32_t value and contains WeakPtr to the ParentNode-class instance to exclude circular // dependencies. { int32_t m_value; public: System::WeakPtr<ParentNode> parentNode; 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. System::SharedPtr<ParentNode> getParenNode() { 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() { System::WeakPtr<ChildNode> child; { 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 */ |
typedef System::Details::ArrayTypeResolver<T>::type System::SmartPtr< T >::ArrayType |
Same as Pointee_, if it is a specialization of System::Array, and void otherwise.
typedef T System::SmartPtr< T >::Pointee_ |
Pointed type.
typedef SmartPtr<T> System::SmartPtr< T >::SmartPtr_ |
Specialized smart pointer type.
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.
|
inline |
Creates SmartPtr object of required mode.
mode | Pointer mode. |
|
inline |
Creates null-pointer SmartPtr object of required mode.
mode | Pointer mode. |
|
inline |
|
inline |
Copy constructs SmartPtr object. Both pointers point to the same object afterwards.
ptr | Pointer to copy. |
mode | Pointer mode. |
|
inline |
Copy constructs SmartPtr object. Both pointers point to the same object afterwards. Performs type conversion if allowed.
Q | Type of object pointed by x. |
x | Pointer to copy. |
mode | Pointer mode. |
|
inlinenoexcept |
Move constructs SmartPtr object. Effectively, swaps two pointers, if they are both of same mode. x may be unusable after call.
x | Pointer to move. |
mode | Pointer mode. |
|
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++.
Y | Type of source array. |
src | Pointer to array to create a copy of, but with different type of elements. |
mode | Pointer mode. |
|
inlineexplicit |
Initializes empty array. Used to translate some C# code constructs.
Y | Placeholder of EmptyArrayInitializer type. |
|
inline |
Constructs a SmartPtr which shares ownership information with the initial value of ptr, but holds an unrelated and unmanaged pointer p.
ptr | Another smart pointer to share the ownership to the ownership from. |
p | Pointer to an object to manage. |
mode | Pointer mode. #include "system/object.h" #include "system/smart_ptr.h" #include <iostream> // This class contains a field that will be printed. { public: std::string value = "Hello, world!"; }; // This class contains an instance of the Foo class. { public: Foo data; }; // Used to print a string from the Foo-class instance. { std::cout << foo->value << std::endl; } // Prints the number of shared pointers pointing to the object. { 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! */ |
|
inline |
Destroys SmartPtr object. If required, decreases pointed object's reference counter and deletes object.
|
inline |
|
inline |
|
inline |
Casts pointer to its type itself.
Y | Target type of pointed object. |
Check | Flags to throw exception if no cast available. |
|
inline |
Casts pointer to base type using static_cast.
Y | Target type of pointed object. |
Check | Flags to throw exception if no cast available. |
|
inline |
Casts pointer to derived type dynamic_cast.
Y | Target type of pointed object. |
Check | Flags to throw exception if no cast available. |
|
inline |
Casts pointer to derived type dynamic_cast.
Y | Target type of pointed object. |
Check | Flags to throw exception if no cast available. |
|
inline |
|
inline |
|
inline |
Casts pointer to different type using const_cast on pointed object.
Y | Target type of pointed object. |
|
inline |
Casts pointer to different type using dynamic_cast on pointed object.
Y | Target type of pointed object. |
|
inline |
|
inline |
|
inline |
Gets pointed object.
|
inline |
Gets pointer mode.
|
inline |
Gets pointed object, but asserts that pointer is in shared mode.
|
inline |
Gets number of shared pointers existing to referenced object, including current one. Asserts current pointer being in shared mode.
|
inline |
Calls GetHashCode() on pointed object.
|
inline |
Gets currently referenced object (if any) or throws.
System::NullReferenceException | Thrown if called on null-pointer. |
|
inline |
Gets pointed object (if any) or nullptr. Same as get().
|
inline |
Gets referenced object.
|
inline |
Gets pointed object (if any) or nullptr. Same as get().
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.
target | Specifies target type to check against. |
|
inline |
Checks if pointer is pointed to another object than owned (created by an aliasing constructor).
|
inline |
Checks if pointer is in shared mode.
|
inline |
Checks if pointer is in weak mode.
|
inlineexplicitnoexcept |
Checks if pointer is not null.
|
inlinenoexcept |
Checks if pointer is null.
|
inline |
Gets reference to pointed object. Asserts that pointer is not null.
|
inline |
Allows to access members of referenced object.
System::NullReferenceException | If pointer is null. |
|
inline |
|
inlinenoexcept |
Move-assigns SmartPtr object. x becomes unusable.
x | Pointer to move-assign. |
|
inline |
|
inline |
Copy-assigns SmartPtr object. Does required type conversions.
Q | Type of object pointed by x. |
x | Pointer to copy-assign. |
|
inline |
Assigns raw pointer to SmartPtr object.
p | Pointer value to assign. |
|
inline |
Sets pointer value to nullptr.
|
inline |
Checks if pointer points to nullptr.
|
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.
|
inline |
Sets pointed object.
ptr | Raw pointer to new referenced object. |
|
inline |
Makes pointer pointing to nullptr.
|
inline |
Sets pointer mode. May alter referenced object's reference counts.
mode | New mode of pointer. #include "system/smart_ptr.h" #include <iostream> { 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; ptr.set_Mode(System::SmartPtrMode::Weak); 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); ptr2.set_Mode(System::SmartPtrMode::Shared); PrintSharedCount(ptr1); ChangeModeToWeak(ptr1); ChangeModeToWeak(ptr2); std::cout << "The pointer to an instance of the Item class expired: " << 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 */ |
void System::SmartPtr< T >::SetContainedTemplateWeakPtr | ( | uint32_t | argument | ) | const |
Calls SetTemplateWeakPtr() method on pointed object (if any).
Implementation.
argument | Argument of SetTemplateWeakPtr method called on referenced object. |
|
inline |
Casts pointer to different type using static_cast on pointed object.
Y | Target type of pointed object. |
SmartPtr< Object > System::SmartPtr< T >::ToObjectPtr | ( | ) | const |
Converts any pointer type to pointer to Object. Doesn't require Pointee_ type to be complete.
Implementation.
|
inlinestatic |
Shortcut to get System::TypeInfo object for the Pointee_ type.
|
inline |
Accessor for array elements. Only compiles if SmartPtr_ is specialization of System::Array.
IdxType | Type of index (assumed integral). |
idx | Index in array. |