value_context.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/array_context.hpp>
  17. #include <bsoncxx/builder/stream/closed_context.hpp>
  18. #include <bsoncxx/builder/stream/helpers.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 value, which can later be followed by
  27. /// 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>
  43. class value_context {
  44. public:
  45. ///
  46. /// Create a value_context given a core builder
  47. ///
  48. /// @param core
  49. /// The core builder to orchestrate
  50. ///
  51. BSONCXX_INLINE value_context(core* core) : _core(core) {}
  52. ///
  53. /// << operator for accepting a real value and appending it to the core
  54. /// builder.
  55. ///
  56. /// @param t
  57. /// The value to append
  58. ///
  59. template <class T>
  60. BSONCXX_INLINE
  61. typename std::enable_if<!util::is_functor<T, void(single_context)>::value, base>::type
  62. operator<<(T&& t) {
  63. _core->append(std::forward<T>(t));
  64. return unwrap();
  65. }
  66. ///
  67. /// << operator for accepting a callable of the form void(single_context)
  68. /// and invoking it to perform a value append to the core builder.
  69. ///
  70. /// @param func
  71. /// The callback to invoke
  72. ///
  73. template <typename T>
  74. BSONCXX_INLINE
  75. typename std::enable_if<util::is_functor<T, void(single_context)>::value, base>::type
  76. operator<<(T&& func) {
  77. func(*this);
  78. return unwrap();
  79. }
  80. ///
  81. /// << operator for opening a new subdocument in the core builder.
  82. ///
  83. /// @param _
  84. /// An open_document_type token
  85. ///
  86. BSONCXX_INLINE key_context<base> operator<<(const open_document_type) {
  87. _core->open_document();
  88. return wrap_document();
  89. }
  90. ///
  91. /// << operator for opening a new subarray in the core builder.
  92. ///
  93. /// @param _
  94. /// An open_array_type token
  95. ///
  96. BSONCXX_INLINE array_context<base> operator<<(const open_array_type) {
  97. _core->open_array();
  98. return wrap_array();
  99. }
  100. ///
  101. /// Conversion operator for single_context.
  102. ///
  103. /// @relatesalso single_context
  104. ///
  105. operator single_context();
  106. #if !defined(_MSC_VER)
  107. // TODO(MSVC): Causes an ICE under VS2015U1
  108. static_assert(
  109. std::is_same<value_context, decltype(std::declval<value_context>() << 1 << "str")>::value,
  110. "value_context must be templatized on a key_context");
  111. #endif
  112. private:
  113. BSONCXX_INLINE base unwrap() {
  114. return base(_core);
  115. }
  116. BSONCXX_INLINE array_context<base> wrap_array() {
  117. return array_context<base>(_core);
  118. }
  119. BSONCXX_INLINE key_context<base> wrap_document() {
  120. return key_context<base>(_core);
  121. }
  122. core* _core;
  123. };
  124. } // namespace stream
  125. } // namespace builder
  126. BSONCXX_INLINE_NAMESPACE_END
  127. } // namespace bsoncxx
  128. #include <bsoncxx/config/postlude.hpp>