MongoDB C++ Driver  mongocxx-3.7.0
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
value_context.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/core.hpp>
18 #include <bsoncxx/builder/stream/array_context.hpp>
19 #include <bsoncxx/builder/stream/closed_context.hpp>
20 #include <bsoncxx/builder/stream/helpers.hpp>
21 #include <bsoncxx/util/functor.hpp>
22 
23 #include <bsoncxx/config/prelude.hpp>
24 
25 namespace bsoncxx {
26 BSONCXX_INLINE_NAMESPACE_BEGIN
27 namespace builder {
28 namespace stream {
29 
47 template <class base>
49  public:
56  BSONCXX_INLINE value_context(core* core) : _core(core) {}
57 
65  template <class T>
66  BSONCXX_INLINE
67  typename std::enable_if<!util::is_functor<T, void(single_context)>::value, base>::type
68  operator<<(T&& t) {
69  _core->append(std::forward<T>(t));
70  return unwrap();
71  }
72 
80  template <typename T>
81  BSONCXX_INLINE
82  typename std::enable_if<util::is_functor<T, void(single_context)>::value, base>::type
83  operator<<(T&& func) {
84  func(*this);
85  return unwrap();
86  }
87 
95  _core->open_document();
96  return wrap_document();
97  }
98 
106  _core->open_array();
107  return wrap_array();
108  }
109 
115  operator single_context();
116 
117 #if !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
118  // TODO(MSVC): Causes an ICE under VS2015U1
119  static_assert(
120  std::is_same<value_context, decltype(std::declval<value_context>() << 1 << "str")>::value,
121  "value_context must be templatized on a key_context");
122 #endif
123 
124  private:
125  BSONCXX_INLINE base unwrap() {
126  return base(_core);
127  }
128 
129  BSONCXX_INLINE array_context<base> wrap_array() {
130  return array_context<base>(_core);
131  }
132 
133  BSONCXX_INLINE key_context<base> wrap_document() {
134  return key_context<base>(_core);
135  }
136 
137  core* _core;
138 };
139 
140 } // namespace stream
141 } // namespace builder
142 BSONCXX_INLINE_NAMESPACE_END
143 } // namespace bsoncxx
144 
145 #include <bsoncxx/config/postlude.hpp>
bsoncxx::builder::stream::value_context
A stream context which expects a value, which can later be followed by more key/value pairs.
Definition: value_context.hpp:48
bsoncxx::builder::stream::value_context::value_context
value_context(core *core)
Create a value_context given a core builder.
Definition: value_context.hpp:56
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::builder::stream::open_array_type
The type of a stream manipulator to open a subarray.
Definition: helpers.hpp:56
bsoncxx::builder::stream::open_document_type
The type of a stream manipulator to open a subdocument.
Definition: helpers.hpp:32
bsoncxx::builder::core::append
core & append(const types::b_double &value)
Appends a BSON double.
bsoncxx::builder::stream::single_context
A stream context which appends a single value.
Definition: single_context.hpp:36
bsoncxx::builder::stream::array_context
A stream context which expects any number of values.
Definition: array_context.hpp:52
bsoncxx::builder::core::open_array
core & open_array()
Opens a sub-array within this BSON datum.
bsoncxx::builder::core
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:42
bsoncxx::builder::core::open_document
core & open_document()
Opens a sub-document within this BSON datum.
bsoncxx::builder::stream::value_context::operator<<
std::enable_if<!util::is_functor< T, void(single_context)>::value, base >::type operator<<(T &&t)
<< operator for accepting a real value and appending it to the core builder.
Definition: value_context.hpp:68
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:46
bsoncxx::builder::stream::key_context
A stream context which expects a key, which can later be followed by value, then more key/value pairs...
Definition: array_context.hpp:32