MongoDB C++ Driver  mongocxx-3.9.0
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
sub_document.hpp
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 
15 #pragma once
16 
17 #include <bsoncxx/builder/basic/helpers.hpp>
18 #include <bsoncxx/builder/concatenate.hpp>
19 #include <bsoncxx/builder/core.hpp>
20 #include <bsoncxx/stdx/string_view.hpp>
21 #include <bsoncxx/stdx/type_traits.hpp>
22 
23 #include <bsoncxx/config/prelude.hpp>
24 
25 namespace bsoncxx {
26 inline namespace v_noabi {
27 namespace builder {
28 namespace basic {
29 
30 namespace impl {
31 template <typename T>
32 void value_append(core* core, T&& t);
33 } // namespace impl
34 
39 class sub_document {
40  public:
41  BSONCXX_INLINE sub_document(core* core) : _core(core) {}
42 
46  template <typename Arg, typename... Args>
47  BSONCXX_INLINE void append(Arg&& a, Args&&... args) {
48  append_(std::forward<Arg>(a));
49  append(std::forward<Args>(args)...);
50  }
51 
55  BSONCXX_INLINE
56  void append() {}
57 
58  private:
59  //
60  // Appends a basic::kvp where the key is a non-owning string view.
61  //
62  template <typename K, typename V>
63  BSONCXX_INLINE detail::requires_t<void, detail::is_alike<K, stdx::string_view>> //
64  append_(std::tuple<K, V>&& t) {
65  _core->key_view(std::forward<K>(std::get<0>(t)));
66  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
67  }
68 
69  //
70  // Appends a basic::kvp where the key is an owning STL string.
71  //
72  template <typename K, typename V>
73  BSONCXX_INLINE detail::requires_t<void, detail::is_alike<K, std::string>> //
74  append_(std::tuple<K, V>&& t) {
75  _core->key_owned(std::forward<K>(std::get<0>(t)));
76  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
77  }
78 
79  //
80  // Appends a basic::kvp where the key is a string literal
81  //
82  template <std::size_t n, typename V>
83  BSONCXX_INLINE void append_(std::tuple<const char (&)[n], V>&& t) {
84  _core->key_view(stdx::string_view{std::get<0>(t), n - 1});
85  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
86  }
87 
88  //
89  // Concatenates another bson document directly.
90  //
91  BSONCXX_INLINE
92  void append_(concatenate_doc doc) {
93  _core->concatenate(doc);
94  }
95 
96  core* _core;
97 };
98 
99 } // namespace basic
100 } // namespace builder
101 } // namespace v_noabi
102 } // namespace bsoncxx
103 
104 #include <bsoncxx/config/postlude.hpp>
An internal class of builder::basic.
Definition: sub_document.hpp:39
void append(Arg &&a, Args &&... args)
Appends multiple basic::kvp key-value pairs.
Definition: sub_document.hpp:47
void append()
Inductive base-case for the variadic append(...)
Definition: sub_document.hpp:56
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:43
core & key_owned(std::string key)
Appends a key passed as an STL string.
core & concatenate(const document::view &view)
Appends the keys from a BSON document into this BSON datum.
core & key_view(stdx::string_view key)
Appends a key passed as a non-owning stdx::string_view.
The top-level namespace for bsoncxx library entities.
Definition: element.hpp:24