This example demonstrates how to publish configuration attributes of a plugin element.
#include "MyIoBatch.hpp"
#include <xentara/config/Resolver.hpp>
#include <xentara/data/ReadHandle.hpp>
#include <xentara/io/Component.hpp>
#include <xentara/io/ComponentClass.hpp>
#include <xentara/memory/Array.hpp>
#include <xentara/model/Attribute.hpp>
#include <xentara/utils/core/Uuid.hpp>
#include <xentara/utils/json/decoder/Errors.hpp>
#include <xentara/utils/json/decoder/Object.hpp>
class MyComponent : public utils::io::Component
{
private:
struct CustomConfig
{
double factor { 1.0 };
double offset { 0.0 };
};
public:
{
public:
static auto instance() -> Class &
{
return _instance;
}
{
using namespace std::literals;
return "MyComponent"sv;
}
auto uuid() const -> utils::core::Uuid override
{
return "759ce2f0-6ae9-40ea-8c0b-8f19eac38359"_uuid;
}
auto customConfig() const -> const auto &
{
return _customConfig;
}
private:
static Class _instance;
};
protected:
auto loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void override;
auto resolveAttribute(
std::string_view name) ->
const model::Attribute *
override;
auto readHandle(const model::Attribute &attribute) const noexcept -> data::ReadHandle override;
private:
auto customConfig() const -> decltype(auto)
{
return configBlock().handle(Class::instance().configHandle());
}
};
using namespace std::literals;
MyComponent::Class MyComponent::_instance;
auto MyComponent::loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void
{
auto &attributes = initializer[Class::instance().customConfig()];
for (auto && [key, value] : jsonObject)
{
if (key == "factor")
{
attributes.factor = value.toNumber<double>();
}
else if (key == "offset")
{
attributes.offset = value.asNumber<double>();
}
else
{
fallbackHandler(key, value);
}
}
}
auto MyComponent::resolveAttribute(
std::string_view name) ->
const model::Attribute *
{
kFactorAttribute,
kOffsetAttribute);
}
auto MyComponent::readHandle(const model::Attribute &attribute) const noexcept -> data::ReadHandle
{
if (attribute == kFactorAttribute)
{
return customConfig().member(&Config::factor);
}
if (attribute == kOffsetAttribute)
{
return customConfig().member(&Config::offset);
}
}
An object used to resolve cross references in a configuration.
Definition Resolver.hpp:76
static const DataType kFloatingPoint
A floating point value.
Definition DataType.hpp:56
A class of I/O component that a driver supports.
Definition ComponentClass.hpp:29
A handle to an array element containing an object or an array of objects.
Definition Array_ObjectHandle.hpp:11
A Xentara attribute.
Definition Attribute.hpp:47
static auto resolve(const String &name, const Attribute &firstAttribute, const Attributes &... moreAttributes) -> const Attribute *
Checks the name of a list of attributes and returns the first one that matches.
Definition Attribute.hpp:279
@ ReadOnly
The attribute supports reading only.
Definition Attribute.hpp:69