$darkmode
enum.hpp File Reference
#include <map>
#include <string>
#include <type_traits>
#include <utility>
#include <fable/fable_fwd.hpp>
#include <fable/json.hpp>
Include dependency graph for enum.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  fable::EnumSerializer< T >
 
class  fable::has_enum_serializer< T >
 
struct  fable::EnumSerializerImpl< T, EnumSerializerPresent >
 
struct  fable::EnumSerializerImpl< T, true >
 
struct  fable::EnumSerializerImpl< T, false >
 

Macros

#define ENUM_SERIALIZATION(xType, xMap)
 
#define FABLE_ENUM_SERIALIZATION(xType, xMap)
 

Functions

template<typename X , typename Y >
std::map< Y, X > fable::invert (const std::map< X, Y > &m)
 
template<typename T , std::enable_if_t< std::is_enum_v< T >, int > = 0>
const std::map< T, std::string > & fable::enum_serialization ()
 
template<typename T , std::enable_if_t< std::is_enum_v< T >, int > = 0>
const std::map< std::string, T > & fable::enum_deserialization ()
 
template<typename T , std::enable_if_t< std::is_enum_v< T >, int > = 0>
std::string fable::to_string (T x)
 
template<typename T , std::enable_if_t< std::is_enum_v< T >, int > = 0>
fable::from_string (const std::string &s)
 

Variables

template<typename T >
constexpr bool fable::has_enum_serializer_v = has_enum_serializer<T>::value
 

Detailed Description

Macro Definition Documentation

◆ ENUM_SERIALIZATION

#define ENUM_SERIALIZATION (   xType,
  xMap 
)
Value:
inline const std::map<xType, std::string>& enum_serialization(xType) { \
static std::map<xType, std::string> data xMap; \
return data; \
} \
inline const std::map<std::string, xType>& enum_deserialization(xType) { \
static const std::map<std::string, xType> data = ::fable::invert(enum_serialization(xType())); \
return data; \
} \
inline void to_json(::fable::Json& j, const xType& v) { j = enum_serialization(v).at(v); } \
inline void from_json(const ::fable::Json& j, xType& v) { \
v = enum_deserialization(v).at(j.get<std::string>()); \
}
nlohmann::json Json
Definition: fable_fwd.hpp:35

Define serialization and deserilization for an enum.

This must be called in the originating namespace of the enum. If this is not desirable, you can also use FABLE_ENUM_SERIALIZATION.

This macro makes it easier to use enum classes and only need to provide a mapping to strings once. Given the type T, it defines the following functions:

void enum_serialization(const std::map<T, std::string>**) void enum_deserialization(const std::map<std::string, T>**) void to_json(Json&, const T&) void from_json(const Json&, T&)

These functions are used by the Enum class below to provide easy insertion into Schemas.

◆ FABLE_ENUM_SERIALIZATION

#define FABLE_ENUM_SERIALIZATION (   xType,
  xMap 
)
Value:
namespace fable { \
template <> \
struct EnumSerializer<xType> { \
static const std::map<xType, std::string>& serialization() { \
static const std::map<xType, std::string> ser xMap; \
return ser; \
} \
static const std::map<std::string, xType>& deserialization() { \
static std::map<std::string, xType> de = \
::fable::invert(EnumSerializer<xType>::serialization()); \
return de; \
} \
}; \
} \
namespace nlohmann { \
template <> \
struct adl_serializer<xType> { \
static void to_json(json& j, const xType& e) { j = ::fable::to_string(e); } \
static void from_json(const json& j, xType& e) { \
e = ::fable::from_string<xType>(j.get<std::string>()); \
} \
}; \
}

Function Documentation

◆ invert()

template<typename X , typename Y >
std::map<Y, X> fable::invert ( const std::map< X, Y > &  m)

Invert a map.

This requires the map to be an injection; that is, for every element y in Y, there is at most one element x in X, so that (x, y) is in mapping m. It therefore holds: Given {x1, x2} in X and x1 != x2, then m[x1] != m[x2]. This unfortunately precludes the possibility of aliases.

Here is the call graph for this function:
Here is the caller graph for this function: