Asposecpplib
CsToCppPorter::MemoryManagement Class Reference

Defines a methods that changes the lifetime of objects. More...

Public Types

using ObjectsBag = System::Details::ObjectsBag
 Highly optimized container designed to extend objects lifetime. More...
 

Static Public Member Functions

template<typename T1 , typename T2 >
static System::SmartPtr< typename T1::Pointee_ > BindLifetime (const T1 &target, const T2 &owner)
 Creates a smart pointer using the aliasing constructor. Creates a pointer to the target object which controls lifetime of the owner object. This pointer's expiration is bound to expiration of the owner object, just like this of any other pointer that owns owner object. If converted to weak type, it is still consistent with other pointers to owner and does not expire while there is at least one shared pointer to owner object. Also, the returned pointer does not ensure that target object is alive, so some other means (e. g. shared pointer stored in the owner object) should do so. Should be used in the situations where owner owns the target, which effectively means that the target will be kept alive by owner even if the returned aliasing pointer no longer exists. Otherwise, doesn't guarantee that the resulting pointer will remain valid, because if owner doesn't actually hold shared reference to target, target can be deleted any time as the resulting pointer only owns owner but not the target. More...
 
template<typename T1 , typename T2 >
static std::enable_if<!std::is_reference< T1 >::value, System::SmartPtr< typename T1::Pointee_ > >::type BindLifetime (T1 &&target, const T2 &owner)
 Creates a smart pointer using the aliasing constructor. Creates a pointer to the target object which controls lifetime of the owner object. This pointer's expiration is bound to expiration of the owner object, just like this of any other pointer that owns owner object. If converted to weak type, it is still consistent with other pointers to owner and does not expire while there is at least one shared pointer to owner object. Also, the returned pointer does not ensure that target object is alive, so some other means (e. g. shared pointer stored in the owner object) should do so. Should be used in the situations where owner owns the target, which effectively means that the target will be kept alive by owner even if the returned aliasing pointer no longer exists. Otherwise, doesn't guarantee that the returned pointer will remain valid, because if owner doesn't actually hold shared reference to target, target can be deleted any time as the returned pointer only owns owner but not the target. More...
 
template<typename T , typename ... Objects>
static System::SmartPtr< typename T::Pointee_ > ExtendLifetime (const T &target, const Objects &...objects)
 Creates a smart pointer using the aliasing constructor and copies target and objects pointers to the "proxy" objects holder. Creates a pointer to the target and extends the lifetime of all objects to the lifetime of this pointer. The resulting pointer guarantees all parameters to remain alive, even if it is the only pointer that keeps track of them. The resulting pointer effectively owns all objects passed to this method, however, it has its own expiration track. This means, that converting its own copy to weak type will expire it, even if all objects it tracks are still alive. Should be used in the situations when there are several unrelated (not holding shared references to each other) objects that should be guaranteed to remain alive together. The returned pointer will own a temporary objects that keeps all of them alive, and this lifetime extension stops working once the returned pointer no longer exists (or all of its copies become weak). More...
 

Detailed Description

Defines a methods that changes the lifetime of objects.

Member Typedef Documentation

◆ ObjectsBag

using CsToCppPorter::MemoryManagement::ObjectsBag = System::Details::ObjectsBag

Highly optimized container designed to extend objects lifetime.

Member Function Documentation

◆ BindLifetime() [1/2]

template<typename T1 , typename T2 >
static System::SmartPtr<typename T1::Pointee_> CsToCppPorter::MemoryManagement::BindLifetime ( const T1 &  target,
const T2 &  owner 
)
inlinestatic

Creates a smart pointer using the aliasing constructor. Creates a pointer to the target object which controls lifetime of the owner object. This pointer's expiration is bound to expiration of the owner object, just like this of any other pointer that owns owner object. If converted to weak type, it is still consistent with other pointers to owner and does not expire while there is at least one shared pointer to owner object. Also, the returned pointer does not ensure that target object is alive, so some other means (e. g. shared pointer stored in the owner object) should do so. Should be used in the situations where owner owns the target, which effectively means that the target will be kept alive by owner even if the returned aliasing pointer no longer exists. Otherwise, doesn't guarantee that the resulting pointer will remain valid, because if owner doesn't actually hold shared reference to target, target can be deleted any time as the resulting pointer only owns owner but not the target.

Template Parameters
T1Type of smart pointer to the object that the new smart pointer will refer to.
T2Type of smart pointer to an object whose ownership is shared with a new smart pointer.
Parameters
targetSmart pointer to the object that the new smart pointer will refer to.
ownerSmart pointer to an object whose ownership is shared with a new smart pointer.
Returns
New smart pointer.
#include "system/memory_management.h"
#include "system/object.h"
#include <iostream>
// The ChildNode-class forward declaration.
class ChildNode;
// Stores a SharedPtr to an instance of the ChildNode class.
class ParentNode: public System::Object
{
public:
};
// Stores a WeakPtr to an instance of the ParentNode-class.
class ChildNode: public System::Object
{
public:
};
// The lifetime of the ChildNode- and ParentNode-class instances will be bound when true is passed as a param value.
System::SharedPtr<ChildNode> GetChildNode(bool bindLifetime)
{
auto parent = System::MakeObject<ParentNode>();
auto child = System::MakeObject<ChildNode>();
parent->child = child;
child->parent = parent;
return bindLifetime ? CsToCppPorter::MemoryManagement::BindLifetime(child, parent) : child;
}
// This function prints information about WeakPtr to a ParentNode-class instance.
void PrintInfo(System::WeakPtr<ParentNode> &parent)
{
std::cout << "The pointer to an instance of the ParentNode class has expired: " << (parent.expired() ? "True" : "False") << std::endl;
}
int main()
{
auto child = GetChildNode(false);
PrintInfo(child->parent);
child = GetChildNode(true);
PrintInfo(child->parent);
return 0;
}
/*
This code example produces the following output:
The pointer to an instance of the ParentNode class has expired: True
The pointer to an instance of the ParentNode class has expired: False
*/
static System::SmartPtr< typename T1::Pointee_ > BindLifetime(const T1 &target, const T2 &owner)
Creates a smart pointer using the aliasing constructor. Creates a pointer to the target object which ...
Definition: memory_management.h:129
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:65
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:173
Subclass of System::SmartPtr which sets itself to weak mode at construction. Please note that this cl...
Definition: weak_ptr.h:18
bool expired() const
Checks if referenced object was already deleted.
Definition: weak_ptr.h:86

◆ BindLifetime() [2/2]

template<typename T1 , typename T2 >
static std::enable_if<!std::is_reference<T1>::value, System::SmartPtr<typename T1::Pointee_> >::type CsToCppPorter::MemoryManagement::BindLifetime ( T1 &&  target,
const T2 &  owner 
)
inlinestatic

Creates a smart pointer using the aliasing constructor. Creates a pointer to the target object which controls lifetime of the owner object. This pointer's expiration is bound to expiration of the owner object, just like this of any other pointer that owns owner object. If converted to weak type, it is still consistent with other pointers to owner and does not expire while there is at least one shared pointer to owner object. Also, the returned pointer does not ensure that target object is alive, so some other means (e. g. shared pointer stored in the owner object) should do so. Should be used in the situations where owner owns the target, which effectively means that the target will be kept alive by owner even if the returned aliasing pointer no longer exists. Otherwise, doesn't guarantee that the returned pointer will remain valid, because if owner doesn't actually hold shared reference to target, target can be deleted any time as the returned pointer only owns owner but not the target.

Template Parameters
T1Type of smart pointer to the object that the new smart pointer will refer to.
T2Type of smart pointer to an object whose ownership is shared with a new smart pointer.
Parameters
targetSmart pointer to the object that the new smart pointer will refer to.
ownerSmart pointer to an object whose ownership is shared with a new smart pointer.
Returns
New smart pointer.

◆ ExtendLifetime()

template<typename T , typename ... Objects>
static System::SmartPtr<typename T::Pointee_> CsToCppPorter::MemoryManagement::ExtendLifetime ( const T &  target,
const Objects &...  objects 
)
inlinestatic

Creates a smart pointer using the aliasing constructor and copies target and objects pointers to the "proxy" objects holder. Creates a pointer to the target and extends the lifetime of all objects to the lifetime of this pointer. The resulting pointer guarantees all parameters to remain alive, even if it is the only pointer that keeps track of them. The resulting pointer effectively owns all objects passed to this method, however, it has its own expiration track. This means, that converting its own copy to weak type will expire it, even if all objects it tracks are still alive. Should be used in the situations when there are several unrelated (not holding shared references to each other) objects that should be guaranteed to remain alive together. The returned pointer will own a temporary objects that keeps all of them alive, and this lifetime extension stops working once the returned pointer no longer exists (or all of its copies become weak).

Template Parameters
TType of the object that the new smart pointer will refer to.
ObjectsTypes of the objects whose ownership is shared with a new smart pointer.
Parameters
targetSmart pointer to the object that the new smart pointer will refer to.
objectsSmart pointers to the objects whose ownership is shared with a new smart pointer.
Returns
New smart pointer.
#include "system/memory_management.h"
#include "system/object.h"
#include <iostream>
// Create a class that stores links to the previous node and to the next one.
class Node: public System::Object
{
using NodePtr = System::WeakPtr<Node>;
int32_t m_value;
public:
NodePtr previous;
NodePtr next;
explicit Node(int32_t value): m_value(value) {}
int32_t GetValue() const
{
return m_value;
}
};
// This function prints a value that is stored by the specified node.
void PrintValue(System::SmartPtr<Node> &node)
{
std::cout << (node == nullptr ? "nullptr" : std::to_string(node->GetValue())) << ' ';
}
int main()
{
{
auto first = System::MakeObject<Node>(1);
node = System::MakeObject<Node>(2);
auto last = System::MakeObject<Node>(3);
first->next = node;
node->previous = first;
node->next = last;
last->previous = node;
}
PrintValue(node->previous);
PrintValue(node);
PrintValue(node->next);
std::cout << std::endl;
{
auto first = System::MakeObject<Node>(4);
auto middle = System::MakeObject<Node>(5);
auto last = System::MakeObject<Node>(6);
first->next = middle;
middle->previous = first;
middle->next = last;
last->previous = middle;
}
PrintValue(node->previous);
PrintValue(node);
PrintValue(node->next);
std::cout << std::endl;
return 0;
}
/*
This code example produces the following output:
nullptr 2 nullptr
4 5 6
*/
static System::SmartPtr< typename T::Pointee_ > ExtendLifetime(const T &target, const Objects &...objects)
Creates a smart pointer using the aliasing constructor and copies target and objects pointers to the ...
Definition: memory_management.h:258