MongoDB C++ Driver  mongocxx-3.10.2
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/sub_document-fwd.hpp>
18 
19 #include <bsoncxx/builder/basic/helpers.hpp>
20 #include <bsoncxx/builder/concatenate.hpp>
21 #include <bsoncxx/builder/core.hpp>
22 #include <bsoncxx/stdx/string_view.hpp>
23 #include <bsoncxx/stdx/type_traits.hpp>
24 
25 #include <bsoncxx/config/prelude.hpp>
26 
27 namespace bsoncxx {
28 namespace v_noabi {
29 namespace builder {
30 namespace basic {
31 
32 namespace impl {
33 
34 template <typename T>
35 void value_append(core* core, T&& t);
36 
37 } // namespace impl
38 
43 class sub_document {
44  public:
45  BSONCXX_INLINE sub_document(core* core) : _core(core) {}
46 
50  template <typename Arg, typename... Args>
51  BSONCXX_INLINE void append(Arg&& a, Args&&... args) {
52  append_(std::forward<Arg>(a));
53  append(std::forward<Args>(args)...);
54  }
55 
59  BSONCXX_INLINE
60  void append() {}
61 
62  private:
63  //
64  // Appends a basic::kvp where the key is a non-owning string view.
65  //
66  template <typename K, typename V>
67  BSONCXX_INLINE detail::requires_t<void, detail::is_alike<K, stdx::string_view>> //
68  append_(std::tuple<K, V>&& t) {
69  _core->key_view(std::forward<K>(std::get<0>(t)));
70  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
71  }
72 
73  //
74  // Appends a basic::kvp where the key is an owning STL string.
75  //
76  template <typename K, typename V>
77  BSONCXX_INLINE detail::requires_t<void, detail::is_alike<K, std::string>> //
78  append_(std::tuple<K, V>&& t) {
79  _core->key_owned(std::forward<K>(std::get<0>(t)));
80  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
81  }
82 
83  //
84  // Appends a basic::kvp where the key is a string literal
85  //
86  template <std::size_t n, typename V>
87  BSONCXX_INLINE void append_(std::tuple<const char (&)[n], V>&& t) {
88  _core->key_view(stdx::string_view{std::get<0>(t), n - 1});
89  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
90  }
91 
92  //
93  // Concatenates another bson document directly.
94  //
95  BSONCXX_INLINE
96  void append_(concatenate_doc doc) {
97  _core->concatenate(doc);
98  }
99 
100  core* _core;
101 };
102 
103 } // namespace basic
104 } // namespace builder
105 } // namespace v_noabi
106 } // namespace bsoncxx
107 
108 #include <bsoncxx/config/postlude.hpp>
An internal class of builder::basic.
Definition: sub_document.hpp:43
void append(Arg &&a, Args &&... args)
Appends multiple basic::kvp key-value pairs.
Definition: sub_document.hpp:51
void append()
Inductive base-case for the variadic append(...)
Definition: sub_document.hpp:60
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:45
core & key_owned(std::string key)
Appends a key passed as an STL string.
core & concatenate(const bsoncxx::v_noabi::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-fwd.hpp:19