xentara-plugin v2.0.4
The Xentara Plugin Framework
Loading...
Searching...
No Matches
LoadingElementConfig.cpp

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

#include "MyQuery.hpp"
#include <xentara/config/Context.hpp>
#include <xentara/config/Errors.hpp>
#include <xentara/model/ElementCategory.hpp>
#include <xentara/skill/Element.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 MyElement class
//#############################################
using namespace std::literals;
using namespace xentara::literals;
// This class represents a MyElement
class MyElement : public xentara::skill::Element
{
public:
// This type contains the meta information for the element type
using Class = xentara::skill::Element::ConcreteClass<"MyElement", "b465bbcc-be55-4087-9458-58eaebfd3250"_uuid, "my element">;
auto category() const noexcept -> model::ElementCategory final
{
}
protected:
auto load(xentara::utils::json::decoder::Object &jsonObject, xentara::config::Context &context) -> 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 MyElement class
//#############################################
auto MyElement::load(xentara::utils::json::decoder::Object &jsonObject, xentara::config::Context &context) -> void
{
// Loop through all the members of the JSON object
for (auto && [name, value] : jsonObject)
{
// Check the different keys
if (name == "minimum"sv)
{
_minimum = value.toNumber<int>();
}
else if (name == "maximum"sv)
{
_maximum = value.asNumber<int>();
}
else if (name == "divisor"sv)
{
_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 (name == "query"sv)
{
// Submit a request to resolve the query to the resolver
resolver.submit(_query);
}
else
{
// Throw an error for unknown parameters
}
}
// 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");
}
}
A context used when loading skill elements.
Definition Context.hpp:56
Convenience subclass of Class that implements all callbacks.
Definition Element.hpp:1032
Base class for elements provided by a Xentara skill.
Definition Element.hpp:82
virtual auto load(utils::json::decoder::Object &jsonObject, config::Context &context) -> void
Called by the framework to load the element’s configuration from a JSON object.
virtual auto category() const noexcept -> model::ElementCategory=0
Callback for getting the element category.
auto throwUnknownParameterError(const utils::json::decoder::Name &name) -> void
Throws an exception denoting that a member of a JSON object is unknown.
ElementCategory
Different categories of Xentara elements.
Definition ElementCategory.hpp:20
@ SpecialPurpose
A skill element representing anything that does not fit into any of the other categories.