Antkeeper  0.0.1
serialize-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_SERIALIZE_CONTEXT_HPP
21 #define ANTKEEPER_RESOURCES_SERIALIZE_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 
53  virtual std::size_t write8(const std::byte* data, std::size_t count) noexcept(false) = 0;
54 
65  virtual std::size_t write16_le(const std::byte* data, std::size_t count) noexcept(false) = 0;
66 
77  virtual std::size_t write16_be(const std::byte* data, std::size_t count) noexcept(false) = 0;
78 
91  template <std::endian Endian>
92  inline std::size_t write16(const std::byte* data, std::size_t count) noexcept(false)
93  {
94  if constexpr (Endian == std::endian::little)
95  {
96  return write16_le(data, count);
97  }
98  else
99  {
100  return write16_be(data, count);
101  }
102  }
103 
114  virtual std::size_t write32_le(const std::byte* data, std::size_t count) noexcept(false) = 0;
115 
126  virtual std::size_t write32_be(const std::byte* data, std::size_t count) noexcept(false) = 0;
127 
140  template <std::endian Endian>
141  inline std::size_t write32(const std::byte* data, std::size_t count) noexcept(false)
142  {
143  if constexpr (Endian == std::endian::little)
144  {
145  return write32_le(data, count);
146  }
147  else
148  {
149  return write32_be(data, count);
150  }
151  }
152 
163  virtual std::size_t write64_le(const std::byte* data, std::size_t count) noexcept(false) = 0;
164 
175  virtual std::size_t write64_be(const std::byte* data, std::size_t count) noexcept(false) = 0;
176 
189  template <std::endian Endian>
190  inline std::size_t write64(const std::byte* data, std::size_t count) noexcept(false)
191  {
192  if constexpr (Endian == std::endian::little)
193  {
194  return write64_le(data, count);
195  }
196  else
197  {
198  return write64_be(data, count);
199  }
200  }
201 };
202 
203 #endif // ANTKEEPER_RESOURCES_SERIALIZE_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
Provides access to a serialization state.
virtual std::size_t write32_le(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 32-bit (double word) little-endian data.
std::size_t write16(const std::byte *data, std::size_t count) noexcept(false)
Writes 16-bit (word) data.
virtual std::size_t write64_le(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 64-bit (quad word) little-endian data.
virtual std::size_t write64_be(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 64-bit (quad word) big-endian data.
virtual std::size_t write16_be(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 16-bit (word) big-endian data.
virtual std::size_t write8(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 8-bit (byte) data.
std::size_t write32(const std::byte *data, std::size_t count) noexcept(false)
Writes 32-bit (double word) data.
virtual std::size_t write32_be(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 32-bit (double word) big-endian data.
virtual std::size_t write16_le(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 16-bit (word) little-endian data.
virtual const std::filesystem::path & path() const noexcept=0
Returns the path associated with this serialize context.
std::size_t write64(const std::byte *data, std::size_t count) noexcept(false)
Writes 64-bit (quad word) data.
virtual bool error() const noexcept=0
Returns true if an error occured during a write operation or initialization, false otherwise.