$darkmode
fable::Confable Class Reference

#include <confable.hpp>

Inheritance diagram for fable::Confable:

Public Member Functions

 Confable (const Confable &) noexcept
 
 Confable (Confable &&) noexcept=default
 
Confableoperator= (const Confable &other) noexcept
 
Confableoperator= (Confable &&other) noexcept
 
virtual void reset_schema ()
 
Schemaschema ()
 
const Schemaschema () 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 ()
 

Detailed Description

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")},
    };
  }
};

Member Function Documentation

◆ from_conf()

void fable::Confable::from_conf ( const Conf c)
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 >.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset_schema()

void fable::Confable::reset_schema ( )
virtual

Reset the internal schema cache.

This causes schema_impl to be called next time the schema is requested.

Here is the caller graph for this function:

◆ schema() [1/2]

Schema & fable::Confable::schema ( )

Return the object schema for deserialization.

This method uses schema_impl under the hood, which the object should implement.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ schema() [2/2]

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.

Here is the call graph for this function:

◆ schema_impl()

Schema fable::Confable::schema_impl ( )
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.

Here is the caller graph for this function:

◆ to_json() [1/2]

Json fable::Confable::to_json ( ) const

Serialize a Confable to Json.

◆ to_json() [2/2]

void fable::Confable::to_json ( Json j) const
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.

Here is the call graph for this function:

◆ validate()

bool fable::Confable::validate ( const Conf c,
std::optional< SchemaError > &  err 
) const
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.

Parameters
cJSON to validate
errreference to store error in
Returns
true if validate successful
Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate_or_throw()

void fable::Confable::validate_or_throw ( const Conf c) const
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.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: