$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_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< 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 (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< SchemaError > | fail (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) | |
| FactoryBase & | operator= (const FactoryBase &other) |
| FactoryBase (FactoryBase &&) noexcept=default | |
| FactoryBase & | operator= (FactoryBase &&) noexcept=default |
| 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 > | |
| 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 | |
| Interface & | operator= (const Interface &)=default |
| Interface & | operator= (Interface &&) noexcept=default |
Protected Attributes | |
| std::unique_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.
|
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.
|
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 >.
| 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.
|
inline |
Set a transform function for the schema and return this for chaining.
|
inlineoverridevirtual |
Validate the input JSON configuration for correctness.
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.| c | JSON to check |
| error | reference to store error if occurred |
Implements fable::schema::Interface.