File: binaryreader.hpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (83 lines) | stat: -rw-r--r-- 2,487 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef OPENMW_COMPONENTS_SERIALIZATION_BINARYREADER_H
#define OPENMW_COMPONENTS_SERIALIZATION_BINARYREADER_H

#include <components/misc/endianness.hpp>

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <stdexcept>
#include <type_traits>

namespace Serialization
{
    struct NotEnoughData : std::runtime_error
    {
        NotEnoughData()
            : std::runtime_error("Not enough data")
        {
        }
    };

    class BinaryReader
    {
    public:
        explicit BinaryReader(const std::byte* pos, const std::byte* end)
            : mPos(pos)
            , mEnd(end)
        {
            assert(mPos <= mEnd);
        }

        BinaryReader(const BinaryReader&) = delete;

        template <class Format, class T>
        void operator()(Format&& format, T& value)
        {
            if constexpr (std::is_enum_v<T>)
                (*this)(std::forward<Format>(format), static_cast<std::underlying_type_t<T>&>(value));
            else if constexpr (std::is_arithmetic_v<T>)
            {
                if (mEnd - mPos < static_cast<std::ptrdiff_t>(sizeof(T)))
                    throw NotEnoughData();
                std::memcpy(&value, mPos, sizeof(T));
                mPos += sizeof(T);
                value = Misc::toLittleEndian(value);
            }
            else if constexpr (std::is_pointer_v<T>)
                value = reinterpret_cast<T>(mPos);
            else
            {
                format(*this, value);
            }
        }

        template <class Format, class T>
        auto operator()(Format&& format, T* data, std::size_t count)
        {
            if constexpr (std::is_enum_v<T>)
                (*this)(std::forward<Format>(format), reinterpret_cast<std::underlying_type_t<T>*>(data), count);
            else if constexpr (std::is_arithmetic_v<T>)
            {
                const std::size_t size = sizeof(T) * count;
                if (mEnd - mPos < static_cast<std::ptrdiff_t>(size))
                    throw NotEnoughData();
                std::memcpy(data, mPos, size);
                mPos += size;
                if constexpr (!Misc::IS_LITTLE_ENDIAN)
                    std::for_each(data, data + count, [&](T& v) { v = Misc::fromLittleEndian(v); });
            }
            else
            {
                format(*this, data, count);
            }
        }

    private:
        const std::byte* mPos;
        const std::byte* const mEnd;
    };
}

#endif