document.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/basic/impl.hpp>
  16. #include <bsoncxx/builder/basic/kvp.hpp>
  17. #include <bsoncxx/builder/basic/sub_document.hpp>
  18. #include <bsoncxx/builder/core.hpp>
  19. #include <bsoncxx/document/value.hpp>
  20. #include <bsoncxx/document/view.hpp>
  21. #include <bsoncxx/config/prelude.hpp>
  22. namespace bsoncxx {
  23. BSONCXX_INLINE_NAMESPACE_BEGIN
  24. namespace builder {
  25. namespace basic {
  26. class array;
  27. ///
  28. /// A traditional builder-style interface for constructing
  29. /// a BSON document.
  30. ///
  31. class document : public sub_document {
  32. public:
  33. ///
  34. /// Default constructor
  35. ///
  36. BSONCXX_INLINE document() : sub_document(&_core), _core(false) {}
  37. ///
  38. /// Move constructor
  39. ///
  40. BSONCXX_INLINE document(document &&doc) noexcept : sub_document(&_core),
  41. _core(std::move(doc._core)) {}
  42. ///
  43. /// Move assignment operator
  44. ///
  45. BSONCXX_INLINE document& operator=(document&& doc) noexcept {
  46. _core = std::move(doc._core);
  47. return *this;
  48. }
  49. ///
  50. /// @return A view of the BSON document.
  51. ///
  52. BSONCXX_INLINE bsoncxx::document::view view() const {
  53. return _core.view_document();
  54. }
  55. ///
  56. /// Conversion operator that provides a view of the current builder
  57. /// contents.
  58. ///
  59. /// @return A view of the current builder contents.
  60. ///
  61. BSONCXX_INLINE operator bsoncxx::document::view() const {
  62. return view();
  63. }
  64. ///
  65. /// Transfer ownership of the underlying document to the caller.
  66. ///
  67. /// @return A document::value with ownership of the document.
  68. ///
  69. /// @warning
  70. /// After calling extract() it is illegal to call any methods
  71. /// on this class, unless it is subsequenly moved into.
  72. ///
  73. BSONCXX_INLINE bsoncxx::document::value extract() {
  74. return _core.extract_document();
  75. }
  76. ///
  77. /// Reset the underlying BSON to an empty document.
  78. ///
  79. BSONCXX_INLINE void clear() {
  80. _core.clear();
  81. }
  82. private:
  83. core _core;
  84. };
  85. ///
  86. /// Creates a document from a list of key-value pairs.
  87. ///
  88. /// @param args
  89. /// A variadiac list of key-value pairs. The types of the keys and values can be anything that
  90. /// builder::basic::sub_document::append accepts.
  91. ///
  92. /// @return
  93. /// A bsoncxx::document::value containing the elements.
  94. ///
  95. template <typename... Args>
  96. bsoncxx::document::value BSONCXX_CALL make_document(Args&&... args) {
  97. basic::document document;
  98. document.append(std::forward<Args>(args)...);
  99. return document.extract();
  100. }
  101. } // namespace basic
  102. } // namespace builder
  103. BSONCXX_INLINE_NAMESPACE_END
  104. } // namespace bsoncxx
  105. #include <bsoncxx/config/postlude.hpp>