MongoDB C++ Driver  mongocxx-3.6.7
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 
22 #include <bsoncxx/config/prelude.hpp>
23 
24 namespace bsoncxx {
25 BSONCXX_INLINE_NAMESPACE_BEGIN
26 namespace builder {
27 namespace basic {
28 
29 namespace impl {
30 template <typename T>
31 void value_append(core* core, T&& t);
32 } // namespace impl
33 
38 class sub_document {
39  public:
40  BSONCXX_INLINE sub_document(core* core) : _core(core) {}
41 
45  template <typename Arg, typename... Args>
46  BSONCXX_INLINE void append(Arg&& a, Args&&... args) {
47  append_(std::forward<Arg>(a));
48  append(std::forward<Args>(args)...);
49  }
50 
54  BSONCXX_INLINE
55  void append() {}
56 
57  private:
58  //
59  // Appends a basic::kvp where the key is a non-owning string view.
60  //
61  template <typename K, typename V>
62  BSONCXX_INLINE typename std::enable_if<
63  std::is_same<typename std::decay<K>::type, stdx::string_view>::value>::type
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 typename std::enable_if<
74  std::is_same<typename std::decay<K>::type, std::string>::value>::type
75  append_(std::tuple<K, V>&& t) {
76  _core->key_owned(std::forward<K>(std::get<0>(t)));
77  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
78  }
79 
80  //
81  // Appends a basic::kvp where the key is a string literal
82  //
83  template <std::size_t n, typename V>
84  BSONCXX_INLINE void append_(std::tuple<const char (&)[n], V>&& t) {
85  _core->key_view(stdx::string_view{std::get<0>(t), n - 1});
86  impl::value_append(_core, std::forward<V>(std::get<1>(t)));
87  }
88 
89  //
90  // Concatenates another bson document directly.
91  //
92  BSONCXX_INLINE
93  void append_(concatenate_doc doc) {
94  _core->concatenate(doc);
95  }
96 
97  core* _core;
98 };
99 
100 } // namespace basic
101 } // namespace builder
102 BSONCXX_INLINE_NAMESPACE_END
103 } // namespace bsoncxx
104 
105 #include <bsoncxx/config/postlude.hpp>
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::builder::core::key_owned
core & key_owned(std::string key)
Appends a key passed as an STL string.
bsoncxx::builder::basic::sub_document::append
void append()
Inductive base-case for the variadic append(...)
Definition: sub_document.hpp:55
bsoncxx::builder::basic::sub_document::append
void append(Arg &&a, Args &&... args)
Appends multiple basic::kvp key-value pairs.
Definition: sub_document.hpp:46
bsoncxx::builder::core::key_view
core & key_view(stdx::string_view key)
Appends a key passed as a non-owning stdx::string_view.
bsoncxx::builder::core
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:42
bsoncxx::builder::basic::sub_document
An internal class of builder::basic.
Definition: sub_document.hpp:38
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:39
bsoncxx::builder::core::concatenate
core & concatenate(const document::view &view)
Appends the keys from a BSON document into this BSON datum.