💿🐜 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.

152 lines
3.8 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_DESERIALIZE_CONTEXT_HPP
  20. #define ANTKEEPER_RESOURCES_DESERIALIZE_CONTEXT_HPP
  21. #include <cstddef>
  22. #include <bit>
  23. /**
  24. * Provides access to a deserialization state.
  25. */
  26. struct deserialize_context
  27. {
  28. public:
  29. /**
  30. * Reads 8-bit (byte) data.
  31. *
  32. * @param data Pointer to data source.
  33. * @param count Number of bytes to read.
  34. *
  35. * @return Number of bytes read.
  36. *
  37. * @throw deserialize_error Read error.
  38. */
  39. std::size_t read8(std::byte* data, std::size_t count) noexcept(false);
  40. /**
  41. * Reads 16-bit (word) data.
  42. *
  43. * @tparam Endian Endianness of the read operation.
  44. *
  45. * @param data Pointer to data destination.
  46. * @param count Number of words to read.
  47. *
  48. * @return Number of words read.
  49. *
  50. * @throw deserialize_error Read error.
  51. */
  52. template <std::endian Endian = std::endian::native>
  53. inline std::size_t read16(std::byte* data, std::size_t count) noexcept(false)
  54. {
  55. if constexpr (Endian == std::endian::little)
  56. {
  57. return read16_le(data, count);
  58. }
  59. else
  60. {
  61. return read16_be(data, count);
  62. }
  63. }
  64. /**
  65. * Reads 32-bit (double word) data.
  66. *
  67. * @tparam Endian Endianness of the read operation.
  68. *
  69. * @param data Pointer to data destination.
  70. * @param count Number of double words to read.
  71. *
  72. * @return Number of double words read.
  73. *
  74. * @throw deserialize_error Read error.
  75. */
  76. template <std::endian Endian = std::endian::native>
  77. inline std::size_t read32(std::byte* data, std::size_t count) noexcept(false)
  78. {
  79. if constexpr (Endian == std::endian::little)
  80. {
  81. return read32_le(data, count);
  82. }
  83. else
  84. {
  85. return read32_be(data, count);
  86. }
  87. }
  88. /**
  89. * Reads 64-bit (quad word) data.
  90. *
  91. * @tparam Endian Endianness of the read operation.
  92. *
  93. * @param data Pointer to data destination.
  94. * @param count Number of quad words to read.
  95. *
  96. * @return Number of quad words read.
  97. *
  98. * @throw deserialize_error Read error.
  99. */
  100. template <std::endian Endian = std::endian::native>
  101. inline std::size_t read64(std::byte* data, std::size_t count) noexcept(false)
  102. {
  103. if constexpr (Endian == std::endian::little)
  104. {
  105. return read64_le(data, count);
  106. }
  107. else
  108. {
  109. return read64_be(data, count);
  110. }
  111. }
  112. /**
  113. * Returns `true` if the end of a file was reached.
  114. */
  115. inline bool eof() const noexcept
  116. {
  117. return m_eof;
  118. }
  119. /**
  120. * Returns `true` if an error occured during a read operation, `false` otherwise.
  121. */
  122. inline bool error() const noexcept
  123. {
  124. return m_error;
  125. }
  126. private:
  127. template <class T>
  128. friend class resource_loader;
  129. deserialize_context(void* handle);
  130. std::size_t read16_le(std::byte* data, std::size_t count) noexcept(false);
  131. std::size_t read16_be(std::byte* data, std::size_t count) noexcept(false);
  132. std::size_t read32_le(std::byte* data, std::size_t count) noexcept(false);
  133. std::size_t read32_be(std::byte* data, std::size_t count) noexcept(false);
  134. std::size_t read64_le(std::byte* data, std::size_t count) noexcept(false);
  135. std::size_t read64_be(std::byte* data, std::size_t count) noexcept(false);
  136. void* handle;
  137. bool m_eof;
  138. bool m_error;
  139. };
  140. #endif // ANTKEEPER_RESOURCES_DESERIALIZE_CONTEXT_HPP