key_context.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/builder/core.hpp>
  16. #include <bsoncxx/builder/stream/closed_context.hpp>
  17. #include <bsoncxx/builder/stream/value_context.hpp>
  18. #include <bsoncxx/stdx/string_view.hpp>
  19. #include <bsoncxx/util/functor.hpp>
  20. #include <bsoncxx/config/prelude.hpp>
  21. namespace bsoncxx {
  22. BSONCXX_INLINE_NAMESPACE_BEGIN
  23. namespace builder {
  24. namespace stream {
  25. ///
  26. /// A stream context which expects a key, which can later be followed by
  27. /// value, then more key/value pairs.
  28. ///
  29. /// The template argument can be used to hold additional information about
  30. /// containing documents or arrays. I.e. value_context<> implies that this
  31. /// document is a sub_document in a document, while array_context would
  32. /// indicated a sub_document in an array. These types can be nested, such that
  33. /// contextual parsing (for key/value pairs) and depth (to prevent an invalid
  34. /// document_close) are enforced by the type system.
  35. ///
  36. /// When in document context, the first parameter will be in key_context, then
  37. /// in value_context, then in key_context, etc.
  38. ///
  39. /// I.e.
  40. /// builder << key_context << value_context << key_context << ...
  41. ///
  42. template <class base = closed_context>
  43. class key_context {
  44. public:
  45. ///
  46. /// Create a key_context given a core builder
  47. ///
  48. /// @param core
  49. /// The core builder to orchestrate
  50. ///
  51. key_context(core* core) : _core(core) {}
  52. ///
  53. /// << operator for accepting a literal key and appending it to the core
  54. /// builder.
  55. ///
  56. /// @param v
  57. /// The key to append
  58. ///
  59. /// @throws bsoncxx::exception if the previous value appended to the builder was also a key.
  60. ///
  61. template <std::size_t n>
  62. BSONCXX_INLINE value_context<key_context> operator<<(const char (&v)[n]) {
  63. _core->key_view(stdx::string_view{v, n - 1});
  64. return value_context<key_context>(_core);
  65. }
  66. ///
  67. /// << operator for accepting a std::string key and appending it to the core
  68. /// builder.
  69. ///
  70. /// @param str
  71. /// The key to append
  72. ///
  73. /// @throws bsoncxx::exception if the previous value appended to the builder was also a key.
  74. ///
  75. BSONCXX_INLINE value_context<key_context> operator<<(std::string str) {
  76. _core->key_owned(std::move(str));
  77. return value_context<key_context>(_core);
  78. }
  79. ///
  80. /// << operator for accepting a stdx::string_view key and appending it to
  81. /// the core builder.
  82. ///
  83. /// @param str
  84. /// The key to append
  85. ///
  86. /// @throws bsoncxx::exception if the previous value appended to the builder was also a key.
  87. ///
  88. BSONCXX_INLINE value_context<key_context> operator<<(stdx::string_view str) {
  89. _core->key_view(std::move(str));
  90. return value_context<key_context>(_core);
  91. }
  92. ///
  93. /// << operator for accepting a callable of the form void(key_context)
  94. /// and invoking it to perform 1 or more key, value appends to the core
  95. /// builder.
  96. ///
  97. /// @param func
  98. /// The callback to invoke
  99. ///
  100. template <typename T>
  101. BSONCXX_INLINE
  102. typename std::enable_if<util::is_functor<T, void(key_context<>)>::value, key_context>::type&
  103. operator<<(T&& func) {
  104. func(*this);
  105. return *this;
  106. }
  107. ///
  108. /// << operator for finalizing the stream.
  109. ///
  110. /// This operation finishes all processing necessary to fully encode the
  111. /// bson bytes and returns an owning value.
  112. ///
  113. /// @param _
  114. /// A finalize_type token
  115. ///
  116. /// @return A value type which holds the complete bson document.
  117. ///
  118. template <typename T>
  119. BSONCXX_INLINE typename std::enable_if<
  120. std::is_same<base, closed_context>::value &&
  121. std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
  122. // TODO(MSVC): This should just be 'document::value', but
  123. // VS2015U1 can't resolve the name.
  124. bsoncxx::document::value>::type
  125. operator<<(T&&) {
  126. return _core->extract_document();
  127. }
  128. ///
  129. /// << operator for concatenating another document.
  130. ///
  131. /// This operation concatenates all of the keys and values from the passed
  132. /// document into the current stream.
  133. ///
  134. /// @param doc
  135. /// A document to concatenate
  136. ///
  137. BSONCXX_INLINE key_context operator<<(concatenate_doc doc) {
  138. _core->concatenate(doc);
  139. return *this;
  140. }
  141. ///
  142. /// << operator for closing a subdocument in the core builder.
  143. ///
  144. /// @param _
  145. /// A close_document_type token
  146. ///
  147. BSONCXX_INLINE base operator<<(const close_document_type) {
  148. _core->close_document();
  149. return unwrap();
  150. }
  151. ///
  152. /// Conversion operator which provides a rooted document given any stream
  153. /// currently in a nested key_context.
  154. ///
  155. BSONCXX_INLINE operator key_context<>() {
  156. return key_context<>(_core);
  157. }
  158. private:
  159. BSONCXX_INLINE base unwrap() {
  160. return base(_core);
  161. }
  162. core* _core;
  163. };
  164. } // namespace stream
  165. } // namespace builder
  166. BSONCXX_INLINE_NAMESPACE_END
  167. } // namespace bsoncxx
  168. #include <bsoncxx/config/postlude.hpp>