BufferDetailIterator.hh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_BufferDetailIterator_hh__
  19. #define avro_BufferDetailIterator_hh__
  20. #include "BufferDetail.hh"
  21. /**
  22. * \file BufferDetailIterator.hh
  23. *
  24. * \brief The implementation details for the Buffer iterators.
  25. **/
  26. namespace avro {
  27. namespace detail {
  28. /**
  29. * \brief Implements conversion from a chunk to asio::const_buffer
  30. *
  31. * Iterators for an InputBuffer will iterate over the avro of chunks, so
  32. * internally they contain an iterator. But the iterator needs to be
  33. * convertable to an asio buffer for use in boost::asio functions. This class
  34. * wraps the iterator with a cast operator to do this conversion.
  35. **/
  36. struct InputIteratorHelper {
  37. /// Construct a helper with an unnassigned iterator.
  38. InputIteratorHelper() : iter_() {}
  39. /// Construct a helper with an iterator.
  40. explicit InputIteratorHelper(const BufferImpl::ChunkList::const_iterator &iter) : iter_(iter) {}
  41. /// The location of valid data in this chunk.
  42. const data_type *data() const {
  43. return iter_->tellReadPos();
  44. }
  45. /// The size of valid data in this chunk.
  46. size_type size() const {
  47. return iter_->dataSize();
  48. }
  49. /// Conversion operator. It doesn't check for null, because the only
  50. /// the only time the chunk should be null is when it's the iterator
  51. /// end(), which should never be dereferenced anyway.
  52. #ifdef HAVE_BOOST_ASIO
  53. operator ConstAsioBuffer() const {
  54. return ConstAsioBuffer(data(), size());
  55. }
  56. #endif
  57. BufferImpl::ChunkList::const_iterator iter_; ///< the current iterator
  58. };
  59. /**
  60. * \brief Implements conversion from a chunk to asio::buffer
  61. *
  62. * Iterators for an OutputBuffer will iterate over the avro of chunks, so
  63. * internally they contain an iterator. But the iterator needs to be
  64. * convertable to an asio buffer for use in boost::asio functions. This class
  65. * wraps the iterator with a cast operator to do this conversion.
  66. */
  67. struct OutputIteratorHelper {
  68. /// Construct a helper with an unnassigned iterator.
  69. OutputIteratorHelper() : iter_() {}
  70. /// Construct a helper with an iterator.
  71. explicit OutputIteratorHelper(const BufferImpl::ChunkList::const_iterator &iter) : iter_(iter) {}
  72. /// The location of the first writable byte in this chunk.
  73. data_type *data() const {
  74. return iter_->tellWritePos();
  75. }
  76. /// The size of area that can be written in this chunk.
  77. size_type size() const {
  78. return iter_->freeSize();
  79. }
  80. /// Conversion operator. It doesn't check for null, because the only
  81. /// the only time the chunk should be null is when it's the iterator
  82. /// end(), which should never be dereferenced anyway.
  83. #ifdef HAVE_BOOST_ASIO
  84. operator MutableAsioBuffer() const {
  85. return MutableAsioBuffer(data(), size());
  86. }
  87. #endif
  88. BufferImpl::ChunkList::const_iterator iter_; ///< the current iterator
  89. };
  90. /**
  91. * \brief Implements the iterator for Buffer, that iterates through the
  92. * buffer's chunks.
  93. **/
  94. template<typename Helper>
  95. class BufferIterator {
  96. public:
  97. typedef BufferIterator<Helper> this_type;
  98. /**
  99. * @name Typedefs
  100. *
  101. * STL iterators define the following declarations. According to
  102. * boost::asio documentation, the library expects the iterator to be
  103. * bidirectional, however this implements only the forward iterator type.
  104. * So far this has not created any problems with asio, but may change if
  105. * future versions of the asio require it.
  106. **/
  107. //@{
  108. typedef std::forward_iterator_tag iterator_category; // this is a lie to appease asio
  109. typedef Helper value_type;
  110. typedef std::ptrdiff_t difference_type;
  111. typedef value_type *pointer;
  112. typedef value_type &reference;
  113. //@}
  114. /// Construct an unitialized iterator.
  115. BufferIterator() : helper_() {}
  116. /* The default implementations are good here
  117. /// Copy constructor.
  118. BufferIterator(const BufferIterator &src) :
  119. helper_(src.helper_)
  120. { }
  121. /// Assignment.
  122. this_type& operator= (const this_type &rhs) {
  123. helper_ = rhs.helper_;
  124. return *this;
  125. }
  126. */
  127. /// Construct iterator at the position in the buffer's chunk list.
  128. explicit BufferIterator(BufferImpl::ChunkList::const_iterator iter) : helper_(iter) {}
  129. /// Dereference iterator, returns InputIteratorHelper or OutputIteratorHelper wrapper.
  130. reference operator*() {
  131. return helper_;
  132. }
  133. /// Dereference iterator, returns const InputIteratorHelper or OutputIteratorHelper wrapper.
  134. const value_type &operator*() const {
  135. return helper_;
  136. }
  137. /// Dereference iterator, returns InputIteratorHelper or OutputIteratorHelper wrapper.
  138. pointer operator->() {
  139. return &helper_;
  140. }
  141. /// Dereference iterator, returns const InputIteratorHelper or OutputIteratorHelper wrapper.
  142. const value_type *operator->() const {
  143. return &helper_;
  144. }
  145. /// Increment to next chunk in list, or to end() iterator.
  146. this_type &operator++() {
  147. ++helper_.iter_;
  148. return *this;
  149. }
  150. /// Increment to next chunk in list, or to end() iterator.
  151. this_type operator++(int) {
  152. this_type ret = *this;
  153. ++helper_.iter_;
  154. return ret;
  155. }
  156. /// True if iterators point to same chunks.
  157. bool operator==(const this_type &rhs) const {
  158. return (helper_.iter_ == rhs.helper_.iter_);
  159. }
  160. /// True if iterators point to different chunks.
  161. bool operator!=(const this_type &rhs) const {
  162. return (helper_.iter_ != rhs.helper_.iter_);
  163. }
  164. private:
  165. Helper helper_;
  166. };
  167. typedef BufferIterator<InputIteratorHelper> InputBufferIterator;
  168. typedef BufferIterator<OutputIteratorHelper> OutputBufferIterator;
  169. } // namespace detail
  170. } // namespace avro
  171. #endif