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

afxres / binary-cxx / 14244779547

03 Apr 2025 01:53PM UTC coverage: 99.345% (-0.3%) from 99.65%
14244779547

push

github

afxres
Add variant converter

38 of 40 new or added lines in 2 files covered. (95.0%)

607 of 611 relevant lines covered (99.35%)

84.59 hits per line

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

94.74
/code/binary/include/binary/converters/VariantConverter.hpp
1
#ifndef BINARY_CONVERTERS_VARIANTCONVERTER_HPP
2
#define BINARY_CONVERTERS_VARIANTCONVERTER_HPP
3

4
#include <memory>
5
#include <variant>
6

7
#include "binary/Converter.hpp"
8
#include "binary/ConverterExtensions.hpp"
9

10
namespace binary::converters {
11
template <typename T>
12
class VariantConverter;
13

14
template <template <typename...> typename T, typename... E>
15
    requires requires { std::variant_size<T<E...>>::value; }
16
class VariantConverter<T<E...>> : public Converter<T<E...>> {
17
private:
18
    using GenericArgument = T<E...>;
19
    using ElementSequence = std::make_index_sequence<sizeof...(E)>;
20

21
    template <size_t IsAuto, size_t Index>
22
    void EncodeInternal(Allocator& allocator, const GenericArgument& item) {
20✔
23
        if (Index == item.index()) {
20✔
24
            const auto& data = std::get<Index>(item);
8✔
25
            const auto& converter = std::get<Index>(this->converter);
8✔
26
            if constexpr (IsAuto == 0) {
27
                converter->Encode(allocator, data);
4✔
28
            } else {
29
                converter->EncodeAuto(allocator, data);
4✔
30
            }
31
        }
32
    }
20✔
33

34
    template <size_t IsAuto, size_t... Index>
35
    void EncodeInternal(Allocator& allocator, const GenericArgument& item, std::index_sequence<Index...>) {
8✔
36
        ::binary::Encode(allocator, item.index());
8✔
37
        (EncodeInternal<IsAuto, Index>(allocator, item), ...);
8✔
38
    }
8✔
39

40
    template <size_t IsAuto, size_t Index>
41
    void DecodeInternal(std::span<const std::byte>& span, GenericArgument& result, size_t index) {
20✔
42
        if (Index == index) {
20✔
43
            const auto& converter = std::get<Index>(this->converter);
8✔
44
            if constexpr (IsAuto == 0) {
45
                result = GenericArgument(std::in_place_index<Index>, converter->Decode(span));
4✔
46
            } else {
47
                result = GenericArgument(std::in_place_index<Index>, converter->DecodeAuto(span));
4✔
48
            }
49
        }
50
    }
20✔
51

52
    template <size_t IsAuto, size_t... Index>
53
    auto DecodeInternal(std::span<const std::byte>& span, std::index_sequence<Index...>) {
8✔
54
        auto index = ::binary::Decode(span);
8✔
55
        if (index > sizeof...(E)) {
8✔
NEW
56
            throw std::out_of_range("variant index out of range");
×
57
        }
58
        GenericArgument result;
8✔
59
        (DecodeInternal<IsAuto, Index>(span, result, index), ...);
8✔
60
        return result;
8✔
NEW
61
    }
×
62

63
    const std::tuple<std::shared_ptr<Converter<std::remove_cv_t<E>>>...> converter;
64

65
public:
66
    VariantConverter(const std::shared_ptr<Converter<std::remove_cv_t<E>>>&... converter)
8✔
67
        : converter({converter...}) {}
8✔
68

69
    virtual void Encode(Allocator& allocator, const GenericArgument& item) override {
4✔
70
        EncodeInternal<0>(allocator, item, ElementSequence());
4✔
71
    }
4✔
72

73
    virtual void EncodeAuto(Allocator& allocator, const GenericArgument& item) override {
4✔
74
        EncodeInternal<1>(allocator, item, ElementSequence());
4✔
75
    }
4✔
76

77
    virtual GenericArgument Decode(const std::span<const std::byte>& span) override {
4✔
78
        std::span<const std::byte> copy = span;
4✔
79
        return DecodeInternal<0>(copy, ElementSequence());
8✔
80
    }
81

82
    virtual GenericArgument DecodeAuto(std::span<const std::byte>& span) override {
4✔
83
        return DecodeInternal<1>(span, ElementSequence());
4✔
84
    }
85
};
86
}
87

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

© 2026 Coveralls, Inc