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

#include <factory.hpp>

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

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< F >::value &&std::is_convertible< std::unique_ptr< F >, T >::value), int > = 0>
void add_default_factory (const std::string &key)
 
Json json_schema () const override
 
void validate (const Conf &c) const override
 
Type make (const Conf &c) const
 
Type deserialize (const Conf &c) const
 
Json serialize (const Type &x) const
 
void from_conf (const Conf &c) override
 
void to_json (Json &j) const override
 
void reset_ptr () override
 
- Public Member Functions inherited from fable::schema::Base< CRTP >
 Base (JsonType t, std::string &&desc)
 
 Base (JsonType t)
 
 Base (std::string &&desc)
 
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 (const std::string &s) override
 
void set_description (std::string &&s)
 
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 bool is_valid (const Conf &c) const
 
virtual Json to_json () const
 

Protected Member Functions

void reset_schema ()
 
std::vector< Boxfactory_schemas () const
 
std::vector< Jsonfactory_json_schemas () const
 
- Protected Member Functions inherited from fable::schema::Base< CRTP >
void validate_type (const Conf &c) const
 
template<typename... Args>
void throw_error (const Conf &c, const char *format, Args... args) const
 
void throw_error (const ConfError &e) const
 
void throw_wrong_type (const Conf &c) const
 
void augment_schema (Json &j) const
 

Protected Attributes

std::shared_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()
Here is the caller graph for this function:

Member Function Documentation

◆ add_default_factory()

template<typename T, typename CRTP>
template<typename F , std::enable_if_t<(std::is_default_constructible< F >::value &&std::is_convertible< std::unique_ptr< F >, T >::value), 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.

◆ 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()

◆ 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()

◆ 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()

◆ 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()

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 >.

◆ 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()

◆ validate()

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

Validate the input JSON configuration.

If you don't have a Conf but would like to validate a Json type, then construct a Conf on the fly:

s.validate(Conf{j});

This function is not provided inline to prevent incorrect use.

Implements fable::schema::Interface.


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