Fields.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. *
  4. * This file is part of Embedded Proto.
  5. *
  6. * Embedded Proto is open source software: you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation, version 3 of the license.
  9. *
  10. * Embedded Proto is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * For commercial and closed source application please visit:
  19. * <https://EmbeddedProto.com/license/>.
  20. *
  21. * Embedded AMS B.V.
  22. * Info:
  23. * info at EmbeddedProto dot com
  24. *
  25. * Postal address:
  26. * Johan Huizingalaan 763a
  27. * 1066 VH, Amsterdam
  28. * the Netherlands
  29. */
  30. #include "Fields.h"
  31. #include "MessageSizeCalculator.h"
  32. namespace EmbeddedProto
  33. {
  34. uint32_t Field::serialized_size() const
  35. {
  36. ::EmbeddedProto::MessageSizeCalculator calcBuffer;
  37. this->serialize(calcBuffer);
  38. return calcBuffer.get_size();
  39. }
  40. bool int32::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  41. {
  42. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  43. }
  44. bool int64::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  45. {
  46. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  47. }
  48. bool uint32::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  49. {
  50. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  51. }
  52. bool uint64::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  53. {
  54. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  55. }
  56. bool sint32::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  57. {
  58. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  59. }
  60. bool sint64::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  61. {
  62. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  63. }
  64. bool boolean::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  65. {
  66. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::VARINT), buffer) && serialize(buffer);
  67. }
  68. bool fixed32::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  69. {
  70. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::FIXED32), buffer) && serialize(buffer);
  71. }
  72. bool fixed64::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  73. {
  74. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::FIXED64), buffer) && serialize(buffer);
  75. }
  76. bool sfixed32::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  77. {
  78. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::FIXED32), buffer) && serialize(buffer);
  79. }
  80. bool sfixed64::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  81. {
  82. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::FIXED64), buffer) && serialize(buffer);
  83. }
  84. bool floatfixed::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  85. {
  86. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::FIXED32), buffer) && serialize(buffer);
  87. }
  88. bool doublefixed::serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const
  89. {
  90. return WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WireFormatter::WireType::FIXED64), buffer) && serialize(buffer);
  91. }
  92. bool int32::serialize(WriteBufferInterface& buffer) const
  93. {
  94. return WireFormatter::SerializeVarint(static_cast<uint32_t>(get()), buffer);
  95. }
  96. bool int64::serialize(WriteBufferInterface& buffer) const
  97. {
  98. return WireFormatter::SerializeVarint(static_cast<uint64_t>(get()), buffer);
  99. }
  100. bool uint32::serialize(WriteBufferInterface& buffer) const
  101. {
  102. return WireFormatter::SerializeVarint(get(), buffer);
  103. }
  104. bool uint64::serialize(WriteBufferInterface& buffer) const
  105. {
  106. return WireFormatter::SerializeVarint(get(), buffer);
  107. }
  108. bool sint32::serialize(WriteBufferInterface& buffer) const
  109. {
  110. return WireFormatter::SerializeVarint(WireFormatter::ZigZagEncode(get()), buffer);
  111. }
  112. bool sint64::serialize(WriteBufferInterface& buffer) const
  113. {
  114. return WireFormatter::SerializeVarint(WireFormatter::ZigZagEncode(get()), buffer);
  115. }
  116. bool boolean::serialize(WriteBufferInterface& buffer) const
  117. {
  118. return buffer.push(get() ? 0x01 : 0x00);
  119. }
  120. bool fixed32::serialize(WriteBufferInterface& buffer) const
  121. {
  122. return WireFormatter::SerialzieFixedNoTag(get(), buffer);
  123. }
  124. bool fixed64::serialize(WriteBufferInterface& buffer) const
  125. {
  126. return WireFormatter::SerialzieFixedNoTag(get(), buffer);
  127. }
  128. bool sfixed32::serialize(WriteBufferInterface& buffer) const
  129. {
  130. return WireFormatter::SerialzieSFixedNoTag(get(), buffer);
  131. }
  132. bool sfixed64::serialize(WriteBufferInterface& buffer) const
  133. {
  134. return WireFormatter::SerialzieSFixedNoTag(get(), buffer);
  135. }
  136. bool floatfixed::serialize(WriteBufferInterface& buffer) const
  137. {
  138. return WireFormatter::SerialzieFloatNoTag(get(), buffer);
  139. }
  140. bool doublefixed::serialize(WriteBufferInterface& buffer) const
  141. {
  142. return WireFormatter::SerialzieDoubleNoTag(get(), buffer);
  143. }
  144. bool int32::deserialize(ReadBufferInterface& buffer)
  145. {
  146. return WireFormatter::DeserializeInt(buffer, get());
  147. }
  148. bool int64::deserialize(ReadBufferInterface& buffer)
  149. {
  150. return WireFormatter::DeserializeInt(buffer, get());
  151. }
  152. bool uint32::deserialize(ReadBufferInterface& buffer)
  153. {
  154. return WireFormatter::DeserializeUInt(buffer, get());
  155. }
  156. bool uint64::deserialize(ReadBufferInterface& buffer)
  157. {
  158. return WireFormatter::DeserializeUInt(buffer, get());
  159. }
  160. bool sint32::deserialize(ReadBufferInterface& buffer)
  161. {
  162. return WireFormatter::DeserializeSInt(buffer, get());
  163. }
  164. bool sint64::deserialize(ReadBufferInterface& buffer)
  165. {
  166. return WireFormatter::DeserializeSInt(buffer, get());
  167. }
  168. bool boolean::deserialize(ReadBufferInterface& buffer)
  169. {
  170. return WireFormatter::DeserializeBool(buffer, get());
  171. }
  172. bool fixed32::deserialize(ReadBufferInterface& buffer)
  173. {
  174. return WireFormatter::DeserializeFixed(buffer, get());
  175. }
  176. bool fixed64::deserialize(ReadBufferInterface& buffer)
  177. {
  178. return WireFormatter::DeserializeFixed(buffer, get());
  179. }
  180. bool sfixed32::deserialize(ReadBufferInterface& buffer)
  181. {
  182. return WireFormatter::DeserializeSFixed(buffer, get());
  183. }
  184. bool sfixed64::deserialize(ReadBufferInterface& buffer)
  185. {
  186. return WireFormatter::DeserializeSFixed(buffer, get());
  187. }
  188. bool floatfixed::deserialize(ReadBufferInterface& buffer)
  189. {
  190. return WireFormatter::DeserializeFloat(buffer, get());
  191. }
  192. bool doublefixed::deserialize(ReadBufferInterface& buffer)
  193. {
  194. return WireFormatter::DeserializeDouble(buffer, get());
  195. }
  196. } // End of namespace EmbeddedProto