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

120 lines
4.0 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. #include "resources/serializer.hpp"
  20. #include <string>
  21. template <>
  22. void serializer<bool>::serialize(const bool& value, serialize_context& ctx)
  23. {
  24. const std::uint8_t temp = (value) ? 1 : 0;
  25. ctx.write8(reinterpret_cast<const std::byte*>(&temp), 1);
  26. };
  27. template <>
  28. void serializer<std::uint8_t>::serialize(const std::uint8_t& value, serialize_context& ctx)
  29. {
  30. ctx.write8(reinterpret_cast<const std::byte*>(&value), 1);
  31. };
  32. template <>
  33. void serializer<std::uint16_t>::serialize(const std::uint16_t& value, serialize_context& ctx)
  34. {
  35. ctx.write16(reinterpret_cast<const std::byte*>(&value), 1);
  36. };
  37. template <>
  38. void serializer<std::uint32_t>::serialize(const std::uint32_t& value, serialize_context& ctx)
  39. {
  40. ctx.write32(reinterpret_cast<const std::byte*>(&value), 1);
  41. };
  42. template <>
  43. void serializer<std::uint64_t>::serialize(const std::uint64_t& value, serialize_context& ctx)
  44. {
  45. ctx.write64(reinterpret_cast<const std::byte*>(&value), 1);
  46. };
  47. template <>
  48. void serializer<std::int8_t>::serialize(const std::int8_t& value, serialize_context& ctx)
  49. {
  50. ctx.write8(reinterpret_cast<const std::byte*>(&value), 1);
  51. };
  52. template <>
  53. void serializer<std::int16_t>::serialize(const std::int16_t& value, serialize_context& ctx)
  54. {
  55. ctx.write16(reinterpret_cast<const std::byte*>(&value), 1);
  56. };
  57. template <>
  58. void serializer<std::int32_t>::serialize(const std::int32_t& value, serialize_context& ctx)
  59. {
  60. ctx.write32(reinterpret_cast<const std::byte*>(&value), 1);
  61. };
  62. template <>
  63. void serializer<std::int64_t>::serialize(const std::int64_t& value, serialize_context& ctx)
  64. {
  65. ctx.write64(reinterpret_cast<const std::byte*>(&value), 1);
  66. };
  67. template <>
  68. void serializer<float>::serialize(const float& value, serialize_context& ctx)
  69. {
  70. ctx.write32(reinterpret_cast<const std::byte*>(&value), 1);
  71. };
  72. template <>
  73. void serializer<double>::serialize(const double& value, serialize_context& ctx)
  74. {
  75. ctx.write64(reinterpret_cast<const std::byte*>(&value), 1);
  76. };
  77. template <>
  78. void serializer<std::string>::serialize(const std::string& value, serialize_context& ctx)
  79. {
  80. const std::uint64_t length = static_cast<std::uint64_t>(value.length());
  81. ctx.write64(reinterpret_cast<const std::byte*>(&length), 1);
  82. ctx.write8(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
  83. };
  84. template <>
  85. void serializer<std::u8string>::serialize(const std::u8string& value, serialize_context& ctx)
  86. {
  87. const std::uint64_t length = static_cast<std::uint64_t>(value.length());
  88. ctx.write64(reinterpret_cast<const std::byte*>(&length), 1);
  89. ctx.write8(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
  90. };
  91. template <>
  92. void serializer<std::u16string>::serialize(const std::u16string& value, serialize_context& ctx)
  93. {
  94. const std::uint64_t length = static_cast<std::uint64_t>(value.length());
  95. ctx.write64(reinterpret_cast<const std::byte*>(&length), 1);
  96. ctx.write16(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
  97. };
  98. template <>
  99. void serializer<std::u32string>::serialize(const std::u32string& value, serialize_context& ctx)
  100. {
  101. const std::uint64_t length = static_cast<std::uint64_t>(value.length());
  102. ctx.write64(reinterpret_cast<const std::byte*>(&length), 1);
  103. ctx.write32(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
  104. };