array_context.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2014 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <bsoncxx/array/value.hpp>
  16. #include <bsoncxx/builder/concatenate.hpp>
  17. #include <bsoncxx/builder/core.hpp>
  18. #include <bsoncxx/builder/stream/closed_context.hpp>
  19. #include <bsoncxx/builder/stream/helpers.hpp>
  20. #include <bsoncxx/util/functor.hpp>
  21. #include <bsoncxx/config/prelude.hpp>
  22. namespace bsoncxx {
  23. BSONCXX_INLINE_NAMESPACE_BEGIN
  24. namespace builder {
  25. namespace stream {
  26. template <class T>
  27. class key_context;
  28. class single_context;
  29. ///
  30. /// A stream context which expects any number of values.
  31. ///
  32. /// The template argument can be used to hold additional information about
  33. /// containing documents or arrays. I.e. value_context<> implies that this array
  34. /// is a sub_array in a document, while array_context would indicated a sub_array
  35. /// in an array. These types can be nested, such that contextual parsing (for
  36. /// key/value pairs) and depth (to prevent an invalid array_close) are enforced
  37. /// by the type system.
  38. ///
  39. /// I.e.
  40. /// builder << array_context << array_context << ...;
  41. ///
  42. /// This builds a bson array with successively higher index keys
  43. ///
  44. template <class base = closed_context>
  45. class array_context {
  46. public:
  47. ///
  48. /// Create an array_context given a core builder
  49. ///
  50. /// @param core
  51. /// The core builder to orchestrate
  52. ///
  53. BSONCXX_INLINE array_context(core* core) : _core(core) {}
  54. ///
  55. /// << operator for accepting a real value and appending it to the core
  56. /// builder.
  57. ///
  58. /// @param t
  59. /// The value to append
  60. ///
  61. template <class T>
  62. BSONCXX_INLINE typename std::enable_if<
  63. !(util::is_functor<T, void(array_context<>)>::value ||
  64. util::is_functor<T, void(single_context)>::value ||
  65. std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value),
  66. array_context>::type&
  67. operator<<(T&& t) {
  68. _core->append(std::forward<T>(t));
  69. return *this;
  70. }
  71. ///
  72. /// << operator for accepting a callable of the form void(array_context) or
  73. /// void(single_context) and invoking it to perform 1 or more value appends
  74. /// to the core builder.
  75. ///
  76. /// @param func
  77. /// The callback to invoke
  78. ///
  79. template <typename Func>
  80. BSONCXX_INLINE typename std::enable_if<(util::is_functor<Func, void(array_context<>)>::value ||
  81. util::is_functor<Func, void(single_context)>::value),
  82. array_context>::type&
  83. operator<<(Func&& func) {
  84. func(*this);
  85. return *this;
  86. }
  87. ///
  88. /// << operator for finalizing the stream.
  89. ///
  90. /// This operation finishes all processing necessary to fully encode the
  91. /// bson bytes and returns an owning value.
  92. ///
  93. /// @param _
  94. /// A finalize_type token
  95. ///
  96. /// @return A value type which holds the complete bson document.
  97. ///
  98. template <typename T>
  99. BSONCXX_INLINE typename std::enable_if<
  100. std::is_same<base, closed_context>::value &&
  101. std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
  102. // TODO(MSVC): This should just be 'array::value', but
  103. // VS2015U1 can't resolve the name.
  104. bsoncxx::array::value>::type
  105. operator<<(T&&) {
  106. return _core->extract_array();
  107. }
  108. ///
  109. /// << operator for opening a new subdocument in the core builder.
  110. ///
  111. /// @param _
  112. /// An open_document_type token
  113. ///
  114. BSONCXX_INLINE key_context<array_context> operator<<(const open_document_type) {
  115. _core->open_document();
  116. return wrap_document();
  117. }
  118. ///
  119. /// << operator for concatenating another array.
  120. ///
  121. /// This operation concatenates all of the values from the passed document
  122. /// into the current stream. Keys are adjusted to match the existing array.
  123. ///
  124. /// @param array
  125. /// An array to concatenate
  126. ///
  127. BSONCXX_INLINE array_context operator<<(concatenate_array array) {
  128. _core->concatenate(array.view());
  129. return *this;
  130. }
  131. ///
  132. /// << operator for opening a new subarray in the core builder.
  133. ///
  134. /// @param _
  135. /// An open_document_type token
  136. ///
  137. BSONCXX_INLINE array_context<array_context> operator<<(const open_array_type) {
  138. _core->open_array();
  139. return wrap_array();
  140. }
  141. ///
  142. /// << operator for closing a subarray in the core builder.
  143. ///
  144. /// @param _
  145. /// A close_array_type token
  146. ///
  147. BSONCXX_INLINE base operator<<(const close_array_type) {
  148. _core->close_array();
  149. return unwrap();
  150. }
  151. ///
  152. /// Conversion operator which provides a rooted array context given any
  153. /// stream currently in a nested array_context.
  154. ///
  155. BSONCXX_INLINE operator array_context<>() {
  156. return array_context<>(_core);
  157. }
  158. ///
  159. /// Conversion operator for single_context.
  160. ///
  161. /// @relatesalso single_context
  162. ///
  163. BSONCXX_INLINE operator single_context();
  164. private:
  165. BSONCXX_INLINE base unwrap() {
  166. return base(_core);
  167. }
  168. BSONCXX_INLINE array_context<array_context> wrap_array() {
  169. return array_context<array_context>(_core);
  170. }
  171. BSONCXX_INLINE key_context<array_context> wrap_document() {
  172. return key_context<array_context>(_core);
  173. }
  174. core* _core;
  175. };
  176. } // namespace stream
  177. } // namespace builder
  178. BSONCXX_INLINE_NAMESPACE_END
  179. } // namespace bsoncxx
  180. #include <bsoncxx/config/postlude.hpp>