Antkeeper  0.0.1
deserialize-context.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_RESOURCES_DESERIALIZE_CONTEXT_HPP
21 #define ANTKEEPER_RESOURCES_DESERIALIZE_CONTEXT_HPP
22 
23 #include <cstddef>
24 #include <bit>
25 #include <filesystem>
26 
31 {
32 public:
36  [[nodiscard]] virtual const std::filesystem::path& path() const noexcept = 0;
37 
41  [[nodiscard]] virtual bool error() const noexcept = 0;
42 
46  [[nodiscard]] virtual bool eof() const noexcept = 0;
47 
51  [[nodiscard]] virtual std::size_t size() const noexcept = 0;
52 
58  [[nodiscard]] virtual std::size_t tell() const = 0;
59 
67  virtual void seek(std::size_t offset) = 0;
68 
79  virtual std::size_t read8(std::byte* data, std::size_t count) noexcept(false) = 0;
80 
91  virtual std::size_t read16_le(std::byte* data, std::size_t count) noexcept(false) = 0;
92 
103  virtual std::size_t read16_be(std::byte* data, std::size_t count) noexcept(false) = 0;
104 
117  template <std::endian Endian>
118  inline std::size_t read16(std::byte* data, std::size_t count) noexcept(false)
119  {
120  if constexpr (Endian == std::endian::little)
121  {
122  return read16_le(data, count);
123  }
124  else
125  {
126  return read16_be(data, count);
127  }
128  }
129 
140  virtual std::size_t read32_le(std::byte* data, std::size_t count) noexcept(false) = 0;
141 
152  virtual std::size_t read32_be(std::byte* data, std::size_t count) noexcept(false) = 0;
153 
166  template <std::endian Endian>
167  inline std::size_t read32(std::byte* data, std::size_t count) noexcept(false)
168  {
169  if constexpr (Endian == std::endian::little)
170  {
171  return read32_le(data, count);
172  }
173  else
174  {
175  return read32_be(data, count);
176  }
177  }
178 
189  virtual std::size_t read64_le(std::byte* data, std::size_t count) noexcept(false) = 0;
190 
201  virtual std::size_t read64_be(std::byte* data, std::size_t count) noexcept(false) = 0;
202 
215  template <std::endian Endian>
216  inline std::size_t read64(std::byte* data, std::size_t count) noexcept(false)
217  {
218  if constexpr (Endian == std::endian::little)
219  {
220  return read64_le(data, count);
221  }
222  else
223  {
224  return read64_be(data, count);
225  }
226  }
227 };
228 
229 #endif // ANTKEEPER_RESOURCES_DESERIALIZE_CONTEXT_HPP
constexpr int count(T x) noexcept
Returns the number of set bits in a value, known as a population count or Hamming weight.
Definition: bit-math.hpp:211
T offset(T longitude)
Calculates the UTC offset at a given longitude.
Definition: utc.hpp:38
Provides access to a deserialization state.
virtual std::size_t read16_be(std::byte *data, std::size_t count) noexcept(false)=0
Reads 16-bit (word) big-endian data.
virtual bool eof() const noexcept=0
Returns true if the end of a file was reached.
virtual std::size_t read32_le(std::byte *data, std::size_t count) noexcept(false)=0
Reads 32-bit (double word) little-endian data.
virtual std::size_t read64_be(std::byte *data, std::size_t count) noexcept(false)=0
Reads 64-bit (quad word) big-endian data.
std::size_t read16(std::byte *data, std::size_t count) noexcept(false)
Reads 16-bit (word) data.
virtual std::size_t read16_le(std::byte *data, std::size_t count) noexcept(false)=0
Reads 16-bit (word) little-endian data.
std::size_t read32(std::byte *data, std::size_t count) noexcept(false)
Reads 32-bit (double word) data.
virtual std::size_t tell() const =0
Returns the offsets from the start of the file to the current position, in bytes.
virtual void seek(std::size_t offset)=0
Seeks to a position in the file.
virtual bool error() const noexcept=0
Returns true if an error occured during a read operation or initialization, false otherwise.
virtual const std::filesystem::path & path() const noexcept=0
Returns the path associated with this deserialize context.
virtual std::size_t size() const noexcept=0
Returns the size of the file, in bytes.
virtual std::size_t read8(std::byte *data, std::size_t count) noexcept(false)=0
Reads 8-bit (byte) data.
virtual std::size_t read64_le(std::byte *data, std::size_t count) noexcept(false)=0
Reads 64-bit (quad word) little-endian data.
std::size_t read64(std::byte *data, std::size_t count) noexcept(false)
Reads 64-bit (quad word) data.
virtual std::size_t read32_be(std::byte *data, std::size_t count) noexcept(false)=0
Reads 32-bit (double word) big-endian data.