array.hpp 2.4 KB

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