concatenate.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2015 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/view_or_value.hpp>
  16. #include <bsoncxx/document/view_or_value.hpp>
  17. #include <bsoncxx/config/prelude.hpp>
  18. namespace bsoncxx {
  19. BSONCXX_INLINE_NAMESPACE_BEGIN
  20. namespace builder {
  21. ///
  22. /// Container to concatenate a document. Use it by constructing an instance with the
  23. /// document to be concatenated, and pass it into a document stream builder.
  24. ///
  25. struct concatenate_doc {
  26. document::view_or_value doc;
  27. // MSVC seems to need a hint that it should always
  28. // inline this destructor.
  29. BSONCXX_INLINE ~concatenate_doc() = default;
  30. ///
  31. /// Conversion operator that provides a view of the wrapped concatenate
  32. /// document.
  33. ///
  34. /// @return A view of the wrapped concatenate document.
  35. ///
  36. BSONCXX_INLINE operator document::view() const {
  37. return doc;
  38. }
  39. ///
  40. /// Accessor that provides a view of the wrapped concatenate
  41. /// document.
  42. ///
  43. /// @return A view of the wrapped concatenate document.
  44. ///
  45. BSONCXX_INLINE document::view view() const {
  46. return doc;
  47. }
  48. };
  49. ///
  50. /// Container to concatenate an array. Use this with the array stream builder in order
  51. /// to pass an array into a new builder and append its values to the stream.
  52. ///
  53. struct concatenate_array {
  54. array::view_or_value array;
  55. // MSVC seems to need a hint that it should always
  56. // inline this destructor.
  57. BSONCXX_INLINE ~concatenate_array() = default;
  58. ///
  59. /// Conversion operator that provides a view of the wrapped concatenate
  60. /// array.
  61. ///
  62. /// @return A view of the wrapped concatenate array.
  63. ///
  64. BSONCXX_INLINE operator array::view() const {
  65. return array;
  66. }
  67. ///
  68. /// Accessor that provides a view of the wrapped concatenate
  69. /// array.
  70. ///
  71. /// @return A view of the wrapped concatenate array.
  72. ///
  73. BSONCXX_INLINE array::view view() const {
  74. return array;
  75. }
  76. };
  77. ///
  78. /// Helper method to concatenate a document. Use this with the document stream
  79. /// builder to merge an existing document's fields with a new document's.
  80. ///
  81. /// @param doc A document to be concatenated.
  82. ///
  83. /// @return concatenate_doc A concatenating struct.
  84. ///
  85. /// @relatesalso concatenate_doc
  86. ///
  87. BSONCXX_INLINE concatenate_doc concatenate(document::view_or_value doc) {
  88. return {std::move(doc)};
  89. }
  90. ///
  91. /// Method to concatenate an array with a new array. Use this with the array stream
  92. /// builder to merge an existing array's fields with a new array.
  93. ///
  94. /// @param array An array to be concatenated.
  95. ///
  96. /// @return concatenate_array A concatenating struct.
  97. ///
  98. /// @relatesalso concatenate_array
  99. ///
  100. BSONCXX_INLINE concatenate_array concatenate(array::view_or_value array) {
  101. return {std::move(array)};
  102. }
  103. } // namespace builder
  104. BSONCXX_INLINE_NAMESPACE_END
  105. } // namespace bsoncxx
  106. #include <bsoncxx/config/postlude.hpp>