libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
Quotient::MetaEnum< EnumT > Struct Template Reference

Enabling structure for conversion between a given enum and JSON. More...

#include <quotient_common.h>

Inheritance diagram for Quotient::MetaEnum< EnumT >:

Detailed Description

template<typename EnumT>
struct Quotient::MetaEnum< EnumT >

Enabling structure for conversion between a given enum and JSON.

Providing this structure in a way that satisfies either Serializable_Enum or Serializable_Flag enables de/serialisation using fromJson() and toJson() for the respective enum. See commented out code in the definition for an idea what should be there to satisfy either concept. strings has the list of JSON representations for respective enumerators. defaultValue should have the enumerator used when the string coming from JSON cannot be found in strings, or when the JSON value is not even a string. isFlag defines whether the enumeration should be treated as a collection of flag values (usually, such enum also has a QFlags counterpart type defined with Q_FLAG, Q_FLAG_NS, QUO_DECLARE_FLAGS or QUO_DECLARE_FLAGS_NS). isFlag can either be false or completely omitted from the definition for non-flag enumerations.

Note
This mechanism doesn't support using numeric values of enumerators in JSON; if you need to store and load the numeric value, use fromJson() and toJson() with the underlying integral type instead of the enumeration type.

The rules to define enumerations and their string representations are different for non-flag and flag enumerations - see the respective note blocks below. These cannot be checked at compile-time, breaking them will lead to either assertion failures or incorrect conversion.

Note
For non-flag enumerations, EnumT must not have gaps in enumerators, or strings has to match those gaps (i.e., if EnumT is defined as { Value1 = 1, Value2 = 3, Value3 = 5 } then strings should be defined as { "", "Value1", "", "Value2", "", "Value3" } (mind the gap at value 0, in particular). Although it is allowed to have enumerations based
For flag enumerations, enumerators of EnumT must follow the power-of-two sequence starting from 1, so exactly 1,2,4,8,16 and so on. Having gaps is allowed but the same rule as for non-flag enumerations applies: strings has to reflect this gap, skipping an array item. As of now, there's no way to encode and decode the value of zero (no flags set) or a combination of flags (i.e. Flag4|Flag16).

In most cases you can use facility macros, QUO_META_ENUM and QUO_META_FLAG, instead of writing all the boilerplate yourself. The enumeration can be defined in any namespace but the macros are only valid when put into namespace Quotient. Clients are free to write namespace Quotient { QUO_META_ENUM(...) } for that matter.

Definition at line 85 of file quotient_common.h.


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