sub_document.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/builder/basic/helpers.hpp>
  16. #include <bsoncxx/builder/concatenate.hpp>
  17. #include <bsoncxx/builder/core.hpp>
  18. #include <bsoncxx/stdx/string_view.hpp>
  19. #include <bsoncxx/config/prelude.hpp>
  20. namespace bsoncxx {
  21. BSONCXX_INLINE_NAMESPACE_BEGIN
  22. namespace builder {
  23. namespace basic {
  24. namespace impl {
  25. template <typename T>
  26. void value_append(core* core, T&& t);
  27. } // namespace impl
  28. ///
  29. /// An internal class of builder::basic.
  30. /// Users should almost always construct a builder::basic::document instead.
  31. ///
  32. class sub_document {
  33. public:
  34. BSONCXX_INLINE sub_document(core* core) : _core(core) {}
  35. ///
  36. /// Appends multiple basic::kvp key-value pairs.
  37. ///
  38. template <typename Arg, typename... Args>
  39. BSONCXX_INLINE void append(Arg&& a, Args&&... args) {
  40. append_(std::forward<Arg>(a));
  41. append(std::forward<Args>(args)...);
  42. }
  43. ///
  44. /// Inductive base-case for the variadic append(...)
  45. ///
  46. BSONCXX_INLINE
  47. void append() {}
  48. private:
  49. //
  50. // Appends a basic::kvp where the key is a non-owning string view.
  51. //
  52. template <typename K, typename V>
  53. BSONCXX_INLINE typename std::enable_if<
  54. std::is_same<typename std::decay<K>::type, stdx::string_view>::value>::type
  55. append_(std::tuple<K, V>&& t) {
  56. _core->key_view(std::forward<K>(std::get<0>(t)));
  57. impl::value_append(_core, std::forward<V>(std::get<1>(t)));
  58. }
  59. //
  60. // Appends a basic::kvp where the key is an owning STL string.
  61. //
  62. template <typename K, typename V>
  63. BSONCXX_INLINE typename std::enable_if<
  64. std::is_same<typename std::decay<K>::type, std::string>::value>::type
  65. append_(std::tuple<K, V>&& t) {
  66. _core->key_view(std::forward<K>(std::get<0>(t)));
  67. impl::value_append(_core, std::forward<V>(std::get<1>(t)));
  68. }
  69. //
  70. // Appends a basic::kvp where the key is a string literal
  71. //
  72. template <std::size_t n, typename V>
  73. BSONCXX_INLINE void append_(std::tuple<const char (&)[n], V>&& t) {
  74. _core->key_view(stdx::string_view{std::get<0>(t), n - 1});
  75. impl::value_append(_core, std::forward<V>(std::get<1>(t)));
  76. }
  77. //
  78. // Concatenates another bson document directly.
  79. //
  80. BSONCXX_INLINE
  81. void append_(concatenate_doc doc) {
  82. _core->concatenate(doc);
  83. }
  84. core* _core;
  85. };
  86. } // namespace basic
  87. } // namespace builder
  88. BSONCXX_INLINE_NAMESPACE_END
  89. } // namespace bsoncxx
  90. #include <bsoncxx/config/postlude.hpp>