DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
ConfigEnum.hpp File Reference

Extended enum-to-JSON serialization macro that also exposes allowed string values for JSON Schema generation. More...

#include "JsonUtil.hpp"
#include <vector>
#include <string>
Include dependency graph for ConfigEnum.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  DNDS::detail::EnumStringPair< EnumType >
 A pair of (enum-value, optional string) used to extract allowed values. More...
 

Namespaces

namespace  DNDS
 the host side operators are provided as implemented
 
namespace  DNDS::detail
 

Macros

#define DNDS_DEFINE_ENUM_JSON(EnumType_, ...)
 Define JSON serialization for an enum AND expose its allowed string values.
 
#define DNDS_ENUM_ALLOWED_VALUES(EnumType_)    _dnds_enum_allowed_values_fn(static_cast<EnumType_ *>(nullptr))
 Get the list of allowed string values for an enum type.
 

Functions

template<typename EnumType >
std::vector< std::string > DNDS::detail::extractEnumStrings (std::initializer_list< EnumStringPair< EnumType > > pairs)
 Extract non-null string values from a list of enum-string pairs.
 

Detailed Description

Extended enum-to-JSON serialization macro that also exposes allowed string values for JSON Schema generation.

Problem

The standard NLOHMANN_JSON_SERIALIZE_ENUM macro generates to_json/from_json overloads for an enum, but provides no way to programmatically query the set of allowed string values at runtime. The config schema generator needs this list to produce "enum": ["Roe", "HLLC", ...] entries.

Solution

DNDS_DEFINE_ENUM_JSON wraps NLOHMANN_JSON_SERIALIZE_ENUM and additionally generates a free function _dnds_enum_allowed_values_<EnumType>() that returns std::vector<std::string> of the valid string representations.

The companion macro DNDS_ENUM_ALLOWED_VALUES(EnumType) calls this function.

Usage

// In Gas.hpp (at namespace scope, after enum definition):
enum RiemannSolverType { UnknownRS, Roe, HLLC, HLLEP };
DNDS_DEFINE_ENUM_JSON(RiemannSolverType,
{UnknownRS, nullptr},
{Roe, "Roe"},
{HLLC, "HLLC"},
{HLLEP, "HLLEP"})
// Then in a config struct's DNDS_DECLARE_CONFIG body:
DNDS_FIELD(rsType, "Riemann solver type",
// The schema will include "enum": ["Roe", "HLLC", "HLLEP"]
#define DNDS_DEFINE_ENUM_JSON(EnumType_,...)
Define JSON serialization for an enum AND expose its allowed string values.
#define DNDS_ENUM_ALLOWED_VALUES(EnumType_)
Get the list of allowed string values for an enum type.
#define DNDS_FIELD(name_, desc_,...)
Register a field inside a DNDS_DECLARE_CONFIG body.
EnumValuesTag enum_values(std::vector< std::string > vals)
Create an enum allowed-values tag.

Backward Compatibility

Existing enums using NLOHMANN_JSON_SERIALIZE_ENUM continue to work for serialization. To also get schema enum values, replace with DNDS_DEFINE_ENUM_JSON. Until that replacement, pass allowed values explicitly via DNDS::Config::enum_values({"Roe","HLLC",...}) in the DNDS_FIELD call.

Convention for the nullptr sentinel

Following the existing DNDSR convention, the first entry in the mapping should map the "unknown/invalid" enum value to nullptr. This entry is skipped when building the allowed-values list for the schema (users should never write null in a config file).

Definition in file ConfigEnum.hpp.

Macro Definition Documentation

◆ DNDS_DEFINE_ENUM_JSON

#define DNDS_DEFINE_ENUM_JSON (   EnumType_,
  ... 
)
Value:
/* (1) Standard nlohmann enum serialization */ \
NLOHMANN_JSON_SERIALIZE_ENUM(EnumType_, __VA_ARGS__) \
/* (2) Allowed-values function for schema generation */ \
inline std::vector<std::string> _dnds_enum_allowed_values_fn(EnumType_ *) \
{ \
return ::DNDS::detail::extractEnumStrings<EnumType_>(__VA_ARGS__); \
}

Define JSON serialization for an enum AND expose its allowed string values.

Drop-in replacement for NLOHMANN_JSON_SERIALIZE_ENUM that additionally generates a function returning the allowed string values for schema emission.

The variadic arguments are {EnumValue, "string"} pairs, identical to those accepted by NLOHMANN_JSON_SERIALIZE_ENUM. Pairs with nullptr as the string (sentinel/unknown values) are excluded from the allowed-values list.

Parameters
EnumType_The enum type.
...Comma-separated {EnumValue, "string_or_nullptr"} pairs.

Generates:

  1. NLOHMANN_JSON_SERIALIZE_ENUM(EnumType_, ...) — standard serialization.
  2. inline std::vector<std::string> _dnds_enum_values_<mangled>() — allowed values. Accessed via DNDS_ENUM_ALLOWED_VALUES(EnumType_).

Definition at line 126 of file ConfigEnum.hpp.

◆ DNDS_ENUM_ALLOWED_VALUES

#define DNDS_ENUM_ALLOWED_VALUES (   EnumType_)     _dnds_enum_allowed_values_fn(static_cast<EnumType_ *>(nullptr))

Get the list of allowed string values for an enum type.

Returns std::vector<std::string>. Only works for enums defined with DNDS_DEFINE_ENUM_JSON. Uses ADL on a dummy pointer argument to find the correct overload of _dnds_enum_allowed_values_fn.

Parameters
EnumType_The enum type (unquoted).

Definition at line 145 of file ConfigEnum.hpp.