value.hpp 4.0 KB

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