MongoDB C++ Driver current
Loading...
Searching...
No Matches
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/stdx/type_traits.hpp>
22
23#include <bsoncxx/config/prelude.hpp>
24
25namespace bsoncxx {
26inline namespace v_noabi {
27namespace builder {
28namespace stream {
29
47template <class base>
49 public:
56 BSONCXX_INLINE value_context(core* core) : _core(core) {}
57
65 template <class T>
66 BSONCXX_INLINE detail::requires_not_t<base, detail::is_invocable<T, single_context>> //
67 operator<<(T&& t) {
68 _core->append(std::forward<T>(t));
69 return unwrap();
70 }
71
79 template <typename T>
80 BSONCXX_INLINE detail::requires_t<base, detail::is_invocable<T, single_context>> //
81 operator<<(T&& func) {
82 detail::invoke(std::forward<T>(func), *this);
83 return unwrap();
84 }
85
91 BSONCXX_INLINE key_context<base> operator<<(const open_document_type) {
92 _core->open_document();
93 return wrap_document();
94 }
95
101 BSONCXX_INLINE array_context<base> operator<<(const open_array_type) {
102 _core->open_array();
103 return wrap_array();
104 }
105
111 operator single_context();
112
113#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
114 // TODO(MSVC): Causes an ICE under VS2015U1
115 static_assert(
116 std::is_same<value_context, decltype(std::declval<value_context>() << 1 << "str")>::value,
117 "value_context must be templatized on a key_context");
118#endif
119
120 private:
121 BSONCXX_INLINE base unwrap() {
122 return base(_core);
123 }
124
125 BSONCXX_INLINE array_context<base> wrap_array() {
126 return array_context<base>(_core);
127 }
128
129 BSONCXX_INLINE key_context<base> wrap_document() {
130 return key_context<base>(_core);
131 }
132
133 core* _core;
134};
135
136} // namespace stream
137} // namespace builder
138} // namespace v_noabi
139} // namespace bsoncxx
140
141#include <bsoncxx/config/postlude.hpp>
A low-level interface for constructing BSON documents and arrays.
Definition core.hpp:43
core & append(const types::b_double &value)
Appends a BSON double.
core & open_document()
Opens a sub-document within this BSON datum.
core & open_array()
Opens a sub-array within this BSON datum.
A stream context which expects a value, which can later be followed by more key/value pairs.
Definition value_context.hpp:48
detail::requires_not_t< base, detail::is_invocable< T, single_context > > operator<<(T &&t)
<< operator for accepting a real value and appending it to the core builder.
Definition value_context.hpp:67
The top-level namespace for bsoncxx library entities.
Definition element.hpp:24