$darkmode
#include <factory.hpp>
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 TypeFactory & | get_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) | |
| Interface * | clone () 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< Box > | factory_schemas () const |
| std::vector< Json > | factory_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< Variant > | schema_ |
| 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_ {} |
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.
|
inlineexplicit |
Construct an empty factory.
This schema is useless until you add some factories with the add_factory method.
|
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.
|
inline |
Add a factory with the given key, schema, and function, provided it doesn't already exist.
Return true if successful, false otherwise.
|
inline |
Set the args key and return this for chaining.
|
inline |
Set whether to return only the args subset and return this for chaining.
|
inline |
Set the factory key and return this for chaining.
|
inlineoverridevirtual |
Apply the input JSON configuration.
This does not validate the input.
Implements fable::schema::Interface.
Reimplemented in fable::schema::Factory< DistributionPtr >.
|
inline |
Return the schema and factory function associated with the given key.
|
inline |
Return whether a factory with the given key is available.
|
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.
|
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 >.
|
inline |
Set the args key that is for selecting the input to pass on to the factory function.
|
inline |
Set whether only the args subset of the conf should be passed to the factory function.
The default behavior is true.
|
inline |
Add or replace a factory with the given key, schema, and function.
|
inline |
Set the factory key in the input that is used for selecting the correct factory.
Common choices could be: factory, type, binding.
|
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.
|
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 >.
|
inline |
Set a transform function for the schema and return this for chaining.
|
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.