ezconfig
Create C++ Objects from Yaml and Json
Loading...
Searching...
No Matches
yaml.hpp File Reference

Yaml factory. More...

#include <yaml-cpp/yaml.h>
#include "factory.hpp"
#include "yaml_fwd.hpp"
Include dependency graph for yaml.hpp:

Go to the source code of this file.

Concepts

concept  ezconfig::yaml::YamlParseable
 

Macros

#define EZ_YAML_DEFINE(Base)
 Define a global yaml factory for a base class.
 
#define EZ_YAML_REGISTER(Base, tag, Derived, ...)
 Register a conversion method with the global yaml factory.
 

Functions

template<typename Base , typename Derived , typename Intermediate = Derived>
requires ( std::is_base_of_v<Base, Derived> && YamlParseable<Intermediate> && std::is_constructible_v<Derived, Intermediate &&>)
void ezconfig::yaml::Add (const std::string &tag)
 Add a factory method.
 
template<typename Base >
std::unique_ptr< Base > ezconfig::yaml::Create (const YAML::Node &y)
 Create an object from yaml using the global factory.
 

Detailed Description

Yaml factory.

Include this file in implementation file for the base class and any derived classes.

Definition in file yaml.hpp.

Macro Definition Documentation

◆ EZ_YAML_DEFINE

#define EZ_YAML_DEFINE ( Base)
Value:
EZ_FACTORY_DEFINE(Base, const YAML::Node &); \
template std::unique_ptr<Base> ezconfig::yaml::Create(const YAML::Node &); \

Define a global yaml factory for a base class.

Do this in the base class implementation file.

Parameters
Basefactory base class.

Example: Define a MyBase yaml factory.

#define EZ_YAML_DEFINE(Base)
Define a global yaml factory for a base class.
Definition yaml.hpp:29

Definition at line 29 of file yaml.hpp.

◆ EZ_YAML_REGISTER

#define EZ_YAML_REGISTER ( Base,
tag,
Derived,
... )
Value:
EZ_STATIC_INVOKE(&ezconfig::yaml::Add<Base, Derived __VA_OPT__(, ) __VA_ARGS__>, tag)

Register a conversion method with the global yaml factory.

Do this in the implementation files for derived classes.

Parameters
Basefactory base class.
tagconversion identifier (string).
Derivedfactory derived class.
Intermediateoptional intermediate class.
Note
Intermediate can be omitted. In that case Derived must be yaml-parseable.
If provided, Intermediate must be a yaml-parsable type.

Example: Register a creator for MyDerived with tag "!mytag".

EZ_YAML_REGISTER(MyBase, "!mytag", MyDerived, MyDerivedConfig);
#define EZ_YAML_REGISTER(Base, tag, Derived,...)
Register a conversion method with the global yaml factory.
Definition yaml.hpp:53

Definition at line 53 of file yaml.hpp.

Function Documentation

◆ Add()

template<typename Base , typename Derived , typename Intermediate = Derived>
requires ( std::is_base_of_v<Base, Derived> && YamlParseable<Intermediate> && std::is_constructible_v<Derived, Intermediate &&>)
void ezconfig::yaml::Add ( const std::string & tag)

Add a factory method.

Template Parameters
Derivedsub-class of Base.
Intermediatetype that is parseable from yaml, and that can construct Derived.

Adds conversions from yaml of the form

!tag object

to Derived, where object is yaml-converted to Intermediate.

Definition at line 82 of file yaml.hpp.

◆ Create()

template<typename Base >
std::unique_ptr< Base > ezconfig::yaml::Create ( const YAML::Node & y)

Create an object from yaml using the global factory.

Template Parameters
Basefactory base class
Parameters
yyaml data
auto obj = yaml::Create<MyBase>(YAML::Load(data));

Definition at line 90 of file yaml.hpp.