$darkmode
#include <confable.hpp>
Public Member Functions | |
| Confable (const Confable &) noexcept | |
| Confable (Confable &&) noexcept=default | |
| Confable & | operator= (const Confable &other) noexcept |
| Confable & | operator= (Confable &&other) noexcept |
| virtual void | reset_schema () |
| Schema & | schema () |
| const Schema & | schema () const |
| virtual void | validate_or_throw (const Conf &c) const |
| virtual bool | validate (const Conf &c, std::optional< SchemaError > &err) const |
| virtual void | from_conf (const Conf &c) |
| virtual void | to_json (Json &j) const |
| Json | to_json () const |
Protected Member Functions | |
| virtual Schema | schema_impl () |
Confable is a base-class that you can inherit for your own types to provide native fable integration.
This provides easy deserialization with validation and serialization, primarily by providing the schema_impl() definition.
Example:
class Foo : public fable::Confable {
double bar;
fable::Schema schema_impl() override {
return fable::Schema{
{"bar", fable::make_schema(&bar, "bar description")},
};
}
using fable::Confable::to_json;
friend void to_json(::fable::Json& j, const xType& t) {
t.to_json(j);
}
friend void from_json(const ::fable::Json& j, xType& t) {
t.from_conf(::fable::Conf{j});
}
};
In order to eliminate some of this boilerplate, the following macro simplifies things and keeps it short and to the point:
class Foo : public fable::Confable {
double bar;
CONFABLE_SCHEMA(Foo) {
using namespace fable;
return Schema{
{"bar", make_schema(&bar, "bar description")},
};
}
};
Your class now be used with Schema and make_schema definitions, and it just works:
class ExtraFine : public fable::Confable {
Foo foo;
CONFABLE_SCHEMA(ExtraFine) {
using namespace fable;
return Schema{
{"foo", make_schema(&foo, "foo description")},
};
}
};
|
virtual |
Deserialize a Confable from a Conf.
Unless you have special needs, it is recommended to implement
Schema schema_impl()
and use the default implementation of this.
Reimplemented in cloe::Frustum, cloe::Command, cloe::component::NormalDistribution< T >, cloe::utility::ContactMap< D >, and cloe::utility::ContactMap< Duration >.
|
virtual |
Reset the internal schema cache.
This causes schema_impl to be called next time the schema is requested.
| Schema & fable::Confable::schema | ( | ) |
Return the object schema for deserialization.
This method uses schema_impl under the hood, which the object should implement.
| const Schema & fable::Confable::schema | ( | ) | const |
Return the object schema for description and validation.
This method uses schema_impl under the hood, which the object should implement.
|
protectedvirtual |
Return a new instance of the schema for this object.
This schema is then stored in schema_. This is called every time the object is created or moved, since Schema contains references to fields that (should be) in the object.
Reimplemented in cloe::component::NormalDistribution< T >, cloe::frustum_culling_plugin::MountPose, cloe::LaneBoundary, and cloe::Frustum.
|
virtual |
Serialize a Confable to Json.
Note: If you implement this type, make sure to either use CONFABLE_FRIENDS or a using Confable::to_json statement in your derived type.
Reimplemented in cloe::frustum_culling_plugin::MountPose, cloe::LaneBoundary, and cloe::Frustum.
|
virtual |
Validate a Conf without applying it and without throwing.
By default, this uses the validate() method on schema(). If you want to guarantee anything extending beyond what's possible with schema, you can do that here.
This method should NOT call from_conf without also overriding from_conf to prevent infinite recursion.
This method should not reset err unless the method returns false.
| c | JSON to validate |
| err | reference to store error in |
|
virtual |
Validate a Conf without applying it.
By default, this uses the validate_or_throw() method on schema(). If you want to guarantee anything extending beyond what's possible with schema, you can do that here.
This method should NOT call from_conf without also overriding from_conf to prevent infinite recursion.