xentara-plugin v1.2.1
The Xentara Plugin Framework
Loading...
Searching...
No Matches
PublishingConfigAttributes.cpp

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>
#include <memory>
#include <string_view>
//#############################################
// Declaration of MyComponent class
//#############################################
// This class represents a MyComponent
class MyComponent : public utils::io::Component
{
private:
// A structure to hold the published configuration attributes
struct CustomConfig
{
double factor { 1.0 };
double offset { 0.0 };
};
public:
// This class contains the meta information for the component type
class Class : public xentara::io::ComponentClass
{
public:
static auto instance() -> Class &
{
return _instance;
}
auto name() const -> std::string_view override
{
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:
// The global instance of the class
static Class _instance;
// An array handle to the custom config attributes within the config memory block
xentara::memory::Array::ObjectHandle<CustomConfig> _customConfig { config().appendObject<CustomConfig>() };
};
// These are the objects that identify the attributes
static const xentara::model::Attribute kFactorAttribute;
static const xentara::model::Attribute kOffsetAttribute;
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:
// This is a conveniance function used to access the custom configuration
auto customConfig() const -> decltype(auto)
{
return configBlock().handle(Class::instance().configHandle());
}
};
//#############################################
// Implementation of MyComponent class
//#############################################
using namespace std::literals;
const xentara::model::Attribute MyComponent::kFactorAttribute{ "cb94c929-1e9e-4153-80c8-82b48a2abd73"_uuid, "factor"sv, xentara::model::Attribute::Access::ReadOnly, xentara::data::DataType::kFloatingPoint };
const xentara::model::Attribute MyComponent::kOffsetAttribute{ "0a648093-7293-42f4-8f25-b0d315c8622a"_uuid, "offset"sv, xentara::model::Attribute::Access::ReadOnly, xentara::data::DataType::kFloatingPoint };
MyComponent::Class MyComponent::_instance;
auto MyComponent::loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void
{
// First, get access our custom attributes from within the config memory block
auto &attributes = initializer[Class::instance().customConfig()];
// Loop through all the members of the JSON object
for (auto && [key, value] : jsonObject)
{
// Check the different keys
if (key == "factor")
{
attributes.factor = value.toNumber<double>();
}
else if (key == "offset")
{
attributes.offset = value.asNumber<double>();
}
else
{
// Handle the built-in parameters
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