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
30 changes: 6 additions & 24 deletions include/rfl/CamelCaseToSnakeCase.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef RFL_CAMELCASETOSNAKECASE_HPP_
#define RFL_CAMELCASETOSNAKECASE_HPP_

#include "Field.hpp"
#include "internal/is_rename.hpp"
#include <utility>

#include "internal/transform_case.hpp"

namespace rfl {
Expand All @@ -15,28 +15,10 @@ struct CamelCaseToSnakeCase {
/// @param _named_tuple The named tuple containing the struct's fields
/// @return A new named tuple with field names converted to snake_case
template <class StructType>
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([]<class FieldType>(const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
!internal::is_rename_v<typename FieldType::Type>) {
return handle_one_field(_f);
} else {
return _f;
}
});
}

private:
/// Applies the camelCase to snake_case transformation to a single field.
/// @tparam FieldType The type of the field being transformed
/// @param _f The field to transform
/// @return A new field with the transformed name and the same value
template <class FieldType>
static auto handle_one_field(const FieldType& _f) {
using NewFieldType =
Field<internal::transform_camel_case<FieldType::name_>(),
typename FieldType::Type>;
return NewFieldType(_f.value());
static auto process(auto&& _named_tuple) {
return internal::transform_named_tuple_case<
internal::CamelCaseToSnakeCaseTransform>(
std::forward<decltype(_named_tuple)>(_named_tuple));
}
};

Expand Down
22 changes: 22 additions & 0 deletions include/rfl/NamedTuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ namespace rfl {

namespace internal {

struct from_values_tuple_t {
explicit from_values_tuple_t() = default;
};

inline constexpr from_values_tuple_t from_values_tuple{};

/// Finds the position of rfl::ExtraFields in a field pack, returning -1 if none
/// is present or -2 if more than one is present.
template <class... FieldTypes>
Expand Down Expand Up @@ -109,6 +115,15 @@ class NamedTuple {
NamedTuple(const rfl::Tuple<FieldTypes...>& _tup)
: NamedTuple(rfl::make_from_tuple<NamedTuple<FieldTypes...>>(_tup)) {}

/// @brief Construct NamedTuple directly from its Values tuple (rvalue).
explicit NamedTuple(internal::from_values_tuple_t, Values&& _values) noexcept(
std::is_nothrow_move_constructible_v<Values>)
: values_(std::move(_values)) {}

/// @brief Construct NamedTuple directly from its Values tuple (const lvalue).
explicit NamedTuple(internal::from_values_tuple_t, const Values& _values)
: values_(_values) {}

/// @brief Copy constructor.
/// @param _other Another NamedTuple to copy from.
NamedTuple(const NamedTuple<FieldTypes...>& _other) = default;
Expand Down Expand Up @@ -722,6 +737,13 @@ class NamedTuple<> {
/// @brief Default constructor for empty NamedTuple.
NamedTuple(){};

/// @brief Construct empty NamedTuple directly from its Values tuple (rvalue).
explicit NamedTuple(internal::from_values_tuple_t, Values&&) noexcept {}

/// @brief Construct empty NamedTuple directly from its Values tuple (const
/// lvalue).
explicit NamedTuple(internal::from_values_tuple_t, const Values&) noexcept {}

/// @brief Destructor.
~NamedTuple() = default;

Expand Down
31 changes: 6 additions & 25 deletions include/rfl/SnakeCaseToCamelCase.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef RFL_SNAKECASETOCAMELCASE_HPP_
#define RFL_SNAKECASETOCAMELCASE_HPP_

#include "Field.hpp"
#include "internal/is_rename.hpp"
#include <utility>

#include "internal/transform_case.hpp"

namespace rfl {
Expand All @@ -15,29 +15,10 @@ struct SnakeCaseToCamelCase {
/// @param _named_tuple The named tuple containing the struct's fields
/// @return A new named tuple with field names converted to camelCase
template <class StructType>
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([]<class FieldType>(const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
!internal::is_rename_v<typename FieldType::Type>) {
return handle_one_field(_f);
} else {
return _f;
}
});
}

private:
/// Applies the snake_case to camelCase transformation to a single field.
/// @tparam FieldType The type of the field being transformed
/// @param _f The field to transform
/// @return A new field with the transformed name and the same value
template <class FieldType>
static auto handle_one_field(const FieldType& _f) {
using NewFieldType =
Field<internal::transform_snake_case<FieldType::name_,
/*capitalize=*/false>(),
typename FieldType::Type>;
return NewFieldType(_f.value());
static auto process(auto&& _named_tuple) {
return internal::transform_named_tuple_case<
internal::SnakeCaseToCamelCaseTransform>(
std::forward<decltype(_named_tuple)>(_named_tuple));
}
};

Expand Down
35 changes: 9 additions & 26 deletions include/rfl/SnakeCaseToKebabCase.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef RFL_SNAKECASETOKEBABCASE_HPP_
#define RFL_SNAKECASETOKEBABCASE_HPP_

#include "Field.hpp"
#include "internal/is_rename.hpp"
#include <utility>

#include "internal/transform_case.hpp"

namespace rfl {
Expand All @@ -15,30 +15,13 @@ struct SnakeCaseToKebabCase {
/// @param _named_tuple The named tuple containing the struct's fields
/// @return A new named tuple with field names converted to kebab-case
template <class StructType>
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([]<class FieldType>(const FieldType& _f) {
// "xml_content" is a reserved name used by the XML Reader/Writer;
// all case-transform processors must leave it unchanged.
if constexpr (FieldType::name() != "xml_content" &&
!internal::is_rename_v<typename FieldType::Type>) {
return handle_one_field(_f);
} else {
return _f;
}
});
}

private:
/// Applies the snake_case to kebab-case transformation to a single field.
/// @tparam FieldType The type of the field being transformed
/// @param _f The field to transform
/// @return A new field with the transformed name and the same value
template <class FieldType>
static auto handle_one_field(const FieldType& _f) {
using NewFieldType =
Field<internal::transform_snake_to_kebab<FieldType::name_>(),
typename FieldType::Type>;
return NewFieldType(_f.value());
static auto process(auto&& _named_tuple) {
// "xml_content" is a reserved name used by the XML Reader/Writer;
// all case-transform processors leave it unchanged via
// transform_case_field.
return internal::transform_named_tuple_case<
internal::SnakeCaseToKebabCaseTransform>(
std::forward<decltype(_named_tuple)>(_named_tuple));
}
};

Expand Down
32 changes: 6 additions & 26 deletions include/rfl/SnakeCaseToPascalCase.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef RFL_SNAKECASETOPASCALCASE_HPP_
#define RFL_SNAKECASETOPASCALCASE_HPP_

#include "Field.hpp"
#include "internal/is_rename.hpp"
#include <utility>

#include "internal/transform_case.hpp"

namespace rfl {
Expand All @@ -15,30 +15,10 @@ struct SnakeCaseToPascalCase {
/// @param _named_tuple The named tuple containing the struct's fields
/// @return A new named tuple with field names converted to PascalCase
template <class StructType>
static auto process(const auto& _named_tuple) {
const auto handle_one = []<class FieldType>(const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
!internal::is_rename_v<typename FieldType::Type>) {
return handle_one_field(_f);
} else {
return _f;
}
};
return _named_tuple.transform(handle_one);
}

private:
/// Applies the snake_case to PascalCase transformation to a single field.
/// @tparam FieldType The type of the field being transformed
/// @param _f The field to transform
/// @return A new field with the transformed name and the same value
template <class FieldType>
static auto handle_one_field(const FieldType& _f) {
using NewFieldType =
Field<internal::transform_snake_case<FieldType::name_,
/*capitalize=*/true>(),
typename FieldType::Type>;
return NewFieldType(_f.value());
static auto process(auto&& _named_tuple) {
return internal::transform_named_tuple_case<
internal::SnakeCaseToPascalCaseTransform>(
std::forward<decltype(_named_tuple)>(_named_tuple));
}
};

Expand Down
28 changes: 5 additions & 23 deletions include/rfl/ToAllCaps.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef RFL_TOALLCAPS_HPP_
#define RFL_TOALLCAPS_HPP_

#include "Field.hpp"
#include "internal/is_rename.hpp"
#include <utility>

#include "internal/transform_case.hpp"

namespace rfl {
Expand All @@ -15,27 +15,9 @@ struct ToAllCaps {
/// @param _named_tuple The named tuple containing the struct's fields
/// @return A new named tuple with field names converted to all uppercase
template <class StructType>
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([]<class FieldType>(const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
!internal::is_rename_v<typename FieldType::Type>) {
return handle_one_field(_f);
} else {
return _f;
}
});
}

private:
/// Applies the all-uppercase transformation to a single field.
/// @tparam FieldType The type of the field being transformed
/// @param _f The field to transform
/// @return A new field with the transformed name and the same value
template <class FieldType>
static auto handle_one_field(const FieldType& _f) {
using NewFieldType = Field<internal::to_all_caps<FieldType::name_>(),
typename FieldType::Type>;
return NewFieldType(_f.value());
static auto process(auto&& _named_tuple) {
return internal::transform_named_tuple_case<internal::ToAllCapsTransform>(
std::forward<decltype(_named_tuple)>(_named_tuple));
}
};

Expand Down
83 changes: 83 additions & 0 deletions include/rfl/internal/transform_case.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
#define RFL_INTERNAL_TRANSFORMSNAKECASE_HPP_

#include <algorithm>
#include <array>
#include <cstddef>
#include <ranges>
#include <type_traits>
#include <utility>

#include "../Field.hpp"
#include "../NamedTuple.hpp"
#include "StringLiteral.hpp"
#include "is_rename.hpp"

namespace rfl::internal {

Expand Down Expand Up @@ -80,6 +87,82 @@ consteval auto transform_snake_to_kebab() {
return StringLiteral<src.size() + 1>(result);
}

struct SnakeCaseToCamelCaseTransform {
template <StringLiteral _name>
static consteval auto apply() {
return transform_snake_case<_name, /*_capitalize=*/false>();
}
};

struct SnakeCaseToPascalCaseTransform {
template <StringLiteral _name>
static consteval auto apply() {
return transform_snake_case<_name, /*_capitalize=*/true>();
}
};

struct SnakeCaseToKebabCaseTransform {
template <StringLiteral _name>
static consteval auto apply() {
return transform_snake_to_kebab<_name>();
}
};

struct CamelCaseToSnakeCaseTransform {
template <StringLiteral _name>
static consteval auto apply() {
return transform_camel_case<_name>();
}
};

struct ToAllCapsTransform {
template <StringLiteral _name>
static consteval auto apply() {
return to_all_caps<_name>();
}
};

template <class NameTransform, class FieldType,
bool ShouldTransform =
(std::remove_cvref_t<FieldType>::name() != "xml_content" &&
!is_rename_v<typename std::remove_cvref_t<FieldType>::Type>)>
struct transform_case_field {
using type = std::remove_cvref_t<FieldType>;
};

template <class NameTransform, class FieldType>
struct transform_case_field<NameTransform, FieldType, true> {
using CleanField = std::remove_cvref_t<FieldType>;
using type = Field<NameTransform::template apply<CleanField::name_>(),
typename CleanField::Type>;
};

/// Transforms all eligible field names of a NamedTuple in-place by moving its
/// underlying values tuple directly without intermediate field-tuple copies.
template <class NameTransform, class... FieldTypes>
auto transform_named_tuple_case(NamedTuple<FieldTypes...>&& _named_tuple) {
if constexpr (sizeof...(FieldTypes) == 0) {
return NamedTuple<>();
} else {
using NewNamedTuple = NamedTuple<
typename transform_case_field<NameTransform, FieldTypes>::type...>;
return NewNamedTuple(from_values_tuple, std::move(_named_tuple.values()));
}
}

/// Transforms all eligible field names of a NamedTuple by copying its
/// underlying values tuple directly without intermediate field-tuple copies.
template <class NameTransform, class... FieldTypes>
auto transform_named_tuple_case(const NamedTuple<FieldTypes...>& _named_tuple) {
if constexpr (sizeof...(FieldTypes) == 0) {
return NamedTuple<>();
} else {
using NewNamedTuple = NamedTuple<
typename transform_case_field<NameTransform, FieldTypes>::type...>;
return NewNamedTuple(from_values_tuple, _named_tuple.values());
}
}

} // namespace rfl::internal

#endif
Loading