AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::Mapping< T > Struct Template Reference

Description of an object for every format (anch-json, ...). More...

#include <mapping.hpp>

Collaboration diagram for anch::Mapping< T >:

Public Types

using Undescribed = void

Detailed Description

template<typename T>
struct anch::Mapping< T >

Description of an object for every format (anch-json, ...).

A type is described by specializing this class template with a registerObject function which registers its fields on the mapper of a format ( anch::ObjectMapper ). The function is a template (the mapper type is the one of each format).

The specialization has to be declared in the header of the type, just after it: every translation unit which uses the type sees its description. A description which is not visible changes the behaviour of formats and validation (the type is not described there) and violates the one definition rule: formats refuse undescribed objects at compile time, and anch-val refuses classes which are neither described nor scalars.

struct Pet {
uint64_t id;
std::string name;
std::string nickname() const { return "little " + name; }
};
// One description of Pet for every format
template<>
struct anch::Mapping<Pet> {
static void
registerObject(anch::ObjectMapper<Pet> auto& mapper) {
mapper
.registerField("id", &Pet::id)
.registerField("name", &Pet::name)
.registerField("nickname", std::function<std::string(const Pet&)>(&Pet::nickname)) // written only
;
}
};
// Types of other libraries can be described too
template<>
struct anch::Mapping<std::pair<std::string, int>> {
static void
registerObject(anch::ObjectMapper<std::pair<std::string, int>> auto& mapper) {
mapper
.registerField("key", &std::pair<std::string, int>::first)
.registerField("value", &std::pair<std::string, int>::second)
;
}
};

Fields are registered with their name and their member pointer, or a getter and / or a setter ( std::function ), and constraints (anch-val: anch::val::required , anch::val::minLength(1) , ...). The primary template marks types which are not described ( anch::Described ).

Template Parameters
Tthe described type
Author
Vincent Lachenal
Since
0.3

Member Typedef Documentation

◆ Undescribed

template<typename T>
using anch::Mapping< T >::Undescribed = void

Marker of the primary template: T is not described


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