LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
LLU::ManagedExpressionStore< T > Class Template Reference

Description

template<typename T>
class LLU::ManagedExpressionStore< T >

ManagedExpressionStore will keep track of instances of managed class T and will provide safe access to them.

Template Parameters
T- managed class

#include <ManagedExpression.hpp>

Public Types

using iterator = typename std::unordered_map< mint, std::shared_ptr< T > >::iterator
 Iterator over ManagedExpressionStore - it iterates over the underlying hash map. More...
 
using const_iterator = typename std::unordered_map< mint, std::shared_ptr< T > >::const_iterator
 Constant iterator over ManagedExpressionStore - it "const-iterates" over the underlying hash map. More...
 
using size_type = typename std::unordered_map< mint, std::shared_ptr< T > >::size_type
 Size type of the Store is the same as size_type of the underlying hash map. More...
 

Public Member Functions

void manageInstance (mbool mode, mint id)
 Function that will actually be called by LibraryLink when an instance of Managed Expression is created or deleted. More...
 
template<class DynamicType = T, typename... Args>
T & createInstance (mint id, Args &&... args)
 Create new object of class T that will be managed from Wolfram Language and place it in the map of managed objects. More...
 
T & createInstance (mint id, std::shared_ptr< T > ptr)
 Create instance in the store from a pointer to the managed class object. More...
 
T & createInstance (mint id, std::unique_ptr< T > ptr)
 Create instance in the store from a unique pointer to the managed class object. More...
 
int releaseInstance (mint id)
 Release an instance managed by this Store. More...
 
bool hasInstance (mint id) const
 Check if instance with given id is present in the store. More...
 
T & getInstance (mint id)
 Get managed instance with given id. More...
 
std::shared_ptr< T > getInstancePointer (mint id)
 Get a shared pointer to a managed instance with given id. More...
 
const std::string & getExpressionName () const noexcept
 Get symbol name that is used in the WL to represent Managed Expressions stored in this Store. More...
 
size_type size () const noexcept
 Get the number of currently managed expressions. More...
 
iterator begin () noexcept
 Get the iterator to the first element of the Store. More...
 
const_iterator begin () const noexcept
 Get the const iterator to the first element of the Store. More...
 
const_iterator cbegin () const noexcept
 Get the const iterator to the first element of the Store. More...
 
iterator end () noexcept
 Get the iterator past the last element of the Store. More...
 
const_iterator end () const noexcept
 Get the const iterator past the last element of the Store. More...
 
const_iterator cend () const noexcept
 Get the const iterator past the last element of the Store. More...
 
void registerType (std::string name, WolframLibraryData libData=LibraryData::API()) noexcept
 Register class T as managed expression under given name. More...
 
void unregisterType (WolframLibraryData libData=LibraryData::API()) const noexcept
 Unregister class T as managed expression. More...
 

Type aliases documentation

◆ const_iterator

template<typename T >
using LLU::ManagedExpressionStore< T >::const_iterator = typename std::unordered_map<mint, std::shared_ptr<T> >::const_iterator

Constant iterator over ManagedExpressionStore - it "const-iterates" over the underlying hash map.

◆ iterator

template<typename T >
using LLU::ManagedExpressionStore< T >::iterator = typename std::unordered_map<mint, std::shared_ptr<T> >::iterator

Iterator over ManagedExpressionStore - it iterates over the underlying hash map.

◆ size_type

template<typename T >
using LLU::ManagedExpressionStore< T >::size_type = typename std::unordered_map<mint, std::shared_ptr<T> >::size_type

Size type of the Store is the same as size_type of the underlying hash map.

Member Function Documentation

◆ begin() [1/2]

template<typename T >
const_iterator LLU::ManagedExpressionStore< T >::begin ( ) const
inlinenoexcept

Get the const iterator to the first element of the Store.

◆ begin() [2/2]

template<typename T >
iterator LLU::ManagedExpressionStore< T >::begin ( )
inlinenoexcept

Get the iterator to the first element of the Store.

◆ cbegin()

template<typename T >
const_iterator LLU::ManagedExpressionStore< T >::cbegin ( ) const
inlinenoexcept

Get the const iterator to the first element of the Store.

◆ cend()

template<typename T >
const_iterator LLU::ManagedExpressionStore< T >::cend ( ) const
inlinenoexcept

Get the const iterator past the last element of the Store.

◆ createInstance() [1/3]

template<typename T >
template<class DynamicType = T, typename... Args>
T& LLU::ManagedExpressionStore< T >::createInstance ( mint  id,
Args &&...  args 
)
inline

Create new object of class T that will be managed from Wolfram Language and place it in the map of managed objects.

Template Parameters
DynamicType- actual type of the constructed object, it allows Store to keep objects of subclasses of T
Args- constructor arguments types
Parameters
id- id of the newly created managed object
args- constructor arguments
Returns
reference to the newly created object

◆ createInstance() [2/3]

template<typename T >
T& LLU::ManagedExpressionStore< T >::createInstance ( mint  id,
std::shared_ptr< T >  ptr 
)
inline

Create instance in the store from a pointer to the managed class object.

This is useful when you have an existing object to be managed or when objects of class T cannot be constructed directly (e.g. because T is an abstract class).

Parameters
id- id of the newly created managed object
ptr- pointer to an instance of T or a subclass
Returns
reference to the object just added to the store

◆ createInstance() [3/3]

template<typename T >
T& LLU::ManagedExpressionStore< T >::createInstance ( mint  id,
std::unique_ptr< T >  ptr 
)
inline

Create instance in the store from a unique pointer to the managed class object.

The store will claim shared ownership of the managed object.

Parameters
id- id of the newly created managed object
ptr- pointer to an instance of T or a subclass
Returns
reference to the object just added to the store

◆ end() [1/2]

template<typename T >
const_iterator LLU::ManagedExpressionStore< T >::end ( ) const
inlinenoexcept

Get the const iterator past the last element of the Store.

◆ end() [2/2]

template<typename T >
iterator LLU::ManagedExpressionStore< T >::end ( )
inlinenoexcept

Get the iterator past the last element of the Store.

◆ getExpressionName()

template<typename T >
const std::string& LLU::ManagedExpressionStore< T >::getExpressionName ( ) const
inlinenoexcept

Get symbol name that is used in the WL to represent Managed Expressions stored in this Store.

Returns
symbol name

◆ getInstance()

template<typename T >
T& LLU::ManagedExpressionStore< T >::getInstance ( mint  id)
inline

Get managed instance with given id.

Throw if the id is invalid or if there is no corresponding instance.

Parameters
id- id of instance of interest
Returns
reference to the managed object

◆ getInstancePointer()

template<typename T >
std::shared_ptr<T> LLU::ManagedExpressionStore< T >::getInstancePointer ( mint  id)
inline

Get a shared pointer to a managed instance with given id.

Throw if the id is invalid.

Parameters
id- id of instance of interest
Returns
shared pointer to the managed object

◆ hasInstance()

template<typename T >
bool LLU::ManagedExpressionStore< T >::hasInstance ( mint  id) const
inline

Check if instance with given id is present in the store.

Parameters
id- id to be checked
Returns
true iff the instance with given id is in the store

◆ manageInstance()

template<typename T >
void LLU::ManagedExpressionStore< T >::manageInstance ( mbool  mode,
mint  id 
)
inline

Function that will actually be called by LibraryLink when an instance of Managed Expression is created or deleted.

Notice that this function does not actually create a new object of class T. This is because there is no way to pass constructor arguments here.

Parameters
mode- are we deleting existing instance (True) or creating new one (False)
id- id of the instance of interest

◆ registerType()

template<typename T >
void LLU::ManagedExpressionStore< T >::registerType ( std::string  name,
WolframLibraryData  libData = LibraryData::API() 
)
inlinenoexcept

Register class T as managed expression under given name.

Parameters
name- name of the Wolfram Language symbol that will be used to manage class T
libData- optionally specify WolframLibraryData instance
Note
This function should typically be called in WolframLibrary_initialize

◆ releaseInstance()

template<typename T >
int LLU::ManagedExpressionStore< T >::releaseInstance ( mint  id)
inline

Release an instance managed by this Store.

Parameters
id- id of the instance to be released
Returns
0 if the id was correct and the operation succeeded, non-negative integer otherwise
Note
Normally, every instance in the Store has a corresponding WL expression and the instance is released as soon as the corresponding expression goes out of scope (its reference count hits 0). This function can be used to force immediate release of a managed instance.
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/releaseManagedLibraryExpression.html

◆ size()

template<typename T >
size_type LLU::ManagedExpressionStore< T >::size ( ) const
inlinenoexcept

Get the number of currently managed expressions.

Returns
size of the store

◆ unregisterType()

template<typename T >
void LLU::ManagedExpressionStore< T >::unregisterType ( WolframLibraryData  libData = LibraryData::API()) const
inlinenoexcept

Unregister class T as managed expression.

Parameters
libData- optionally specify WolframLibraryData instance
Note
This function should typically be called in WolframLibrary_uninitialize