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

This example demonstrates how to load the configuration of a plugin element.

#include "MyIoBatch.hpp"
#include <xentara/config/Resolver.hpp>
#include <xentara/io/Component.hpp>
#include <xentara/io/ComponentClass.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
{
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 "b465bbcc-be55-4087-9458-58eaebfd3250"_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>() };
};
protected:
auto loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void override;
private:
// These are some configuration parameters
int _minimum { 0 };
int _maximum { 100 };
double _divisor { 1.0 };
// This is some reference to another object
};
//#############################################
// Implementation of MyComponent class
//#############################################
MyComponent::Class MyComponent::_instance;
auto MyComponent::loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void
{
// Loop through all the members of the JSON object
for (auto && [key, value] : jsonObject)
{
// Check the different keys
if (key == "minimum")
{
_minimum = value.toNumber<int>();
}
else if (key == "maximum")
{
_maximum = value.asNumber<int>();
}
else if (key == "divisor")
{
_divisor = value.asNumber<double>();
// Check the validity of this particular parameter
if (_divisor == 0)
{
utils::json::decoder::throwWithLocation(value,
"the divisor must not be 0");
}
}
else if (key == "ioBatch")
{
// Submit a request to resolve the I/O batch to the resolver
resolver.submit(_ioBatch);
}
else
{
// Handle the built-in parameters
fallbackHandler(key, value);
}
}
// Check the consistency of all the loaded parameters
if (_maximum <= _minimum)
{
utils::json::decoder::throwWithLocation(jsonObject,
"the maximum must be strictly greater than the minimum");
}
}
An object used to resolve cross references in a configuration.
Definition Resolver.hpp:76
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