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
34 changes: 34 additions & 0 deletions include/rfl/internal/calc_flattened_size.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef RFL_INTERNAL_CALC_FLATTENED_SIZE_HPP_
#define RFL_INTERNAL_CALC_FLATTENED_SIZE_HPP_

#include <cstddef>
#include <type_traits>
#include <utility>

#include "../Tuple.hpp"
#include "is_flatten_field.hpp"
#include "ptr_tuple_t.hpp"

namespace rfl::internal {

/// Calculates the flattened field count of a type (recursively for
/// rfl::Flatten).
template <class T>
consteval std::size_t calc_flattened_size() {
using U = std::remove_cvref_t<std::remove_pointer_t<T>>;
if constexpr (is_flatten_field_v<U>) {
using Tuple = ptr_tuple_t<
std::remove_cvref_t<std::remove_pointer_t<typename U::Type>>>;
return []<std::size_t... _is>(std::index_sequence<_is...>) {
return (std::size_t{0} + ... +
calc_flattened_size<
tuple_element_t<static_cast<int>(_is), Tuple>>());
}(std::make_index_sequence<rfl::tuple_size_v<Tuple>>{});
} else {
return 1;
}
}

} // namespace rfl::internal

#endif
98 changes: 78 additions & 20 deletions include/rfl/internal/concat_literals.hpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,102 @@
#ifndef RFL_INTERNAL_CONCATLITERALS_HPP_
#define RFL_INTERNAL_CONCATLITERALS_HPP_

#include <array>
#include <cstddef>
#include <utility>

#include "../Literal.hpp"
#include "StringLiteral.hpp"

namespace rfl::internal {

// We don't want the operator+ to apply to normal literals,
// so we introduce this wrapper.
template <StringLiteral... _names>
struct LiteralWrapper {
Literal<_names...> literal_;
struct ConcatLiteralIndex {
std::size_t outer;
std::size_t inner;
};

template <StringLiteral... _names>
consteval auto wrap_literal(const Literal<_names...>& _literal) {
return LiteralWrapper<_names...>{_literal};
template <std::size_t... _sizes>
consteval auto calc_concat_literal_indices() {
constexpr std::size_t total = (std::size_t{0} + ... + _sizes);
std::array<ConcatLiteralIndex, total> indices{};
constexpr std::array<std::size_t, sizeof...(_sizes)> sizes{_sizes...};
std::size_t pos = 0;
for (std::size_t outer = 0; outer < sizeof...(_sizes); ++outer) {
for (std::size_t inner = 0; inner < sizes[outer]; ++inner) {
indices[pos++] = ConcatLiteralIndex{outer, inner};
}
}
return indices;
}

template <StringLiteral... _names1, StringLiteral... _names2>
consteval auto operator+(const LiteralWrapper<_names1...>&,
const LiteralWrapper<_names2...>&) {
return LiteralWrapper<_names1..., _names2...>{
rfl::Literal<_names1..., _names2...>()};
template <std::size_t _i, class T>
struct IndexedLiteralType {};

template <std::size_t _i, class T>
T get_indexed_literal_type(IndexedLiteralType<_i, T>);

template <class Seq, class... Ts>
struct LiteralTypeTable;

template <std::size_t... _is, class... Ts>
struct LiteralTypeTable<std::index_sequence<_is...>, Ts...>
: IndexedLiteralType<_is, Ts>... {};

template <class LiteralType>
struct LiteralToTable;

template <StringLiteral... _names>
struct LiteralToTable<rfl::Literal<_names...>> {
using Table =
LiteralTypeTable<std::make_index_sequence<sizeof...(_names)>,
rfl::LiteralHelper<_names>...>;
};

template <class OuterTable, std::size_t... _sizes, std::size_t... _ks>
consteval auto concat_literals_impl(std::index_sequence<_sizes...>,
std::index_sequence<_ks...>) {
constexpr auto indices = calc_concat_literal_indices<_sizes...>();
return rfl::Literal<decltype(rfl::internal::get_indexed_literal_type<
indices[_ks].inner>(
rfl::internal::get_indexed_literal_type<indices[_ks].outer>(
std::declval<OuterTable>())))::name_...>();
}

template <class Head, class... Tail>
consteval auto concat_literals(const Head& _head, const Tail&... _tail) {
return (wrap_literal(_head) + ... + wrap_literal(_tail)).literal_;
// Special case - no literals.
consteval inline auto concat_literals() { return rfl::Literal<>(); }

// Special case - single literal.
template <StringLiteral... _names>
consteval auto concat_literals(const rfl::Literal<_names...>&) {
return rfl::Literal<_names...>();
}

// Special case - every literal contains exactly one value.
template <StringLiteral _head, StringLiteral... _tail>
template <StringLiteral _head, StringLiteral _second, StringLiteral... _tail>
consteval auto concat_literals(const rfl::Literal<_head>&,
const rfl::Literal<_second>&,
const rfl::Literal<_tail>&...) {
return rfl::Literal<_head, _tail...>();
return rfl::Literal<_head, _second, _tail...>();
}

// Special case - no literals.
consteval inline auto concat_literals() { return rfl::Literal<>(); }
// General case - arbitrary mix of empty, single, and multi-value literals.
template <class Head, class Second, class... Tail>
consteval auto concat_literals(const Head&, const Second&, const Tail&...) {
constexpr std::size_t total =
Head::size() + Second::size() + (std::size_t{0} + ... + Tail::size());
if constexpr (total == 0) {
return rfl::Literal<>();
} else {
using OuterTable = LiteralTypeTable<
std::make_index_sequence<2 + sizeof...(Tail)>,
typename LiteralToTable<Head>::Table,
typename LiteralToTable<Second>::Table,
typename LiteralToTable<Tail>::Table...>;
return concat_literals_impl<OuterTable>(
std::index_sequence<Head::size(), Second::size(), Tail::size()...>{},
std::make_index_sequence<total>{});
}
}

} // namespace rfl::internal

Expand Down
Loading
Loading