Skip to content

Commit f275628

Browse files
authored
v1: hint (CXX-3237, CXX-3238) (mongodb#1516)
1 parent 50a1478 commit f275628

File tree

9 files changed

+411
-86
lines changed

9 files changed

+411
-86
lines changed

src/mongocxx/include/mongocxx/v1/hint.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class hint {
131131
///
132132
/// @{
133133
friend bool operator==(hint const& lhs, hint const& rhs) {
134-
return lhs.str() == rhs.str() || lhs.doc() == rhs.doc();
134+
return lhs.str() == rhs.str() && lhs.doc() == rhs.doc();
135135
}
136136

137137
friend bool operator!=(hint const& lhs, hint const& rhs) {
@@ -183,6 +183,8 @@ class hint {
183183
}
184184
/// @}
185185
///
186+
187+
class internal;
186188
};
187189

188190
} // namespace v1

src/mongocxx/include/mongocxx/v_noabi/mongocxx/hint-fwd.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#include <mongocxx/v1/hint-fwd.hpp> // IWYU pragma: export
18+
1719
#include <mongocxx/config/prelude.hpp>
1820

1921
namespace mongocxx {
@@ -26,7 +28,7 @@ class hint;
2628

2729
namespace mongocxx {
2830

29-
using ::mongocxx::v_noabi::hint;
31+
using v_noabi::hint;
3032

3133
} // namespace mongocxx
3234

@@ -36,3 +38,6 @@ using ::mongocxx::v_noabi::hint;
3638
/// @file
3739
/// Declares @ref mongocxx::v_noabi::hint.
3840
///
41+
/// @par Includes
42+
/// - @ref mongocxx/v1/hint-fwd.hpp
43+
///

src/mongocxx/include/mongocxx/v_noabi/mongocxx/hint.hpp

Lines changed: 89 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414

1515
#pragma once
1616

17-
#include <string>
18-
1917
#include <mongocxx/hint-fwd.hpp> // IWYU pragma: export
2018

19+
//
20+
21+
#include <mongocxx/v1/hint.hpp> // IWYU pragma: export
22+
23+
#include <string>
24+
#include <utility>
25+
2126
#include <bsoncxx/document/value.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
27+
#include <bsoncxx/document/view.hpp>
2228
#include <bsoncxx/document/view_or_value.hpp>
23-
#include <bsoncxx/stdx/optional.hpp>
29+
#include <bsoncxx/stdx/optional.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
2430
#include <bsoncxx/string/view_or_value.hpp>
25-
#include <bsoncxx/types/bson_value/view.hpp>
31+
#include <bsoncxx/types.hpp>
32+
#include <bsoncxx/types/view.hpp>
2633

2734
#include <mongocxx/config/prelude.hpp>
2835

@@ -34,6 +41,22 @@ namespace v_noabi {
3441
///
3542
class hint {
3643
public:
44+
///
45+
/// Construct with the @ref mongocxx::v1 equivalent.
46+
///
47+
/* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() hint(v1::hint hint);
48+
49+
///
50+
/// Convert to the @ref mongocxx::v1 equivalent.
51+
///
52+
explicit operator v1::hint() const {
53+
if (_index_doc) {
54+
return v1::hint{bsoncxx::v1::document::value{bsoncxx::v_noabi::to_v1(_index_doc->view())}};
55+
}
56+
57+
return v1::hint{std::string{_index_string->view()}};
58+
}
59+
3760
///
3861
/// Constructs a new hint.
3962
///
@@ -43,15 +66,15 @@ class hint {
4366
/// @param index
4467
/// Document view or value representing the index to be used.
4568
///
46-
MONGOCXX_ABI_EXPORT_CDECL() hint(bsoncxx::v_noabi::document::view_or_value index);
69+
hint(bsoncxx::v_noabi::document::view_or_value index) : _index_doc{std::move(index)} {}
4770

4871
///
4972
/// Constructs a new hint.
5073
///
5174
/// @param index
5275
/// String representing the name of the index to be used.
5376
///
54-
explicit MONGOCXX_ABI_EXPORT_CDECL() hint(bsoncxx::v_noabi::string::view_or_value index);
77+
explicit hint(bsoncxx::v_noabi::string::view_or_value index) : _index_string(std::move(index)) {}
5578

5679
///
5780
/// @relates mongocxx::v_noabi::hint
@@ -60,7 +83,9 @@ class hint {
6083
///
6184
/// Compares equal if the hint contains a matching index name. Otherwise, compares unequal.
6285
///
63-
friend MONGOCXX_ABI_EXPORT_CDECL(bool) operator==(hint const& index_hint, std::string index);
86+
friend bool operator==(hint const& index_hint, std::string index) {
87+
return index_hint._index_string == index;
88+
}
6489

6590
///
6691
/// @relates mongocxx::v_noabi::hint
@@ -69,26 +94,32 @@ class hint {
6994
///
7095
/// Compares equal if the hint contains a matching index document. Otherwise, compares unequal.
7196
///
72-
friend MONGOCXX_ABI_EXPORT_CDECL(bool) operator==(hint const& index_hint, bsoncxx::v_noabi::document::view index);
97+
friend bool operator==(hint const& index_hint, bsoncxx::v_noabi::document::view index) {
98+
return index_hint._index_doc == index;
99+
}
73100

74101
///
75-
/// Returns a types::bson_value::view representing this hint.
102+
/// Returns a @ref bsoncxx::v_noabi::types::view representing this hint.
76103
///
77-
/// @return Hint, as a types::bson_value::view. The caller must ensure that the returned object
78-
/// not outlive
79-
/// the hint object that it was created from.
104+
/// @return Hint, as a @ref bsoncxx::v_noabi::types::view. The caller must ensure that the returned object not
105+
/// outlive the hint object that it was created from.
80106
///
81-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::types::bson_value::view) to_value() const;
107+
bsoncxx::v_noabi::types::view to_value() const {
108+
if (_index_doc) {
109+
return bsoncxx::v_noabi::types::view{bsoncxx::v_noabi::types::b_document{_index_doc->view()}};
110+
}
111+
112+
return bsoncxx::v_noabi::types::view{bsoncxx::v_noabi::types::b_string{_index_string->view()}};
113+
}
82114

83115
///
84-
/// Returns a types::bson_value::view representing this hint.
116+
/// Returns a @ref bsoncxx::v_noabi::types::view representing this hint.
85117
///
86-
/// @return Hint, as a types::bson_value::view. The caller must ensure that the returned object
87-
/// not outlive
88-
/// the hint object that it was created from.
118+
/// @return Hint, as a @ref bsoncxx::v_noabi::types::view. The caller must ensure that the returned object not
119+
/// outlive the hint object that it was created from.
89120
///
90-
operator bsoncxx::v_noabi::types::bson_value::view() const {
91-
return to_value();
121+
operator bsoncxx::v_noabi::types::view() const {
122+
return this->to_value();
92123
}
93124

94125
private:
@@ -104,13 +135,19 @@ class hint {
104135
/// @{
105136

106137
/// @relatesalso mongocxx::v_noabi::hint
107-
MONGOCXX_ABI_EXPORT_CDECL(bool) operator==(std::string index, hint const& index_hint);
138+
inline bool operator==(std::string str, hint const& hint) {
139+
return hint == str;
140+
}
108141

109142
/// @relatesalso mongocxx::v_noabi::hint
110-
MONGOCXX_ABI_EXPORT_CDECL(bool) operator!=(hint const& index_hint, std::string index);
143+
inline bool operator!=(hint const& hint, std::string str) {
144+
return !(hint == str);
145+
}
111146

112147
/// @relatesalso mongocxx::v_noabi::hint
113-
MONGOCXX_ABI_EXPORT_CDECL(bool) operator!=(std::string index, hint const& index_index);
148+
inline bool operator!=(std::string str, hint const& hint) {
149+
return !(hint == str);
150+
}
114151

115152
/// @}
116153
///
@@ -123,25 +160,44 @@ MONGOCXX_ABI_EXPORT_CDECL(bool) operator!=(std::string index, hint const& index_
123160
/// @{
124161

125162
/// @relatesalso mongocxx::v_noabi::hint
126-
MONGOCXX_ABI_EXPORT_CDECL(bool)
127-
operator==(bsoncxx::v_noabi::document::view index, hint const& index_hint);
163+
inline bool operator==(bsoncxx::v_noabi::document::view doc, hint const& hint) {
164+
return hint == doc;
165+
}
166+
128167
/// @relatesalso mongocxx::v_noabi::hint
129-
MONGOCXX_ABI_EXPORT_CDECL(bool)
130-
operator!=(hint const& index_hint, bsoncxx::v_noabi::document::view index);
168+
inline bool operator!=(hint const& hint, bsoncxx::v_noabi::document::view doc) {
169+
return !(hint == doc);
170+
}
171+
131172
/// @relatesalso mongocxx::v_noabi::hint
132-
MONGOCXX_ABI_EXPORT_CDECL(bool)
133-
operator!=(bsoncxx::v_noabi::document::view index, hint const& index_hint);
173+
inline bool operator!=(bsoncxx::v_noabi::document::view doc, hint const& hint) {
174+
return !(hint == doc);
175+
}
134176

135177
/// @}
136178
///
137179

180+
///
181+
/// Convert to the @ref mongocxx::v_noabi equivalent of `v`.
182+
///
183+
inline v_noabi::hint from_v1(v1::hint v) {
184+
return {std::move(v)};
185+
}
186+
187+
///
188+
/// Convert to the @ref mongocxx::v1 equivalent of `v`.
189+
///
190+
inline v1::hint to_v1(v_noabi::hint const& v) {
191+
return v1::hint{v};
192+
}
193+
138194
} // namespace v_noabi
139195
} // namespace mongocxx
140196

141197
namespace mongocxx {
142198

143-
using ::mongocxx::v_noabi::operator==;
144-
using ::mongocxx::v_noabi::operator!=;
199+
using v_noabi::operator==;
200+
using v_noabi::operator!=;
145201

146202
} // namespace mongocxx
147203

@@ -151,3 +207,6 @@ using ::mongocxx::v_noabi::operator!=;
151207
/// @file
152208
/// Provides @ref mongocxx::v_noabi::hint.
153209
///
210+
/// @par Includes
211+
/// - @ref mongocxx/v1/hint.hpp
212+
///

src/mongocxx/lib/mongocxx/v1/hint.cpp

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,110 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <mongocxx/v1/hint.hpp>
15+
#include <mongocxx/v1/hint.hh>
16+
17+
#include <string>
18+
19+
//
20+
21+
#include <bsoncxx/v1/document/value.hpp>
22+
#include <bsoncxx/v1/stdx/optional.hpp>
23+
#include <bsoncxx/v1/stdx/string_view.hpp>
24+
#include <bsoncxx/v1/types/view.hpp>
25+
26+
#include <mongocxx/private/utility.hh>
27+
28+
namespace mongocxx {
29+
namespace v1 {
30+
31+
class hint::impl {
32+
public:
33+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value> _doc;
34+
bsoncxx::v1::stdx::optional<std::string> _str;
35+
36+
static impl const& with(hint const& other) {
37+
return *static_cast<impl const*>(other._impl);
38+
}
39+
40+
static impl const* with(hint const* other) {
41+
return static_cast<impl const*>(other->_impl);
42+
}
43+
44+
static impl& with(hint& other) {
45+
return *static_cast<impl*>(other._impl);
46+
}
47+
48+
static impl* with(hint* other) {
49+
return static_cast<impl*>(other->_impl);
50+
}
51+
52+
static impl* with(void* ptr) {
53+
return static_cast<impl*>(ptr);
54+
}
55+
};
56+
57+
hint::~hint() {
58+
delete impl::with(_impl);
59+
}
60+
61+
hint::hint(hint&& other) noexcept : _impl{exchange(other._impl, nullptr)} {}
62+
63+
hint& hint::operator=(hint&& other) noexcept {
64+
if (this != &other) {
65+
delete impl::with(exchange(_impl, exchange(other._impl, nullptr)));
66+
}
67+
68+
return *this;
69+
}
70+
71+
hint::hint(hint const& other) : _impl{new impl{impl::with(other)}} {}
72+
73+
hint& hint::operator=(hint const& other) {
74+
if (this != &other) {
75+
delete impl::with(exchange(_impl, new impl{impl::with(other)}));
76+
}
77+
78+
return *this;
79+
}
80+
81+
hint::hint() : _impl{new impl{}} {}
82+
83+
hint::hint(std::string str) : hint{} {
84+
impl::with(this)->_str = std::move(str);
85+
}
86+
87+
hint::hint(bsoncxx::v1::document::value doc) : hint{} {
88+
impl::with(this)->_doc = std::move(doc);
89+
}
90+
91+
bsoncxx::v1::stdx::optional<bsoncxx::v1::stdx::string_view> hint::str() const {
92+
return impl::with(this)->_str;
93+
}
94+
95+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::view> hint::doc() const {
96+
return impl::with(this)->_doc;
97+
}
98+
99+
bsoncxx::v1::types::view hint::to_value() const {
100+
if (auto const& opt = impl::with(this)->_doc) {
101+
return bsoncxx::v1::types::b_document{*opt};
102+
}
103+
104+
// Invariant: either `_doc` or `_str` has a value.
105+
return bsoncxx::v1::types::b_string{*impl::with(this)->_str};
106+
}
107+
108+
hint::operator bsoncxx::v1::types::view() const {
109+
return this->to_value();
110+
}
111+
112+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value>& hint::internal::doc(hint& self) {
113+
return impl::with(self)._doc;
114+
}
115+
116+
bsoncxx::v1::stdx::optional<std::string>& hint::internal::str(hint& self) {
117+
return impl::with(self)._str;
118+
}
119+
120+
} // namespace v1
121+
} // namespace mongocxx
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2009-present 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 <mongocxx/v1/hint.hpp> // IWYU pragma: export
18+
19+
//
20+
21+
#include <bsoncxx/v1/document/value-fwd.hpp>
22+
23+
#include <bsoncxx/v1/stdx/optional.hpp>
24+
25+
#include <string>
26+
27+
namespace mongocxx {
28+
namespace v1 {
29+
30+
class hint::internal {
31+
public:
32+
static bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value>& doc(hint& self);
33+
static bsoncxx::v1::stdx::optional<std::string>& str(hint& self);
34+
};
35+
36+
} // namespace v1
37+
} // namespace mongocxx

0 commit comments

Comments
 (0)