 |
LibraryLink Utilities
3.0.1
Modern C++ wrapper over LibraryLink and WSTP
|
Go to the documentation of this file.
7 #ifndef LLU_MANAGEDEXPRESSION_HPP
8 #define LLU_MANAGEDEXPRESSION_HPP
11 #include <unordered_map>
21 #define DEFINE_MANAGED_STORE_AND_SPECIALIZATION(ClassName) \
22 LLU::ManagedExpressionStore<ClassName> ClassName##Store; \
25 inline void LLU::manageInstanceCallback<ClassName>(WolframLibraryData, mbool mode, mint id) { \
26 ClassName##Store.manageInstance(mode, id); \
42 static_assert(dependent_false_v<T>,
"Use of unspecialized ManageInstance function.");
85 template<
class DynamicType = T,
typename... Args>
88 store[id] = std::make_shared<DynamicType>(std::forward<Args>(args)...);
101 store[id] = std::move(ptr);
102 return instanceAt(
id);
124 return LibraryData::API()->releaseManagedLibraryExpression(expressionName.c_str(),
id);
133 return store.count(
id) == 1;
143 return instanceAt(
id);
161 return expressionName;
176 return store.begin();
183 return store.cbegin();
190 return store.cbegin();
221 expressionName = std::move(name);
222 libData->registerLibraryExpressionManager(expressionName.c_str(), manageInstanceCallback<T>);
231 libData->unregisterLibraryExpressionManager(expressionName.c_str());
239 void checkID(mint
id)
const {
250 T& instanceAt(mint
id) {
251 auto& instancePtr = store[id];
260 std::unordered_map<mint, std::shared_ptr<T>> store;
263 std::string expressionName;
268 #endif // LLU_MANAGEDEXPRESSION_HPP
iterator end() noexcept
Get the iterator past the last element of the Store.
Definition: ManagedExpression.hpp:196
const std::string MLENullInstance
Missing managed object for a valid ID.
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
void manageInstanceCallback(WolframLibraryData, mbool, mint)
A template for library callback used by LibraryLink to manage instances of ManagedLibraryExpressions.
Definition: ManagedExpression.hpp:41
int releaseInstance(mint id)
Release an instance managed by this Store.
Definition: ManagedExpression.hpp:123
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 ma...
Definition: ManagedExpression.hpp:86
T & getInstance(mint id)
Get managed instance with given id.
Definition: ManagedExpression.hpp:141
static WolframLibraryData API()
Get currently owned WolframLibraryData, if any.
Definition: LibraryData.cpp:22
void manageInstance(mbool mode, mint id)
Function that will actually be called by LibraryLink when an instance of Managed Expression is create...
Definition: ManagedExpression.hpp:69
const std::string & getExpressionName() const noexcept
Get symbol name that is used in the WL to represent Managed Expressions stored in this Store.
Definition: ManagedExpression.hpp:160
std::shared_ptr< T > getInstancePointer(mint id)
Get a shared pointer to a managed instance with given id.
Definition: ManagedExpression.hpp:151
size_type size() const noexcept
Get the number of currently managed expressions.
Definition: ManagedExpression.hpp:168
const_iterator cend() const noexcept
Get the const iterator past the last element of the Store.
Definition: ManagedExpression.hpp:210
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
const_iterator begin() const noexcept
Get the const iterator to the first element of the Store.
Definition: ManagedExpression.hpp:182
iterator begin() noexcept
Get the iterator to the first element of the Store.
Definition: ManagedExpression.hpp:175
T & createInstance(mint id, std::unique_ptr< T > ptr)
Create instance in the store from a unique pointer to the managed class object.
Definition: ManagedExpression.hpp:111
const std::string ManagedExprInvalidID
Given number is not an ID of any existing managed expression.
T & createInstance(mint id, std::shared_ptr< T > ptr)
Create instance in the store from a pointer to the managed class object.
Definition: ManagedExpression.hpp:99
void unregisterType(WolframLibraryData libData=LibraryData::API()) const noexcept
Unregister class T as managed expression.
Definition: ManagedExpression.hpp:230
void registerType(std::string name, WolframLibraryData libData=LibraryData::API()) noexcept
Register class T as managed expression under given name.
Definition: ManagedExpression.hpp:220
typename std::unordered_map< mint, std::shared_ptr< T > >::iterator iterator
Iterator over ManagedExpressionStore - it iterates over the underlying hash map.
Definition: ManagedExpression.hpp:53
const_iterator cbegin() const noexcept
Get the const iterator to the first element of the Store.
Definition: ManagedExpression.hpp:189
const_iterator end() const noexcept
Get the const iterator past the last element of the Store.
Definition: ManagedExpression.hpp:203
bool hasInstance(mint id) const
Check if instance with given id is present in the store.
Definition: ManagedExpression.hpp:132
ManagedExpressionStore will keep track of instances of managed class T and will provide safe access t...
Definition: ManagedExpression.hpp:50
Short but generally useful functions.
typename std::unordered_map< mint, std::shared_ptr< T > >::size_type size_type
Size type of the Store is the same as size_type of the underlying hash map.
Definition: ManagedExpression.hpp:59
typename std::unordered_map< mint, std::shared_ptr< T > >::const_iterator const_iterator
Constant iterator over ManagedExpressionStore - it "const-iterates" over the underlying hash map.
Definition: ManagedExpression.hpp:56