xentara-plugin v1.2.1
The Xentara Plugin Framework
Loading...
Searching...
No Matches
xentara::data::ReadHandle Class Referencefinal

A handle for reading a data value. More...

#include <data/ReadHandle.hpp>

Public Types

enum class  Error {
  NoError , NullHandle , Unknown , WriteOnly ,
  NoData , TypeMismatch
}
 An enum describing read errors. More...
 

Public Member Functions

 ReadHandle ()
 Default constructor.
 
 ReadHandle (Error error)
 Constructor for an error handle that always returns a read error.
 
 ReadHandle (const std::error_code &error)
 Constructor for an error handle that returns an arbitrary std::error_code.
 
template<typename Structure >
 ReadHandle (const memory::MemoryHandle< Structure > &memoryHandle)
 Constructor using a memory handle.
 
template<typename Structure >
 ReadHandle (memory::MemoryHandle< Structure > &&memoryHandle)
 Constructor that moves a memory handle.
 
auto nativeType () const -> NativeType
 Gets the native type of the handle.
 
template<typename Type >
auto read () const -> utils::eh::expected< Type, std::error_code >
 Reads the current 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].
 

Detailed Description

A handle for reading a data value.

Member Enumeration Documentation

◆ Error

An enum describing read errors.

Enumerator
NoError 

No error occurred.

NullHandle 

The handle is not associated with any value.

Unknown 

The value's identity could not be resolved. This value is returned for unknown or unsupported attributes, for example.

WriteOnly 

The value is write-only.

NoData 

The value contains no data.

TypeMismatch 

The value could not be converted to the desired type.

Constructor & Destructor Documentation

◆ ReadHandle() [1/5]

xentara::data::ReadHandle::ReadHandle ( )

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).

◆ ReadHandle() [2/5]

xentara::data::ReadHandle::ReadHandle ( Error  error)

Constructor for an error handle that always returns a read error.

This constructor creates a handle that always returns a certain error when reading. Reading the handle will always return make_error_code(error).

Parameters
errorThe error the handle should return when attempting to read from it.

◆ ReadHandle() [3/5]

xentara::data::ReadHandle::ReadHandle ( 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 reading. Reading the handle will always return the given error.

Parameters
errorThe error code the handle should return when attempting to read from it.

◆ ReadHandle() [4/5]

template<typename Structure >
xentara::data::ReadHandle::ReadHandle ( const memory::MemoryHandle< Structure > &  memoryHandle)

Constructor using a memory handle.

Parameters
memoryHandleA handle to the memory containing the value

◆ ReadHandle() [5/5]

template<typename Structure >
xentara::data::ReadHandle::ReadHandle ( memory::MemoryHandle< Structure > &&  memoryHandle)

Constructor that moves a memory handle.

Parameters
memoryHandleA handle to the memory containing the value

Member Function Documentation

◆ errorCategory()

static auto xentara::data::ReadHandle::errorCategory ( ) -> const std::error_category &
static

Gets the error category for error codes of type [Error].

◆ hardError()

auto xentara::data::ReadHandle::hardError ( ) const -> std::error_code

Gets the error of an error handle.

This function returns the error code of an error handle created using ReadHandle(Error error) or ReadHandle(const std::error_code &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().

Returns
The hard error stored in the handle, or std::error_code() if this is not an error handle

◆ nativeType()

auto xentara::data::ReadHandle::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 read() 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 read the value as a string, but handle numbers and Booleans differently.

Returns
A hint as to which type would be best suited to store the value read. For error handles, NativeType::None is returned.

◆ operator==() [1/2]

auto xentara::data::ReadHandle::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 ReadHandle(const std::error_code &error) or ReadHandle(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().

Returns
Returns true if the handle is an error handle and the stored error matches error.

◆ operator==() [2/2]

auto xentara::data::ReadHandle::operator== ( Error  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 ReadHandle(Error error) or ReadHandle(const std::error_code &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().

Returns
Returns true if the handle is an error handle and the stored error matches error.

◆ read()

template<typename Type >
auto xentara::data::ReadHandle::read ( ) const -> utils::eh::expected<Type, std::error_code>

Reads the current value.

The read value will be converted to the type specified by the Type template parameter, if possible. If the value cannot be converted, the function will return Error::TypeMismatch.

You can use std::any as template parameter to retrieve the value without knowing the type beforehand. The returned object will then contain the value is a suitable type wrappen in std::any. This value can then be passed to WriteHandle::write(), for example, to copy the value to somewhere else.

Template Parameters
Typethe type that the value should be converted to, or std::any to return the value as a type-erased std::any object.
Returns
The value read, or an error if an error occurred