💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.2 KiB

  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. #ifndef ANTKEEPER_RESOURCES_SERIALIZE_CONTEXT_HPP
  20. #define ANTKEEPER_RESOURCES_SERIALIZE_CONTEXT_HPP
  21. #include <cstddef>
  22. #include <bit>
  23. #include <filesystem>
  24. /**
  25. * Provides access to a serialization state.
  26. */
  27. struct serialize_context
  28. {
  29. public:
  30. /**
  31. * Returns the path associated with this serialize context.
  32. */
  33. [[nodiscard]] virtual const std::filesystem::path& path() const noexcept = 0;
  34. /**
  35. * Returns `true` if an error occured during a write operation or initialization, `false` otherwise.
  36. */
  37. [[nodiscard]] virtual bool error() const noexcept = 0;
  38. /**
  39. * Writes 8-bit (byte) data.
  40. *
  41. * @param data Pointer to data source.
  42. * @param count Number of bytes to write.
  43. *
  44. * @return Number of bytes written.
  45. *
  46. * @throw serialize_error Write error.
  47. */
  48. virtual std::size_t write8(const std::byte* data, std::size_t count) noexcept(false) = 0;
  49. /**
  50. * Writes 16-bit (word) little-endian data.
  51. *
  52. * @param data Pointer to data source.
  53. * @param count Number of words to write.
  54. *
  55. * @return Number of words written.
  56. *
  57. * @throw serialize_error Write error.
  58. */
  59. virtual std::size_t write16_le(const std::byte* data, std::size_t count) noexcept(false) = 0;
  60. /**
  61. * Writes 16-bit (word) big-endian data.
  62. *
  63. * @param data Pointer to data source.
  64. * @param count Number of words to write.
  65. *
  66. * @return Number of words written.
  67. *
  68. * @throw serialize_error Write error.
  69. */
  70. virtual std::size_t write16_be(const std::byte* data, std::size_t count) noexcept(false) = 0;
  71. /**
  72. * Writes 16-bit (word) data.
  73. *
  74. * @tparam Endian Endianness of the write operation.
  75. *
  76. * @param data Pointer to data source.
  77. * @param count Number of words to write.
  78. *
  79. * @return Number of words written.
  80. *
  81. * @throw serialize_error Write error.
  82. */
  83. template <std::endian Endian>
  84. inline std::size_t write16(const std::byte* data, std::size_t count) noexcept(false)
  85. {
  86. if constexpr (Endian == std::endian::little)
  87. {
  88. return write16_le(data, count);
  89. }
  90. else
  91. {
  92. return write16_be(data, count);
  93. }
  94. }
  95. /**
  96. * Writes 32-bit (double word) little-endian data.
  97. *
  98. * @param data Pointer to data source.
  99. * @param count Number of double words to write.
  100. *
  101. * @return Number of double words written.
  102. *
  103. * @throw serialize_error Write error.
  104. */
  105. virtual std::size_t write32_le(const std::byte* data, std::size_t count) noexcept(false) = 0;
  106. /**
  107. * Writes 32-bit (double word) big-endian data.
  108. *
  109. * @param data Pointer to data source.
  110. * @param count Number of double words to write.
  111. *
  112. * @return Number of double words written.
  113. *
  114. * @throw serialize_error Write error.
  115. */
  116. virtual std::size_t write32_be(const std::byte* data, std::size_t count) noexcept(false) = 0;
  117. /**
  118. * Writes 32-bit (double word) data.
  119. *
  120. * @tparam Endian Endianness of the write operation.
  121. *
  122. * @param data Pointer to data source.
  123. * @param count Number of double words to write.
  124. *
  125. * @return Number of double words written.
  126. *
  127. * @throw serialize_error Write error.
  128. */
  129. template <std::endian Endian>
  130. inline std::size_t write32(const std::byte* data, std::size_t count) noexcept(false)
  131. {
  132. if constexpr (Endian == std::endian::little)
  133. {
  134. return write32_le(data, count);
  135. }
  136. else
  137. {
  138. return write32_be(data, count);
  139. }
  140. }
  141. /**
  142. * Writes 64-bit (quad word) little-endian data.
  143. *
  144. * @param data Pointer to data source.
  145. * @param count Number of quad words to write.
  146. *
  147. * @return Number of quad words written.
  148. *
  149. * @throw serialize_error Write error.
  150. */
  151. virtual std::size_t write64_le(const std::byte* data, std::size_t count) noexcept(false) = 0;
  152. /**
  153. * Writes 64-bit (quad word) big-endian data.
  154. *
  155. * @param data Pointer to data source.
  156. * @param count Number of quad words to write.
  157. *
  158. * @return Number of quad words written.
  159. *
  160. * @throw serialize_error Write error.
  161. */
  162. virtual std::size_t write64_be(const std::byte* data, std::size_t count) noexcept(false) = 0;
  163. /**
  164. * Writes 64-bit (quad word) data.
  165. *
  166. * @tparam Endian Endianness of the write operation.
  167. *
  168. * @param data Pointer to data source.
  169. * @param count Number of quad words to write.
  170. *
  171. * @return Number of quad words written.
  172. *
  173. * @throw serialize_error Write error.
  174. */
  175. template <std::endian Endian>
  176. inline std::size_t write64(const std::byte* data, std::size_t count) noexcept(false)
  177. {
  178. if constexpr (Endian == std::endian::little)
  179. {
  180. return write64_le(data, count);
  181. }
  182. else
  183. {
  184. return write64_be(data, count);
  185. }
  186. }
  187. };
  188. #endif // ANTKEEPER_RESOURCES_SERIALIZE_CONTEXT_HPP