summaryrefslogtreecommitdiffhomepage
path: root/serialize/binary-reader.hpp
blob: b95c37130781a48a6d1987fd21064b40b7cb9c8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include "binary-serializer.hpp"
#include <array>
#include <iterator>
#include <Corrade/Containers/StringView.h>

namespace floormat::Serialize {

template<typename T>
concept char_sequence = requires(T& x, const T& cx) {
    requires std::same_as<decltype(std::begin(x)), decltype(std::end(x))>;
    requires std::same_as<decltype(std::cbegin(cx)), decltype(std::cend(cx))>;
    requires std::forward_iterator<decltype(std::begin(x))>;
    requires std::forward_iterator<decltype(std::cbegin(cx))>;
    requires std::same_as<char, std::decay_t<decltype(*std::begin(x))>>;
    requires std::same_as<char, std::decay_t<decltype(*std::cbegin(x))>>;
};

template<typename It>
concept string_input_iterator = requires(It it) {
    requires std::forward_iterator<It>;
    requires std::is_same_v<char, std::decay_t<decltype(*it)>>;
};

template<string_input_iterator It>
struct binary_reader final {
    template<char_sequence Seq> explicit constexpr binary_reader(const Seq& seq) noexcept;
    constexpr binary_reader(It begin, It end) noexcept;
    constexpr void assert_end() noexcept;

    template<serializable T> constexpr T read() noexcept;
    template<std::size_t N> constexpr std::array<char, N> read() noexcept;
    constexpr std::size_t bytes_read() const noexcept { return num_bytes_read; }
    template<std::size_t Max> constexpr auto read_asciiz_string() noexcept;

private:
    std::size_t num_bytes_read = 0;
    It it, end;
};

template<string_input_iterator It, serializable T>
binary_reader<It>& operator>>(binary_reader<It>& reader, T& x) noexcept;

template<string_input_iterator It> binary_reader(It&& begin, It&& end) -> binary_reader<std::decay_t<It>>;

template<typename Array>
binary_reader(Array&& array) -> binary_reader<std::decay_t<decltype(std::begin(array))>>;

} // namespace floormat::Serialize