single_context.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/key_context.hpp>
  18. #include <bsoncxx/builder/stream/value_context.hpp>
  19. #include <bsoncxx/config/prelude.hpp>
  20. namespace bsoncxx {
  21. BSONCXX_INLINE_NAMESPACE_BEGIN
  22. namespace builder {
  23. namespace stream {
  24. ///
  25. /// A stream context which appends a single value.
  26. ///
  27. /// This type is useful as the argument to a callable passed to other stream
  28. /// modes. Specifically, any callback that takes a single_context can be used to
  29. /// write a value in value_context or array_context.
  30. ///
  31. class single_context {
  32. public:
  33. ///
  34. /// Create a single_context given a core builder
  35. ///
  36. /// @param core
  37. /// The core builder to orchestrate
  38. ///
  39. BSONCXX_INLINE single_context(core* core) : _core(core) {}
  40. ///
  41. /// << operator for opening a new subdocument in the core builder.
  42. ///
  43. /// @param _
  44. /// An open_document_type token
  45. ///
  46. BSONCXX_INLINE key_context<> operator<<(open_document_type) {
  47. _core->open_document();
  48. return wrap_document();
  49. }
  50. ///
  51. /// << operator for opening a new subarray in the core builder.
  52. ///
  53. /// @param _
  54. /// An open_array_type token
  55. ///
  56. BSONCXX_INLINE array_context<> operator<<(open_array_type) {
  57. _core->open_array();
  58. return wrap_array();
  59. }
  60. ///
  61. /// << operator for accepting a real value and appending it to the core
  62. /// builder.
  63. ///
  64. /// @param t
  65. /// The value to append
  66. ///
  67. template <class T>
  68. BSONCXX_INLINE void operator<<(T&& t) {
  69. _core->append(std::forward<T>(t));
  70. }
  71. private:
  72. BSONCXX_INLINE array_context<> wrap_array() {
  73. return array_context<>(_core);
  74. }
  75. BSONCXX_INLINE key_context<> wrap_document() {
  76. return key_context<>(_core);
  77. }
  78. core* _core;
  79. };
  80. ///
  81. /// Implementation of the single_context conversion operator for array_context.
  82. ///
  83. template <class T>
  84. BSONCXX_INLINE array_context<T>::operator single_context() {
  85. return single_context(_core);
  86. }
  87. ///
  88. /// Implementation of the single_context conversion operator for value_context.
  89. ///
  90. template <class T>
  91. BSONCXX_INLINE value_context<T>::operator single_context() {
  92. return single_context(_core);
  93. }
  94. } // namespace stream
  95. } // namespace builder
  96. BSONCXX_INLINE_NAMESPACE_END
  97. } // namespace bsoncxx
  98. #include <bsoncxx/config/postlude.hpp>