xentara-plugin v1.2.1
The Xentara Plugin Framework
|
A handle for writing a data value. More...
#include <data/WriteHandle.hpp>
Public Types | |
enum class | Error { NoError , NullHandle , Unknown , ReadOnly , TypeMismatch , OutOfRange , IllegalValue , Defunct , UnknownError } |
An enum describing read errors. More... | |
Public Member Functions | |
WriteHandle () | |
Default constructor. | |
WriteHandle (Error error) | |
Constructor for an error handle that always returns a write error. | |
WriteHandle (const std::error_code &error) | |
Constructor for an error handle that returns an arbitrary std::error_code. | |
template<memory::ObjectBlockType MemoryBlock> | |
WriteHandle (const std::shared_ptr< MemoryBlock > &memoryBlock) | |
Constructor for a handle that writes to a memory block. | |
template<memory::ObjectBlockType MemoryBlock> | |
WriteHandle (const std::weak_ptr< MemoryBlock > &memoryBlock) | |
Constructor for a handle that writes to a memory block. | |
template<typename Type , std::invocable< Type && > Callback> | |
WriteHandle (std::in_place_type_t< Type >, const Callback &callback) | |
Constructor for a handle that writes using a callback. | |
template<typename Type , std::invocable< Type && > Callback> | |
WriteHandle (std::in_place_type_t< Type >, Callback &&callback) | |
Constructor for a handle that writes using a callback. | |
template<typename Type , typename Object , std::invocable< Object &, Type && > Callback> | |
WriteHandle (std::in_place_type_t< Type >, const Callback &callback, std::shared_ptr< Object > object) | |
Constructor for a handle that writes by calling a callback with an object. | |
template<typename Type , typename Object , std::invocable< Object &, Type && > Callback> | |
WriteHandle (std::in_place_type_t< Type >, Callback &&callback, std::shared_ptr< Object > object) | |
Constructor for a handle that writes by calling a callback with an object. | |
template<typename Type , typename Object , std::invocable< Object &, Type && > Callback> | |
WriteHandle (std::in_place_type_t< Type >, const Callback &callback, std::weak_ptr< Object > object) | |
Constructor for a handle that writes by calling a callback with an object. | |
template<typename Type , typename Object , std::invocable< Object &, Type && > Callback> | |
WriteHandle (std::in_place_type_t< Type >, Callback &&callback, std::weak_ptr< Object > object) | |
Constructor for a handle that writes by calling a callback with an object. | |
auto | nativeType () const -> NativeType |
Gets the native type of the handle. | |
template<typename Type > | |
auto | write (const Type &value) const -> std::error_code |
Writes a value. | |
auto | hardError () const -> std::error_code |
Gets the error of an error handle. | |
auto | operator== (Error error) const -> bool |
Checks whether the handle is an error handle that returns a certain error. | |
auto | operator== (const std::error_code &error) const -> bool |
Checks whether the handle is an error handle that returns a certain error. | |
Static Public Member Functions | |
static auto | errorCategory () -> const std::error_category & |
Gets the error category for error codes of type [Error]. | |
A handle for writing a data value.
|
strong |
An enum describing read errors.
xentara::data::WriteHandle::WriteHandle | ( | ) |
Default constructor.
This constructor creates a null handle that is not associated with any value. Reading the handle will always return make_error_code(Error::NullHandle).
xentara::data::WriteHandle::WriteHandle | ( | Error | error | ) |
Constructor for an error handle that always returns a write error.
This constructor creates a handle that always returns a certain error when attempting to write. Writing the handle will always return make_error_code(error).
error | The error the handle should return when attempting to write to it. |
xentara::data::WriteHandle::WriteHandle | ( | const std::error_code & | error | ) |
Constructor for an error handle that returns an arbitrary std::error_code.
This constructor creates a handle that always returns a certain error when attempting to write. Reading the handle will always return the given error.
error | The error code the handle should return when attempting to write to it. |
xentara::data::WriteHandle::WriteHandle | ( | const std::shared_ptr< MemoryBlock > & | memoryBlock | ) |
Constructor for a handle that writes to a memory block.
This function only stores a weak pointer to the memory block. If the weak pointer has expired (lock() returns nullptr) when a value is written, then write() will return Error::Defunct.
memoryBlock | A pointer to the memory block that sould be written. You can use the aliasing constructor (8) of std::shared_ptr to create a shared pointer to the memory block from a shared pointer to the enclosing object. |
xentara::data::WriteHandle::WriteHandle | ( | const std::weak_ptr< MemoryBlock > & | memoryBlock | ) |
Constructor for a handle that writes to a memory block.
If the weak pointer has expired (lock() returns nullptr) when a value is written, then write() will return Error::Defunct.
memoryBlock | A pointer to the memory block that sould be written. |
xentara::data::WriteHandle::WriteHandle | ( | std::in_place_type_t< Type > | , |
const Callback & | callback | ||
) |
Constructor for a handle that writes using a callback.
This constructor construct a handler that uses a callback to write the value. The callback will be called as if by:
Where value is the value, converted to Type.
The return value of the callback will be handled as follows:
Other return types are not allowed.
Examples:
The callback may throw exceptions. If the callback throws an exception derived from std::system_error, write() will return the contained error code. If the callback throws another type of exception, write() will return Error::Unknown.
callback | The callback that should be called to handle the written value |
xentara::data::WriteHandle::WriteHandle | ( | std::in_place_type_t< Type > | , |
Callback && | callback | ||
) |
Constructor for a handle that writes using a callback.
This constructor construct a handler that uses a callback to write the value. The callback will be called as if by:
Where value is the value, converted to Type.
The return value of the callback will be handled as follows:
Other return types are not allowed.
Examples:
The callback may throw exceptions. If the callback throws an exception derived from std::system_error, write() will return the contained error code. If the callback throws another type of exception, write() will return Error::Unknown.
callback | The callback that should be called to handle the written value |
xentara::data::WriteHandle::WriteHandle | ( | std::in_place_type_t< Type > | , |
const Callback & | callback, | ||
std::shared_ptr< Object > | object | ||
) |
Constructor for a handle that writes by calling a callback with an object.
This constructor construct a handler that uses a member function as a callback to write the value. The callback will be called as if by:
Where value is the value, converted to Type.
This function only stores a weak pointer to the memory block. If the weak pointer has expired (lock() returns nullptr) when a value is written, then write() will return Error::Defunct.
The return value of the callback will be handled as follows:
Other return types are not allowed.
Examples:
The callback may throw exceptions. If the callback throws an exception derived from std::system_error, write() will return the contained error code. If the callback throws another type of exception, write() will return Error::Unknown.
callback | The callback that should be called to handle the written value |
object | The object that should be passed to the callback |
xentara::data::WriteHandle::WriteHandle | ( | std::in_place_type_t< Type > | , |
Callback && | callback, | ||
std::shared_ptr< Object > | object | ||
) |
Constructor for a handle that writes by calling a callback with an object.
This constructor construct a handler that uses a member function as a callback to write the value. The callback will be called as if by:
Where value is the value, converted to Type.
This function only stores a weak pointer to the memory block. If the weak pointer has expired (lock() returns nullptr) when a value is written, then write() will return Error::Defunct.
The return value of the callback will be handled as follows:
Other return types are not allowed.
Examples:
The callback may throw exceptions. If the callback throws an exception derived from std::system_error, write() will return the contained error code. If the callback throws another type of exception, write() will return Error::Unknown.
callback | The callback that should be called to handle the written value |
object | The object that should be passed to the callback |
xentara::data::WriteHandle::WriteHandle | ( | std::in_place_type_t< Type > | , |
const Callback & | callback, | ||
std::weak_ptr< Object > | object | ||
) |
Constructor for a handle that writes by calling a callback with an object.
This constructor construct a handler that uses a member function as a callback to write the value. The callback will be called as if by:
Where value is the value as a const Type &. If the weak pointer has expired (lock() returns nullptr) when a value is written, then write() will return Error::Defunct.
The return value of the callback will be handled as follows:
Other return types are not allowed.
Examples:
The callback may throw exceptions. If the callback throws an exception derived from std::system_error, write() will return the contained error code. If the callback throws another type of exception, write() will return Error::Unknown.
callback | The callback that should be called to handle the written value |
object | The object that should be passed to the callback |
xentara::data::WriteHandle::WriteHandle | ( | std::in_place_type_t< Type > | , |
Callback && | callback, | ||
std::weak_ptr< Object > | object | ||
) |
Constructor for a handle that writes by calling a callback with an object.
This constructor construct a handler that uses a member function as a callback to write the value. The callback will be called as if by:
Where value is the value as a const Type &. If the weak pointer has expired (lock() returns nullptr) when a value is written, then write() will return Error::Defunct.
The return value of the callback will be handled as follows:
Other return types are not allowed.
Examples:
The callback may throw exceptions. If the callback throws an exception derived from std::system_error, write() will return the contained error code. If the callback throws another type of exception, write() will return Error::Unknown.
callback | The callback that should be called to handle the written value |
object | The object that should be passed to the callback |
|
static |
Gets the error category for error codes of type [Error].
auto xentara::data::WriteHandle::hardError | ( | ) | const -> std::error_code |
Gets the error of an error handle.
This function returns the error code of an error handle created using WriteHandle(Error error) or WriteHandle(const std::error_code &error). An error handle will always return the given error when calling write().
Soft errors, meaning errors that might go away at some point, can only be detected by attempting to write a value using write().
auto xentara::data::WriteHandle::nativeType | ( | ) | const -> NativeType |
Gets the native type of the handle.
This function can be used to determine what is the best type is to use as a template parameter for the write() function. You can use this function if you want to treat certain data types separately, and not rely on the built-in conversions. For example, you can use this function if you want to normally write the value as a string, but handle numbers and Booleans differently.
auto xentara::data::WriteHandle::operator== | ( | const std::error_code & | error | ) | const -> bool |
Checks whether the handle is an error handle that returns a certain error.
This operator checks whether the handle is an error handle created using WriteHandle(const std::error_code &error) or WriteHandle(Error error). An error handle will always return the given error when calling read(), and will never return a valid value.
Soft errors, meaning errors that might go away at some point, can only be gotten using read().
auto xentara::data::WriteHandle::operator== | ( | Error | error | ) | const -> bool |
Checks whether the handle is an error handle that returns a certain error.
This function returns the error code of an error handle created using WriteHandle(Error error) or WriteHandle(const std::error_code &error). An error handle will always return the given error when calling write().
Soft errors, meaning errors that might go away at some point, can only be detected by attempting to write a value using write().
auto xentara::data::WriteHandle::write | ( | const Type & | value | ) | const -> std::error_code |
Writes a value.
The value will be converted to the type used by the target, if possible. If the value cannot be converted, the function will return Error::TypeMismatch.
To write a value contained in a read handle with unknown type, you can read the value using ReadHandle::read<std::any>() and pass the returned std::any object to this function. This will automatically convert the value from the type stored in the read handle to the type used by the target of the write handle. As with regular values,the function will return Error::TypeMismatch if the value cannot be converted.