$darkmode
fable::schema::FactoryBase< T, CRTP > Class Template Reference

#include <factory.hpp>

Inheritance diagram for fable::schema::FactoryBase< T, CRTP >:
Collaboration diagram for fable::schema::FactoryBase< T, CRTP >:

Classes

struct  TypeFactory
 

Public Types

using Type = T
 
using MakeFunc = std::function< T(const Conf &c)>
 
using TransformFunc = std::function< Box(Struct &&)>
 
using FactoryMap = std::map< std::string, TypeFactory >
 
using FactoryPairList = std::initializer_list< std::pair< std::string, TypeFactory > >
 

Public Member Functions

 FactoryBase (std::string desc="")
 
 FactoryBase (std::string desc, FactoryPairList fs)
 
 FactoryBase (std::string desc, FactoryMap &&fs)
 
CRTP factory_key (const std::string &keyword) &&
 
CRTP args_key (const std::string &keyword) &&
 
CRTP args_subset (bool value) &&
 
CRTP transform_schema (TransformFunc f) &&
 
void set_factory_key (const std::string &keyword)
 
void set_args_key (const std::string &keyword)
 
void set_args_subset (bool value)
 
void set_transform_schema (TransformFunc f)
 
const TypeFactoryget_factory (const std::string &key) const
 
bool has_factory (const std::string &key) const
 
bool add_factory (const std::string &key, Box &&s, MakeFunc f)
 
void set_factory (const std::string &key, Box &&s, MakeFunc f)
 
template<typename F , std::enable_if_t<(std::is_default_constructible_v< F > &&std::is_convertible_v< std::unique_ptr< F >, T >), int > = 0>
void add_default_factory (const std::string &key)
 
Json json_schema () const override
 
bool validate (const Conf &c, std::optional< SchemaError > &err) const override
 
Type make (const Conf &c) const
 
Type deserialize (const Conf &c) const
 
Json serialize (const Type &x) const
 
void serialize_into (Json &j, const Type &x) const
 
void deserialize_into (const Conf &c, Type &x) const
 
void from_conf (const Conf &) override
 
void to_json (Json &) const override
 
void reset_ptr () override
 
virtual Json to_json () const
 
virtual void to_json (Json &) const=0
 
- Public Member Functions inherited from fable::schema::Base< CRTP >
std::unique_ptr< Interfaceclone () const override
 
 operator Box () const
 
JsonType type () const override
 
std::string type_string () const override
 
Json usage () const override
 
bool is_required () const override
 
CRTP require () &&
 
CRTP required (bool value) &&
 
CRTP reset_pointer () &&
 
bool has_description () const
 
void set_description (std::string s) override
 
const std::string & description () const override
 
CRTP description (std::string desc) &&
 
- Public Member Functions inherited from fable::schema::Interface
virtual bool is_variant () const
 
virtual void validate_or_throw (const Conf &c) const final
 
virtual std::optional< SchemaErrorfail (const Conf &c) const final
 
virtual bool is_valid (const Conf &c) const final
 
virtual Json to_json () const
 

Protected Member Functions

 FactoryBase (const FactoryBase &other)
 
FactoryBaseoperator= (const FactoryBase &other)
 
 FactoryBase (FactoryBase &&) noexcept=default
 
FactoryBaseoperator= (FactoryBase &&) noexcept=default
 
void reset_schema ()
 
std::vector< Boxfactory_schemas () const
 
std::vector< Jsonfactory_json_schemas () const
 
- Protected Member Functions inherited from fable::schema::Base< CRTP >
 Base (const Base< CRTP > &)=default
 
 Base (Base< CRTP > &&) noexcept=default
 
Base< CRTP > & operator= (const Base< CRTP > &)=default
 
Base< CRTP > & operator= (Base< CRTP > &&) noexcept=default
 
 Base (JsonType t, std::string desc)
 
 Base (JsonType t)
 
 Base (std::string desc)
 
bool validate_type (const Conf &c, std::optional< SchemaError > &err) const
 
template<typename... Args>
SchemaError error (const Conf &c, std::string_view format, Args &&... args) const
 
SchemaError error (const ConfError &e) const
 
SchemaError wrong_type (const Conf &c) const
 
template<typename... Args>
bool set_error (std::optional< SchemaError > &err, const Conf &c, std::string_view format, Args &&... args) const
 
bool set_error (std::optional< SchemaError > &err, const ConfError &e) const
 
bool set_error (std::optional< SchemaError > &err, SchemaError &&e) const
 
bool set_wrong_type (std::optional< SchemaError > &err, const Conf &c) const
 
void augment_schema (Json &j) const
 
- Protected Member Functions inherited from fable::schema::Interface
 Interface (const Interface &)=default
 
 Interface (Interface &&) noexcept=default
 
Interfaceoperator= (const Interface &)=default
 
Interfaceoperator= (Interface &&) noexcept=default
 

Protected Attributes

std::unique_ptr< Variantschema_
 
TransformFunc transform_func_
 
FactoryMap available_
 
std::string factory_key_ {"factory"}
 
std::string args_key_ {"args"}
 
bool args_subset_ {true}
 
- Protected Attributes inherited from fable::schema::Base< CRTP >
JsonType type_ {JsonType::null}
 
bool required_ {false}
 
std::string desc_ {}
 

Detailed Description

template<typename T, typename CRTP>
class fable::schema::FactoryBase< T, CRTP >

FactoryBase is the base class for Factory and FactoryPointerless.

It is a schema of schemas that can create different objects based on the input. This is why there are two versions, since it may be used in situations where it's not desired to serialize directly into a type via pointer.

Note that this class should not be used directly, instead use Factory or FactoryPointerless. However, the interface of FactoryPointerless and Factory are almost identical, with Factory provided three more constructors with pointer arguments.

Constructor & Destructor Documentation

◆ FactoryBase()

template<typename T , typename CRTP >
fable::schema::FactoryBase< T, CRTP >::FactoryBase ( std::string  desc = "")
inlineexplicit

Construct an empty factory.

This schema is useless until you add some factories with the add_factory method.

See also
add_factory()

Member Function Documentation

◆ add_default_factory()

template<typename T , typename CRTP >
template<typename F , std::enable_if_t<(std::is_default_constructible_v< F > &&std::is_convertible_v< std::unique_ptr< F >, T >), int > = 0>
void fable::schema::FactoryBase< T, CRTP >::add_default_factory ( const std::string &  key)
inline

Add a factory which creates a default instance of F and calls from_conf on F.

  • F must be default-constructible.
  • F must have a from_conf() method.
  • F must have a schema, i.e., make_prototype<F>() should be valid.
  • std::unique_ptr<F> must be convertible to T. This means that T must be either a std::unique_ptr<Base> or std::shared_ptr<Base>, where Base is a base class of F.

Most types inheriting from Confable will fulfill these requirements.

Here is the call graph for this function:

◆ add_factory()

template<typename T , typename CRTP >
bool fable::schema::FactoryBase< T, CRTP >::add_factory ( const std::string &  key,
Box &&  s,
MakeFunc  f 
)
inline

Add a factory with the given key, schema, and function, provided it doesn't already exist.

Return true if successful, false otherwise.

Here is the caller graph for this function:

◆ args_key()

template<typename T , typename CRTP >
CRTP fable::schema::FactoryBase< T, CRTP >::args_key ( const std::string &  keyword) &&
inline

Set the args key and return this for chaining.

See also
set_args_key()
Here is the call graph for this function:

◆ args_subset()

template<typename T , typename CRTP >
CRTP fable::schema::FactoryBase< T, CRTP >::args_subset ( bool  value) &&
inline

Set whether to return only the args subset and return this for chaining.

See also
set_args_subset()
Here is the call graph for this function:

◆ factory_key()

template<typename T , typename CRTP >
CRTP fable::schema::FactoryBase< T, CRTP >::factory_key ( const std::string &  keyword) &&
inline

Set the factory key and return this for chaining.

See also
set_factory_key()
Here is the call graph for this function:

◆ from_conf()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::from_conf ( const Conf )
inlineoverridevirtual

Apply the input JSON configuration.

This does not validate the input.

Implements fable::schema::Interface.

Reimplemented in fable::schema::Factory< DistributionPtr >.

◆ get_factory()

template<typename T , typename CRTP >
const TypeFactory& fable::schema::FactoryBase< T, CRTP >::get_factory ( const std::string &  key) const
inline

Return the schema and factory function associated with the given key.

◆ has_factory()

template<typename T , typename CRTP >
bool fable::schema::FactoryBase< T, CRTP >::has_factory ( const std::string &  key) const
inline

Return whether a factory with the given key is available.

◆ json_schema()

template<typename T , typename CRTP >
Json fable::schema::FactoryBase< T, CRTP >::json_schema ( ) const
inlineoverridevirtual

Return the JSON schema.

Example output:

{ "$schema": "http://json-schema.org/draft-07/schema#", "description": "stand-in no-operation simulator", "properties": { "vehicles": { "description": "list of vehicle names to make available", "items": { "type": "string" }, "type": "array" } }, "title": "nop", "additionalProperties": false, "type": "object" }

See the following links for the specification:

Implements fable::schema::Interface.

◆ reset_ptr()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::reset_ptr ( )
inlineoverridevirtual

Reset the internal pointer to nullptr, protecting against invalid access.

This should be used when a schema is used after the backing data has been deleted.

Implements fable::schema::Interface.

Reimplemented in fable::schema::Factory< DistributionPtr >.

◆ set_args_key()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::set_args_key ( const std::string &  keyword)
inline

Set the args key that is for selecting the input to pass on to the factory function.

  • This affects the final schema used for validation.
  • If the keyword is an empty string, the same object space is used as contains the factory keyword, so the factory schema should not use that keyword.
Here is the caller graph for this function:

◆ set_args_subset()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::set_args_subset ( bool  value)
inline

Set whether only the args subset of the conf should be passed to the factory function.

The default behavior is true.

  • If true, only the args subset of incoming confs will be passed on to the factory function. If the args key is empty, then only the factory key is erased from the conf.
  • If false, the incoming args is passed to the factory function as-is.
Here is the caller graph for this function:

◆ set_factory()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::set_factory ( const std::string &  key,
Box &&  s,
MakeFunc  f 
)
inline

Add or replace a factory with the given key, schema, and function.

◆ set_factory_key()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::set_factory_key ( const std::string &  keyword)
inline

Set the factory key in the input that is used for selecting the correct factory.

Common choices could be: factory, type, binding.

Here is the caller graph for this function:

◆ set_transform_schema()

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::set_transform_schema ( TransformFunc  f)
inline

Set the schema transform function, which is applied to each factory schema after taking factory key, args key, and args subset settings into consideration.

The default behavior is the identity function.

Here is the caller graph for this function:

◆ to_json() [1/3]

template<typename T , typename CRTP >
virtual Json fable::schema::Interface::to_json
inline

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

◆ to_json() [2/3]

template<typename T , typename CRTP >
void fable::schema::FactoryBase< T, CRTP >::to_json ( Json ) const
inlineoverridevirtual

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

Implements fable::schema::Interface.

Reimplemented in fable::schema::Factory< DistributionPtr >.

◆ to_json() [3/3]

template<typename T , typename CRTP >
virtual void fable::schema::Interface::to_json

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

◆ transform_schema()

template<typename T , typename CRTP >
CRTP fable::schema::FactoryBase< T, CRTP >::transform_schema ( TransformFunc  f) &&
inline

Set a transform function for the schema and return this for chaining.

See also
set_transform_schema()
Here is the call graph for this function:

◆ validate()

template<typename T , typename CRTP >
bool fable::schema::FactoryBase< T, CRTP >::validate ( const Conf c,
std::optional< SchemaError > &  error 
) const
inlineoverridevirtual

Validate the input JSON configuration for correctness.

  • This method should only set error if there is an error. This method should not reset error if there is no error. Therefore, the content of error is only valid if the method returns false. This allows you to chain validates and check at the end if there was an error.
  • This method should not throw if there is a schema error!
Parameters
cJSON to check
errorreference to store error if occurred
Returns
true if valid

Implements fable::schema::Interface.

Here is the call graph for this function:

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