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

This example demonstrates how to publish configuration attributes of a skill element.

#include <xentara/config/Context.hpp>
#include <xentara/config/Errors.hpp>
#include <xentara/data/ReadHandle.hpp>
#include <xentara/model/Attribute.hpp>
#include <xentara/model/ElementCategory.hpp>
#include <xentara/model/ForEachAttributeFunction.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>
#include <optional>
//#############################################
// Declaration of MyElement class
//#############################################
using namespace std::literals;
using namespace xentara::literals;
// This class represents a MyElement
class MyElement final : public xentara::skill::Element, public xentara::skill::EnableSharedFromThis<MyElement>
{
public:
// This type contains the meta information for the element type
using Class = xentara::skill::Element::ConcreteClass<"MyElement", "759ce2f0-6ae9-40ea-8c0b-8f19eac38359"_uuid, "my element">;
auto category() const noexcept -> model::ElementCategory final
{
}
// These are the objects that identify the attributes
static const xentara::model::Attribute kFactorAttribute;
static const xentara::model::Attribute kOffsetAttribute;
protected:
auto load(xentara::utils::json::decoder::Object &jsonObject, xentara::config::Context &context) -> void override;
auto forEachAttribute(const model::ForEachAttributeFunction &function) const -> bool final;
auto makeReadHandle(const model::Attribute &attribute) const noexcept -> std::optional<data::ReadHandle> final;
private:
// These are some configuration parameters
double factor { 1.0 };
double offset { 0.0 };
};
//#############################################
// Implementation of MyElement class
//#############################################
using namespace std::literals;
const xentara::model::Attribute MyElement::kFactorAttribute{ "cb94c929-1e9e-4153-80c8-82b48a2abd73"_uuid, "factor"sv, xentara::model::Attribute::Access::ReadOnly, xentara::data::DataType::kFloatingPoint };
const xentara::model::Attribute MyElement::kOffsetAttribute{ "0a648093-7293-42f4-8f25-b0d315c8622a"_uuid, "offset"sv, xentara::model::Attribute::Access::ReadOnly, xentara::data::DataType::kFloatingPoint };
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 == "factor"sv)
{
_factor = value.toNumber<double>();
}
else if (name == "offset"sv)
{
_offset = value.asNumber<double>();
}
else
{
// Throw an error for unknown parameters
}
}
}
{
return
function(kFactorAttribute) ||
function(kOffsetAttribute);
}
{
if (attribute == kFactorAttribute)
{
return sharedFromThis(&_factor);
}
if (attribute == kOffsetAttribute)
{
return sharedFromThis(&_offset);
}
}
A context used when loading skill elements.
Definition Context.hpp:56
static const DataType kFloatingPoint
A floating point value.
Definition DataType.hpp:54
A Xentara attribute.
Definition Attribute.hpp:29
@ ReadOnly
The attribute supports reading only.
A function object used to iterate over all the attributes of an element.
Definition ForEachAttributeFunction.hpp:25
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 makeReadHandle(const model::Attribute &attribute) const noexcept -> std::optional< data::ReadHandle >
Called by the framework to get a read handle for a specific attribute.
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.
virtual auto forEachAttribute(const model::ForEachAttributeFunction &function) const -> bool
Called by the framework to iterate over all the attributes.
A mixin class that enables getting a shared pointer from the this pointer.
Definition EnableSharedFromThis.hpp:18
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.