array.hpp 3.0 KB

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