document.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/key_context.hpp>
  17. #include <bsoncxx/builder/stream/single_context.hpp>
  18. #include <bsoncxx/document/value.hpp>
  19. #include <bsoncxx/document/view.hpp>
  20. #include <bsoncxx/config/prelude.hpp>
  21. namespace bsoncxx {
  22. BSONCXX_INLINE_NAMESPACE_BEGIN
  23. namespace builder {
  24. namespace stream {
  25. ///
  26. /// A streaming interface for constructing
  27. /// a BSON document.
  28. ///
  29. /// @note Use of the stream builder is discouraged. See
  30. /// http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working-with-bson/#stream-builder for more
  31. /// details.
  32. ///
  33. class document : public key_context<> {
  34. public:
  35. ///
  36. /// Default constructor.
  37. ///
  38. BSONCXX_INLINE document() : key_context<>(&_core), _core(false) {}
  39. ///
  40. /// @return A view of the BSON document.
  41. ///
  42. BSONCXX_INLINE bsoncxx::document::view view() const {
  43. return _core.view_document();
  44. }
  45. ///
  46. /// @return A view of the BSON document.
  47. ///
  48. BSONCXX_INLINE operator bsoncxx::document::view() const {
  49. return view();
  50. }
  51. ///
  52. /// Transfer ownership of the underlying document to the caller.
  53. ///
  54. /// @return A document::value with ownership of the document.
  55. ///
  56. /// @warning
  57. /// After calling extract() it is illegal to call any methods
  58. /// on this class, unless it is subsequenly moved into.
  59. ///
  60. BSONCXX_INLINE bsoncxx::document::value extract() {
  61. return _core.extract_document();
  62. }
  63. ///
  64. /// Reset the underlying BSON to an empty document.
  65. ///
  66. BSONCXX_INLINE void clear() {
  67. _core.clear();
  68. }
  69. private:
  70. core _core;
  71. };
  72. } // namespace stream
  73. } // namespace builder
  74. BSONCXX_INLINE_NAMESPACE_END
  75. } // namespace bsoncxx
  76. #include <bsoncxx/config/postlude.hpp>