Generic.hh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_Generic_hh__
  19. #define avro_Generic_hh__
  20. #include <boost/utility.hpp>
  21. #include "Config.hh"
  22. #include "Decoder.hh"
  23. #include "Encoder.hh"
  24. #include "GenericDatum.hh"
  25. #include "Types.hh"
  26. namespace avro {
  27. /**
  28. * A utility class to read generic datum from decoders.
  29. */
  30. class AVRO_DECL GenericReader : boost::noncopyable {
  31. const ValidSchema schema_;
  32. const bool isResolving_;
  33. const DecoderPtr decoder_;
  34. static void read(GenericDatum &datum, Decoder &d, bool isResolving);
  35. public:
  36. /**
  37. * Constructs a reader for the given schema using the given decoder.
  38. */
  39. GenericReader(ValidSchema s, const DecoderPtr &decoder);
  40. /**
  41. * Constructs a reader for the given reader's schema \c readerSchema
  42. * using the given
  43. * decoder which holds data matching writer's schema \c writerSchema.
  44. */
  45. GenericReader(const ValidSchema &writerSchema,
  46. const ValidSchema &readerSchema, const DecoderPtr &decoder);
  47. /**
  48. * Reads a value off the decoder.
  49. */
  50. void read(GenericDatum &datum) const;
  51. /**
  52. * Drains any residual bytes in the input stream (e.g. because
  53. * reader's schema has no use of them) and return unused bytes
  54. * back to the underlying input stream.
  55. */
  56. void drain() {
  57. decoder_->drain();
  58. }
  59. /**
  60. * Reads a generic datum from the stream, using the given schema.
  61. */
  62. static void read(Decoder &d, GenericDatum &g);
  63. /**
  64. * Reads a generic datum from the stream, using the given schema.
  65. */
  66. static void read(Decoder &d, GenericDatum &g, const ValidSchema &s);
  67. };
  68. /**
  69. * A utility class to write generic datum to encoders.
  70. */
  71. class AVRO_DECL GenericWriter : boost::noncopyable {
  72. const ValidSchema schema_;
  73. const EncoderPtr encoder_;
  74. static void write(const GenericDatum &datum, Encoder &e);
  75. public:
  76. /**
  77. * Constructs a writer for the given schema using the given encoder.
  78. */
  79. GenericWriter(ValidSchema s, EncoderPtr encoder);
  80. /**
  81. * Writes a value onto the encoder.
  82. */
  83. void write(const GenericDatum &datum) const;
  84. /**
  85. * Writes a generic datum on to the stream.
  86. */
  87. static void write(Encoder &e, const GenericDatum &g);
  88. /**
  89. * Writes a generic datum on to the stream, using the given schema.
  90. * Retained for backward compatibility.
  91. */
  92. static void write(Encoder &e, const GenericDatum &g, const ValidSchema &) {
  93. write(e, g);
  94. }
  95. };
  96. template<typename T>
  97. struct codec_traits;
  98. /**
  99. * Specialization of codec_traits for Generic datum along with its schema.
  100. * This is maintained for compatibility with old code. Please use the
  101. * cleaner codec_traits<GenericDatum> instead.
  102. */
  103. template<>
  104. struct codec_traits<std::pair<ValidSchema, GenericDatum>> {
  105. /** Encodes */
  106. static void encode(Encoder &e,
  107. const std::pair<ValidSchema, GenericDatum> &p) {
  108. GenericWriter::write(e, p.second, p.first);
  109. }
  110. /** Decodes */
  111. static void decode(Decoder &d, std::pair<ValidSchema, GenericDatum> &p) {
  112. GenericReader::read(d, p.second, p.first);
  113. }
  114. };
  115. /**
  116. * Specialization of codec_traits for GenericDatum.
  117. */
  118. template<>
  119. struct codec_traits<GenericDatum> {
  120. /** Encodes */
  121. static void encode(Encoder &e, const GenericDatum &g) {
  122. GenericWriter::write(e, g);
  123. }
  124. /** Decodes */
  125. static void decode(Decoder &d, GenericDatum &g) {
  126. GenericReader::read(d, g);
  127. }
  128. };
  129. } // namespace avro
  130. #endif