MongoDB C++ Driver  mongocxx-3.0.3
impl.hpp
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 
15 #pragma once
16 
17 #include <bsoncxx/builder/basic/sub_array.hpp>
18 #include <bsoncxx/builder/basic/sub_document.hpp>
19 #include <bsoncxx/util/functor.hpp>
20 
21 #include <bsoncxx/config/prelude.hpp>
22 
23 namespace bsoncxx {
24 BSONCXX_INLINE_NAMESPACE_BEGIN
25 namespace builder {
26 namespace basic {
27 namespace impl {
28 
29 template <typename T>
30 using takes_document = typename util::is_functor<T, void(sub_document)>;
31 
32 template <typename T>
33 using takes_array = typename util::is_functor<T, void(sub_array)>;
34 
35 template <typename T>
36 BSONCXX_INLINE typename std::enable_if<takes_document<T>::value, void>::type generic_append(
37  core* core, T&& func) {
38  core->open_document();
39  func(sub_document(core));
40  core->close_document();
41 }
42 
43 template <typename T>
44 BSONCXX_INLINE typename std::enable_if<takes_array<T>::value, void>::type generic_append(core* core,
45  T&& func) {
46  core->open_array();
47  func(sub_array(core));
48  core->close_array();
49 }
50 
51 template <typename T>
52 BSONCXX_INLINE
53  typename std::enable_if<!takes_document<T>::value && !takes_array<T>::value, void>::type
54  generic_append(core* core, T&& t) {
55  core->append(std::forward<T>(t));
56 }
57 
58 template <typename T>
59 BSONCXX_INLINE void value_append(core* core, T&& t) {
60  generic_append(core, std::forward<T>(t));
61 }
62 
63 } // namespace impl
64 } // namespace basic
65 } // namespace builder
66 BSONCXX_INLINE_NAMESPACE_END
67 } // namespace bsoncxx
68 
69 #include <bsoncxx/config/postlude.hpp>
Definition: element.hpp:24