Serializer.hh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef avro_Serializer_hh__
  19. #define avro_Serializer_hh__
  20. #include <array>
  21. #include <boost/noncopyable.hpp>
  22. #include "Config.hh"
  23. #include "Writer.hh"
  24. namespace avro {
  25. /// Class that wraps a Writer or ValidatingWriter with an interface that uses
  26. /// explicit write* names instead of writeValue
  27. template<class Writer>
  28. class Serializer : private boost::noncopyable {
  29. public:
  30. /// Constructor only works with Writer
  31. explicit Serializer() : writer_() {}
  32. /// Constructor only works with ValidatingWriter
  33. explicit Serializer(const ValidSchema &schema) : writer_(schema) {}
  34. void writeNull() {
  35. writer_.writeValue(Null());
  36. }
  37. void writeBool(bool val) {
  38. writer_.writeValue(val);
  39. }
  40. void writeInt(int32_t val) {
  41. writer_.writeValue(val);
  42. }
  43. void writeLong(int64_t val) {
  44. writer_.writeValue(val);
  45. }
  46. void writeFloat(float val) {
  47. writer_.writeValue(val);
  48. }
  49. void writeDouble(double val) {
  50. writer_.writeValue(val);
  51. }
  52. void writeBytes(const void *val, size_t size) {
  53. writer_.writeBytes(val, size);
  54. }
  55. template<size_t N>
  56. void writeFixed(const uint8_t (&val)[N]) {
  57. writer_.writeFixed(val);
  58. }
  59. template<size_t N>
  60. void writeFixed(const std::array<uint8_t, N> &val) {
  61. writer_.writeFixed(val);
  62. }
  63. void writeString(const std::string &val) {
  64. writer_.writeValue(val);
  65. }
  66. void writeRecord() {
  67. writer_.writeRecord();
  68. }
  69. void writeRecordEnd() {
  70. writer_.writeRecordEnd();
  71. }
  72. void writeArrayBlock(int64_t size) {
  73. writer_.writeArrayBlock(size);
  74. }
  75. void writeArrayEnd() {
  76. writer_.writeArrayEnd();
  77. }
  78. void writeMapBlock(int64_t size) {
  79. writer_.writeMapBlock(size);
  80. }
  81. void writeMapEnd() {
  82. writer_.writeMapEnd();
  83. }
  84. void writeUnion(int64_t choice) {
  85. writer_.writeUnion(choice);
  86. }
  87. void writeEnum(int64_t choice) {
  88. writer_.writeEnum(choice);
  89. }
  90. InputBuffer buffer() const {
  91. return writer_.buffer();
  92. }
  93. private:
  94. Writer writer_;
  95. };
  96. } // namespace avro
  97. #endif