garlic::Module Class Reference

A Module is a repository of models, fields and constraints. More...

#include <module.h>

Public Member Functions

 Module ()
 
tl::expected< void, std::error_code > add_model (model_pointer model) noexcept
 
tl::expected< void, std::error_code > add_field (text &&alias, field_pointer field) noexcept
 
tl::expected< void, std::error_code > add_field (field_pointer field) noexcept
 
model_pointer get_model (const text &name) const noexcept
 
template<typename Callable >
void get_model (const text &name, Callable &&cb) const noexcept
 
const_model_iterator begin_models () const
 
const_model_iterator end_models () const
 
const_model_iterator find_model (const text &name) const
 
field_pointer get_field (const text &name) const noexcept
 
template<typename Callable >
void get_field (const text &name, Callable &&cb) const noexcept
 Calls the callable function with a shared pointer to the field, if found.
 
const_field_iterator begin_fields () const
 
const_field_iterator end_fields () const
 
const_field_iterator find_field (const text &name) const
 

Detailed Description

A Module is a repository of models, fields and constraints.

This allows to package lots of relevant fields and models in one object. This can be parsed and dumped which makes it easy to save in a file or send it over the wire.

In a module, no two records can exist with the same entity and they cannot be overriden. So if a Field has already been added by the name "Field", this cannot be exchanged for any other Field. This is mostly because other Model or Constraint instances might already depend on it.

module.add_field(garlic::make_field("ID", { garlic::make_constraint<garlic::regex_tag>("\\d{1,3}") }));
module.add_field("UID", module.get_field("ID")); // an alias for field ID.
auto user_model = garlic::make_model("User");
user_model->add_field("id", module.get_field("UID"));
module.add_model(user_model);
Note
Try not to add models or fields that depend on resources outside of this module. That would make it impossible to deserialize should that become necessary.

Constructor & Destructor Documentation

◆ Module()

garlic::Module::Module ( )
inline

Creates an empty module with the following fields already available: string, bool, list, object, integer, double

Member Function Documentation

◆ add_field() [1/2]

tl::expected<void, std::error_code> garlic::Module::add_field ( field_pointer  field)
inlinenoexcept

Tries to add a new field to the module, using its name for the field record.

Parameters
fieldA shared pointer to the Field.
Returns
nothing if successful, std::error_code if a field record by the same alias already exists.

◆ add_field() [2/2]

tl::expected<void, std::error_code> garlic::Module::add_field ( text &&  alias,
field_pointer  field 
)
inlinenoexcept

Tries to add a new field to the module.

Parameters
aliasA custom name or an alias to use for the field.
fieldA shared pointer to the Field.
Returns
nothing if successful, std::error_code if a field record by the same alias already exists.

◆ add_model()

tl::expected<void, std::error_code> garlic::Module::add_model ( model_pointer  model)
inlinenoexcept

Tries to add a new model to the module.

Parameters
modelA shared pointer to the Model.
Returns
nothing if successful, std::error_code if a model by the same name already exists.

◆ begin_fields()

const_field_iterator garlic::Module::begin_fields ( ) const
inline
Returns
a read-only (const) iterator that points to the first field in the module.

◆ begin_models()

const_model_iterator garlic::Module::begin_models ( ) const
inline
Returns
a read-only (const) iterator that points to the first model in the module.

◆ end_fields()

const_field_iterator garlic::Module::end_fields ( ) const
inline
Returns
a read-only (const) iterator that points to one past the last field in the module.

◆ end_models()

const_model_iterator garlic::Module::end_models ( ) const
inline
Returns
a read-only (const) iterator that points to one past the last model in the module.

◆ find_field()

const_field_iterator garlic::Module::find_field ( const text name) const
inline
Returns
a read-only (const) iterator that points to found element or end_fields() if not found.

◆ find_model()

const_model_iterator garlic::Module::find_model ( const text name) const
inline
Returns
a read-only (const) iterator that points to found element or end_models() if not found.

◆ get_field()

field_pointer garlic::Module::get_field ( const text name) const
inlinenoexcept
Returns
a shared pointer to the found field or nullptr if not found.

◆ get_model() [1/2]

model_pointer garlic::Module::get_model ( const text name) const
inlinenoexcept

Get a model by its name.

Returns
a shared pointer to the model if found, otherwise nullptr.

◆ get_model() [2/2]

template<typename Callable >
void garlic::Module::get_model ( const text name,
Callable &&  cb 
) const
inlinenoexcept

Get a model by its name. Calls the callback function with a shared pointer to the Model if found.


The documentation for this class was generated from the following file:
garlic::Module
A Module is a repository of models, fields and constraints.
Definition: module.h:39
garlic::Module::add_model
tl::expected< void, std::error_code > add_model(model_pointer model) noexcept
Definition: module.h:76
garlic::Module::get_field
field_pointer get_field(const text &name) const noexcept
Definition: module.h:134
garlic::Module::add_field
tl::expected< void, std::error_code > add_field(text &&alias, field_pointer field) noexcept
Definition: module.h:90