Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/content/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,26 @@ constexpr auto enumerator_array = rfl::get_enumerator_array<Color>();

In case it's more convenient to get the enumerator values as values of the enum's underlying type rather than values of the enum type itself,
there is also `rfl::get_underlying_enumerators<EnumType>()` and `rfl::get_underlying_enumerator_array<EnumType>()`.

## Custom wire names (`rfl::EnumNames` and `rfl::TransformEnumNames`)

To customize serialized enum wire names across JSON, CLI, Generic, XML, YAML, and JSON Schema without duplicating enumerators as `rfl::Literal<...>`, specialize `rfl::EnumNames<E>` and/or pass a `rfl::TransformEnumNames<...>` processor (`rfl::EnumSnakeCaseToKebabCase`, `rfl::EnumSnakeCaseToCamelCase`, `rfl::EnumSnakeCaseToPascalCase`, `rfl::EnumCamelCaseToSnakeCase`, `rfl::EnumToLowerCase`, `rfl::EnumToUpperCase`):

```cpp
enum class OptimizationLevel { no_opt, size_opt, speed_opt, cxx_standard };

template <>
struct rfl::EnumNames<OptimizationLevel> {
static constexpr rfl::EnumNameTransform transform =
rfl::EnumNameTransform::snake_to_kebab;
static constexpr std::array<rfl::EnumOverride<OptimizationLevel>, 1>
overrides = {{
{OptimizationLevel::cxx_standard, "c++20"},
}};
};
```

- `overrides` (either a static contiguous range or a static `overrides()` returning a borrowed contiguous range such as `std::span<const rfl::EnumOverride<E>>`) takes precedence over `transform`, and also supports enumerators outside the default reflected range.
- `transform` can be either a `rfl::EnumNameTransform` constant or a static callable `transform(std::string_view)` returning `std::string` or `std::string_view`.
- If `rfl::EnumNames<E>` only defines `overrides` (without `transform`), non-overridden enumerators still receive any `rfl::TransformEnumNames<...>` processor passed to `read`, `write`, or `to_schema`.
- Use `rfl::get_enum_wire_names<E, Processors...>()` to inspect resolved `(wire_name, enumerator)` pairs.
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rfl/DefaultVal.hpp"
#include "rfl/Description.hpp"
#include "rfl/DurationsAsCount.hpp"
#include "rfl/EnumNames.hpp"
#include "rfl/EnumNamesOnly.hpp"
#include "rfl/ExtraFields.hpp"
#include "rfl/FailFast.hpp"
Expand Down
150 changes: 150 additions & 0 deletions include/rfl/EnumNames.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#ifndef RFL_ENUMNAMES_HPP_
#define RFL_ENUMNAMES_HPP_

#include <concepts>
#include <cstddef>
#include <iterator>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

#include "Processors.hpp"
#include "TransformEnumNames.hpp"
#include "internal/enum_name_transform_v.hpp"

namespace rfl {

/// Pair of `(enumerator, wire_name)` for per-enumerator overrides in
/// `rfl::EnumNames<E>`.
template <class EnumType>
using EnumOverride = std::pair<EnumType, std::string_view>;

/// Specialize `rfl::EnumNames<E>` to customize wire names for enum `E` across
/// JSON, CLI, Generic, YAML, XML, and JSON Schema without duplicating
/// enumerators as `rfl::Literal<...>`.
///
/// Supported members (individually or combined; `overrides` takes precedence):
/// - `static constexpr EnumNameTransform transform = EnumNameTransform::snake_to_kebab;`
/// OR `static std::string transform(std::string_view _raw_name);`
/// (any return type convertible or explicitly constructible into `std::string`,
/// such as `std::string_view`, is accepted)
/// - `static constexpr std::array<rfl::EnumOverride<E>, N> overrides = {{ ... }};`
/// OR `static std::span<const rfl::EnumOverride<E>> overrides();`
template <class EnumType>
struct EnumNames {};

namespace internal {

template <class T>
struct is_std_span : std::false_type {};

template <class T, std::size_t Extent>
struct is_std_span<std::span<T, Extent>> : std::true_type {};

template <class R, class E>
concept EnumOverrideContiguousRange =
(std::is_array_v<std::remove_cvref_t<R>> &&
std::same_as<
std::remove_cv_t<std::remove_extent_t<std::remove_cvref_t<R>>>,
EnumOverride<E>>) ||
requires(const std::remove_reference_t<R>& _r) {
{ _r.data() } -> std::convertible_to<const EnumOverride<E>*>;
{ _r.size() } -> std::convertible_to<std::size_t>;
};

template <class R>
concept BorrowedEnumOverridesResult =
std::is_lvalue_reference_v<R> ||
is_std_span<std::remove_cvref_t<R>>::value;

template <class E>
concept HasEnumOverridesVar = requires {
requires EnumOverrideContiguousRange<decltype(EnumNames<E>::overrides), E>;
};

template <class E>
concept HasAnyEnumOverridesFn = requires {
requires EnumOverrideContiguousRange<decltype(EnumNames<E>::overrides()), E>;
};

template <class E>
concept HasEnumOverridesFn =
HasAnyEnumOverridesFn<E> &&
BorrowedEnumOverridesResult<decltype(EnumNames<E>::overrides())>;

template <class E>
concept HasEnumTransformConst = requires {
{ EnumNames<E>::transform } -> std::convertible_to<EnumNameTransform>;
};

template <class E>
concept HasEnumTransformFn = requires(std::string_view _s) {
requires std::constructible_from<std::string,
decltype(EnumNames<E>::transform(_s))>;
};

template <class E, EnumNameTransform _transform = EnumNameTransform::none>
inline constexpr bool has_custom_enum_names_for_transform_v =
HasEnumOverridesVar<std::remove_cvref_t<E>> ||
HasAnyEnumOverridesFn<std::remove_cvref_t<E>> ||
HasEnumTransformConst<std::remove_cvref_t<E>> ||
HasEnumTransformFn<std::remove_cvref_t<E>> ||
(_transform != EnumNameTransform::none);

template <class E, class ProcessorsType = Processors<>>
inline constexpr bool has_custom_enum_names_v =
has_custom_enum_names_for_transform_v<
E, enum_name_transform_v<ProcessorsType>>;

template <class E>
inline std::span<const EnumOverride<std::remove_cvref_t<E>>>
get_enum_overrides() {
using U = std::remove_cvref_t<E>;
static_assert(
!HasAnyEnumOverridesFn<U> || HasEnumOverridesFn<U>,
"rfl::EnumNames<E>::overrides() must return a borrowed contiguous range "
"(for example, std::span<const rfl::EnumOverride<E>> or const auto&).");
if constexpr (HasEnumOverridesFn<U>) {
auto&& r = EnumNames<U>::overrides();
return std::span<const EnumOverride<U>>(std::data(r), std::size(r));
} else if constexpr (HasEnumOverridesVar<U>) {
return std::span<const EnumOverride<U>>(std::data(EnumNames<U>::overrides),
std::size(EnumNames<U>::overrides));
} else {
return {};
}
}

template <class E, EnumNameTransform _transform = EnumNameTransform::none>
inline std::string resolve_enum_wire_name_for_transform(
const E _val, const std::string_view _raw_name) {
using U = std::remove_cvref_t<E>;
for (const auto& [e, wire] : get_enum_overrides<U>()) {
if (e == _val) {
return std::string(wire);
}
}
if constexpr (HasEnumTransformFn<U>) {
return std::string(EnumNames<U>::transform(_raw_name));
} else if constexpr (HasEnumTransformConst<U>) {
return apply_enum_name_transform(_raw_name, EnumNames<U>::transform);
} else if constexpr (_transform != EnumNameTransform::none) {
return apply_enum_name_transform(_raw_name, _transform);
} else {
return std::string(_raw_name);
}
}

template <class E, class ProcessorsType = Processors<>>
inline std::string resolve_enum_wire_name(const E _val,
const std::string_view _raw_name) {
return resolve_enum_wire_name_for_transform<
E, enum_name_transform_v<ProcessorsType>>(_val, _raw_name);
}

} // namespace internal
} // namespace rfl

#endif // RFL_ENUMNAMES_HPP_
44 changes: 32 additions & 12 deletions include/rfl/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>
#include <vector>

#include "EnumNames.hpp"
#include "Processors.hpp"
#include "Result.hpp"
#include "TransformEnumNames.hpp"
Expand Down Expand Up @@ -62,15 +63,6 @@ constexpr auto get_enum_range() {

namespace internal {

template <class EnumType, EnumNameTransform _transform>
inline constexpr bool has_custom_enum_names_for_transform_v =
(_transform != EnumNameTransform::none);

template <class EnumType, class ProcessorsType>
inline constexpr bool has_custom_enum_names_v =
has_custom_enum_names_for_transform_v<
std::remove_cvref_t<EnumType>, enum_name_transform_v<ProcessorsType>>;

template <class EnumType, EnumNameTransform _transform>
std::vector<std::pair<std::string, std::remove_cvref_t<EnumType>>>
build_enum_wire_names_with_transform() {
Expand All @@ -79,7 +71,23 @@ build_enum_wire_names_with_transform() {
constexpr auto enumerators = get_enumerator_array<E>();
out.reserve(enumerators.size());
for (const auto& [raw_name, val] : enumerators) {
out.emplace_back(apply_enum_name_transform(raw_name, _transform), val);
out.emplace_back(
resolve_enum_wire_name_for_transform<E, _transform>(val, raw_name),
val);
}
if constexpr (has_custom_enum_names_for_transform_v<E, _transform>) {
for (const auto& [val, wire] : get_enum_overrides<E>()) {
bool already_present = false;
for (const auto& entry : out) {
if (entry.second == val) {
already_present = true;
break;
}
}
if (!already_present) {
out.emplace_back(std::string(wire), val);
}
}
}
return out;
}
Expand Down Expand Up @@ -272,15 +280,27 @@ Result<std::remove_cvref_t<EnumType>> string_to_enum_with_transform_noexcept(

} // namespace internal

// Converts an enum value to its wire string representation, honoring any
// `rfl::TransformEnumNames<...>` processor passed in `Ps...`.
// Converts an enum value to its wire string representation, honoring
// `rfl::EnumNames<EnumType>` and any `rfl::TransformEnumNames<...>` processor
// passed in `Ps...`.
template <class EnumType, class... Ps>
std::string enum_to_string(const EnumType _enum) {
return internal::enum_to_string_with_transform<
std::remove_cvref_t<EnumType>,
internal::enum_name_transform_v<Processors<Ps...>>>(_enum);
}

// Returns all declared enumerators (plus any explicit `rfl::EnumNames<E>`
// overrides for values outside the reflected range) paired with their resolved
// wire names.
template <class EnumType, class... Ps>
std::vector<std::pair<std::string, std::remove_cvref_t<EnumType>>>
get_enum_wire_names() {
return internal::get_cached_enum_wire_names_with_transform<
std::remove_cvref_t<EnumType>,
internal::enum_name_transform_v<Processors<Ps...>>>();
}

// Converts a string to a value of the given enum type.
//
// By default, numeric values are accepted in addition to the declared
Expand Down
Loading