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>
class MyComponent : public utils::io::Component
{
public:
{
public:
static auto instance() -> Class &
{
return _instance;
}
{
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:
static Class _instance;
};
protected:
auto loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void override;
private:
int _minimum { 0 };
int _maximum { 100 };
double _divisor { 1.0 };
};
MyComponent::Class MyComponent::_instance;
auto MyComponent::loadConfig(const ConfigIntializer &initializer,
utils::json::decoder::Object &jsonObject,
const FallbackConfigHandler &fallbackHandler) -> void
{
for (auto && [key, value] : jsonObject)
{
if (key == "minimum")
{
_minimum = value.toNumber<int>();
}
else if (key == "maximum")
{
_maximum = value.asNumber<int>();
}
else if (key == "divisor")
{
_divisor = value.asNumber<double>();
if (_divisor == 0)
{
utils::json::decoder::throwWithLocation(value,
"the divisor must not be 0");
}
}
else if (key == "ioBatch")
{
resolver.submit(_ioBatch);
}
else
{
fallbackHandler(key, value);
}
}
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