value.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <cstdlib>
  16. #include <memory>
  17. #include <bsoncxx/array/view.hpp>
  18. #include <bsoncxx/document/value.hpp>
  19. #include <bsoncxx/config/prelude.hpp>
  20. namespace bsoncxx {
  21. BSONCXX_INLINE_NAMESPACE_BEGIN
  22. namespace array {
  23. ///
  24. /// A read-only BSON array that owns its underlying buffer. When a array::value goes
  25. /// out of scope, the underlying buffer is freed. Generally this class should be used
  26. /// sparingly; array::view should be used instead wherever possible.
  27. ///
  28. class BSONCXX_API value {
  29. public:
  30. using deleter_type = void (*)(std::uint8_t*);
  31. using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
  32. ///
  33. /// Constructs a value from a buffer.
  34. /// This constructor transfers ownership of the buffer to the resulting
  35. /// value. A user-provided deleter is used to destroy the buffer.
  36. ///
  37. /// @param data
  38. /// A pointer to a buffer containing a valid BSON array.
  39. /// @param length
  40. /// The length of the document.
  41. /// @param dtor
  42. /// A user provided deleter.
  43. ///
  44. value(std::uint8_t* data, std::size_t length, deleter_type dtor);
  45. ///
  46. /// Constructs a value from a std::unique_ptr to a buffer. The ownership
  47. /// of the buffer is transferred to the resulting value.
  48. ///
  49. /// @param ptr
  50. /// A pointer to a buffer containing a valid BSON array.
  51. /// @param length
  52. /// The length of the document.
  53. ///
  54. value(unique_ptr_type ptr, std::size_t length);
  55. ///
  56. /// Constructs a value from a view of an array. The data referenced
  57. /// by the array::view will be copied into a new buffer managed by the
  58. /// constructed value.
  59. ///
  60. /// @param view
  61. /// A view of another array to copy.
  62. ///
  63. explicit value(array::view view);
  64. value(const value&);
  65. value& operator=(const value&);
  66. value(value&&) noexcept = default;
  67. value& operator=(value&&) noexcept = default;
  68. ///
  69. /// Get a view over the document owned by this value.
  70. ///
  71. BSONCXX_INLINE array::view view() const noexcept;
  72. ///
  73. /// Conversion operator that provides a view given a value.
  74. ///
  75. /// @return A view over the value.
  76. ///
  77. BSONCXX_INLINE operator array::view() const noexcept;
  78. ///
  79. /// Transfer ownership of the underlying buffer to the caller.
  80. ///
  81. /// @warning
  82. /// After calling release() it is illegal to call any methods
  83. /// on this class, unless it is subsequently moved into.
  84. ///
  85. /// @return A std::unique_ptr with ownership of the buffer.
  86. ///
  87. unique_ptr_type release();
  88. private:
  89. unique_ptr_type _data;
  90. std::size_t _length{0};
  91. };
  92. BSONCXX_INLINE array::view value::view() const noexcept {
  93. return array::view{static_cast<uint8_t*>(_data.get()), _length};
  94. }
  95. BSONCXX_INLINE value::operator array::view() const noexcept {
  96. return view();
  97. }
  98. } // namespace array
  99. BSONCXX_INLINE_NAMESPACE_END
  100. } // namespace bsoncxx
  101. #include <bsoncxx/config/postlude.hpp>