• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

afxres / binary-cxx / 40170184792

25 Oct 2026 06:44AM UTC coverage: 98.977% (-0.3%) from 99.318%
40170184792

push

github

afxres
New experimental named object converter

40 of 44 new or added lines in 1 file covered. (90.91%)

1064 of 1075 relevant lines covered (98.98%)

223.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/code/binary-experimental/include/binary/experimental/components/NamedObjectConverter.hpp
1
#ifndef BINARY_EXPERIMENTAL_COMPONENTS_NAMEDOBJECTCONVERTER_HPP
2
#define BINARY_EXPERIMENTAL_COMPONENTS_NAMEDOBJECTCONVERTER_HPP
3

4
#include <array>
5
#include <cstddef>
6
#include <cstdint>
7
#include <format>
8
#include <span>
9
#include <stdexcept>
10
#include <string_view>
11
#include <tuple>
12
#include <utility>
13

14
#include "binary/ConverterExtensions.hpp"
15
#include "binary/components/MemberDescriptor.hpp"
16
#include "binary/experimental/helpers/ConverterDecodeWithLengthPrefixMethodHelper.hpp"
17
#include "binary/experimental/helpers/ConverterEncodeWithLengthPrefixMethodHelper.hpp"
18
#include "binary/experimental/helpers/GetConverterHelper.hpp"
19
#include "binary/experimental/internal/Define.hpp"
20

21
namespace binary::experimental::components {
22
template <typename T, typename Schema>
23
struct NamedObjectConverter {
24
    using Members = std::remove_const_t<decltype(Schema::Members)>;
25
    static constexpr size_t MemberCount = std::tuple_size_v<Members>;
26

27
    template <size_t Index>
28
    using DescriptorType = std::tuple_element_t<Index, Members>;
29

30
    template <size_t Index>
31
    using AccessorType = std::remove_cvref_t<decltype(std::declval<DescriptorType<Index>&>().Access)>;
32

33
    template <size_t Index>
34
    using MemberType = std::remove_cv_t<typename ::binary::components::AccessorValue<T, AccessorType<Index>>::Type>;
35

36
    template <size_t Index>
37
    using ElementConverterType = typename ::binary::experimental::helpers::GetConverterHelper<MemberType<Index>>::Type;
38

39
    template <size_t... Index>
40
    static constexpr auto MakeNameTable(std::index_sequence<Index...>) {
41
        return std::array<std::string_view, sizeof...(Index)>{std::get<Index>(Schema::Members).Name...};
42
    }
43

44
    template <size_t... Index>
45
    static constexpr auto MakeOptionalTable(std::index_sequence<Index...>) {
46
        return std::array<bool, sizeof...(Index)>{std::get<Index>(Schema::Members).IsOptional...};
47
    }
48

49
    static constexpr size_t FindMember(const std::array<std::string_view, MemberCount>& names, std::string_view name) {
4 ✔
50
        for (size_t index = 0; index < MemberCount; index++) {
5 ✔
51
            if (names[index] == name) {
5 ✔
52
                return index;
4 ✔
53
            }
54
        }
NEW
55
        return SIZE_MAX;
×
56
    }
57

58
    template <size_t Index>
59
    static void EncodeMember(Allocator& allocator, const T& item) {
4 ✔
60
        const auto& descriptor = std::get<Index>(Schema::Members);
4 ✔
61
        const std::string_view name = descriptor.Name;
4 ✔
62
        ::binary::EncodeWithLengthPrefix(allocator, std::span<const std::byte>(reinterpret_cast<const std::byte*>(name.data()), name.size()));
4 ✔
63
        ::binary::experimental::helpers::ConverterEncodeWithLengthPrefixMethodHelper<ElementConverterType<Index>>::Invoke(allocator, descriptor.Access.Get(item));
4 ✔
64
    }
4 ✔
65

66
    template <size_t... Index>
67
    static void EncodeMembers(Allocator& allocator, const T& item, std::index_sequence<Index...>) {
2 ✔
68
        (EncodeMember<Index>(allocator, item), ...);
2 ✔
69
    }
2 ✔
70

71
    template <size_t Index>
72
    static void DecodeMember(T& item, std::span<const std::byte> span) {
3 ✔
73
        auto value = ElementConverterType<Index>::Decode(span);
3 ✔
74
        std::get<Index>(Schema::Members).Access.Set(item, std::move(value));
3 ✔
75
    }
3 ✔
76

77
    template <size_t... Index>
78
    static void DecodeMembers(T& item, const std::array<std::span<const std::byte>, MemberCount>& slices, std::index_sequence<Index...>) {
2 ✔
79
        ((slices[Index].data() != nullptr ? (DecodeMember<Index>(item, slices[Index]), void()) : void()), ...);
2 ✔
80
    }
2 ✔
81

82
    BINARY_EXPERIMENTAL_DEFINE_STATIC_LENGTH_METHOD() {
83
        return 0;
84
    }
85

86
    BINARY_EXPERIMENTAL_DEFINE_STATIC_ENCODE_METHOD(T) {
2 ✔
87
        EncodeMembers(allocator, item, std::make_index_sequence<MemberCount>());
2 ✔
88
    }
2 ✔
89

90
    BINARY_EXPERIMENTAL_DEFINE_STATIC_DECODE_METHOD(T) {
3 ✔
91
        static constexpr auto names = MakeNameTable(std::make_index_sequence<MemberCount>());
92
        static constexpr auto optionals = MakeOptionalTable(std::make_index_sequence<MemberCount>());
93
        std::array<std::span<const std::byte>, MemberCount> slices{};
3 ✔
94
        auto copy = span;
3 ✔
95
        while (!copy.empty()) {
7 ✔
96
            auto head = ::binary::DecodeWithLengthPrefix(copy);
4 ✔
97
            auto tail = ::binary::DecodeWithLengthPrefix(copy);
4 ✔
98
            const std::string_view name(reinterpret_cast<const char*>(head.data()), head.size());
4 ✔
99
            const auto index = FindMember(names, name);
4 ✔
100
            if (index == SIZE_MAX) {
4 ✔
NEW
101
                continue;
×
102
            }
103
            if (slices[index].data() != nullptr) {
4 ✔
NEW
104
                throw std::invalid_argument(std::format("named key '{}' already exists", name));
×
105
            }
106
            slices[index] = tail;
4 ✔
107
        }
108

109
        for (size_t index = 0; index < MemberCount; index++) {
9 ✔
110
            if (slices[index].data() == nullptr && !optionals[index]) {
7 ✔
111
                throw std::invalid_argument(std::format("named key '{}' does not exist", names[index]));
1 ✔
112
            }
113
        }
114

115
        T result{};
2 ✔
116
        DecodeMembers(result, slices, std::make_index_sequence<MemberCount>());
2 ✔
117
        return result;
4 ✔
NEW
118
    }
×
119
};
120
}
121

122
namespace binary::experimental::helpers {
123
template <typename T>
124
    requires requires { GetNamedObjectSchema(static_cast<std::remove_cv_t<T>*>(nullptr)); }
125
struct GetConverterHelper<T> {
126
    using Type = ::binary::experimental::components::NamedObjectConverter<T, std::remove_cvref_t<decltype(GetNamedObjectSchema(static_cast<std::remove_cv_t<T>*>(nullptr)))>>;
127
};
128
}
129

130
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc