cronch

Metadata

Aka bootleg reflection

Introduction

cronch is mostly an interface for consuming metadata. The rest of its API is just layers on other libraries that do all the heavy lifting.

Customisation points

template<typename T>
class metadata

This is the main customisation point of cronch. It is used to add a type to the introspection system and to thereby allow the serializing and deserializing of it.

The unspecialised form has no members and is completely empty. To make a type consumable by cronch, you must specialise this struct for your type and define the needed members. See Metadata concepts for more information.

Possible fields

meta::concepts::view members

Members of the type for use when serializing and deserializing. See meta::mems for the provided concrete type for this.

const char *name

Name of the type used by some serializers and deserializers (e.g. xml::pugi)

Example

#include <cronch/meta.hpp>
#include <cronch/metadata.hpp>

struct my_type {
    int my_field;
};

namespace cronch {
template<>
struct metadata<my_type> {
    static constexpr auto name = "my_type";
    static constexpr auto fields = cronch::meta::fields(
        cronch::meta::field("my_field", &my_type::my_field));
};

} // namespace cronch

Helper macros

CRONCH_META_TYPE(type, fields)

Shortcut for generating metadata<type>. The name is set to type while the fields are passed to meta::mems and set as the value of metadata<type>::members.

Example


#include <cronch/meta.hpp>

struct my_type {
    int i;
};

CRONCH_META_TYPE(my_type, (cronch::meta::field("i", &my_type::i)))
CRONCH_META_TYPE(type)

Same as CRONCH_META_TYPE(type, fields) but without the fields

Important Note

These macros must be used either in the cronch namespace or outside of any namespaces.

Registration helpers

template<typename T, typename V>
class field

Holds a field belonging to T with type V.

Constructors

constexpr field::field(std::string_view name, V T::* n)
template<typename T, typename R, typename S>
class property

Holds a property of T, the value of which is set via setter and obtained via getter. serializing a type requires all getters for it to be non-null while deserializing it requires all setters to be non-null

Constructors

constexpr property::property(std::string_view name, R (T::* getter) = nullptr, void (T::* setter) = nullptr)
constexpr property::property(std::string_view name, R(* getter)(const T* ) = nullptr, void(* setter)(T* , S) = nullptr)
constexpr property::property(std::string_view name, R(* getter)(const T&) = nullptr, void(* setter)(T&, S) = nullptr)
template<typename ...Fs>
class mems

Holds the members of a type (which may be property or field’s)

Constructors

constexpr mems::mems(Fs... fs)

Note

All these types are simply implementations of concepts in Metadata concepts that are provided to make cronch easier to use. There is nothing to stop you defining your own accessor types and using those instead of/in conjunction with the above.

Querying

template<has_members T>
const view auto &accessors()

Returns a const reference to a type satisfying view that refers to the members of T satisfying accessor

template<serializable T>
std::string_view nameof()

Returns the name of the type as registered via metadata