MongoDB C++ Driver  mongocxx-3.10.2
view_or_value.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 <string>
18 
19 #include <bsoncxx/string/view_or_value-fwd.hpp>
20 
21 #include <bsoncxx/stdx/string_view.hpp>
22 #include <bsoncxx/view_or_value.hpp>
23 
24 #include <bsoncxx/config/prelude.hpp>
25 
26 namespace bsoncxx {
27 namespace v_noabi {
28 namespace string {
29 
38 class view_or_value : public bsoncxx::v_noabi::view_or_value<stdx::string_view, std::string> {
39  public:
44 
48  BSONCXX_INLINE view_or_value() = default;
49 
57  BSONCXX_INLINE view_or_value(const char* str)
58  : bsoncxx::v_noabi::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) {}
59 
70  BSONCXX_INLINE view_or_value(const std::string& str)
71  : bsoncxx::v_noabi::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) {}
72 
84 
92  const char* data() const;
93 };
94 
102 BSONCXX_INLINE bool operator==(const view_or_value& lhs, const char* rhs) {
103  return lhs.view() == stdx::string_view(rhs);
104 }
105 
106 BSONCXX_INLINE bool operator!=(const view_or_value& lhs, const char* rhs) {
107  return !(lhs == rhs);
108 }
109 
110 BSONCXX_INLINE bool operator==(const char* lhs, const view_or_value& rhs) {
111  return rhs == lhs;
112 }
113 
114 BSONCXX_INLINE bool operator!=(const char* lhs, const view_or_value& rhs) {
115  return !(rhs == lhs);
116 }
120 
121 } // namespace string
122 } // namespace v_noabi
123 } // namespace bsoncxx
124 
125 namespace bsoncxx {
126 namespace string {
127 
128 using ::bsoncxx::v_noabi::string::operator==;
129 using ::bsoncxx::v_noabi::string::operator!=;
130 
131 } // namespace string
132 } // namespace bsoncxx
133 
134 #include <bsoncxx/config/postlude.hpp>
Class representing a view-or-value variant type for strings.
Definition: view_or_value.hpp:38
view_or_value()=default
Default constructor, equivalent to using an empty string.
view_or_value(const std::string &str)
Allow construction with an l-value reference to a std::string.
Definition: view_or_value.hpp:70
bool operator==(const view_or_value &lhs, const char *rhs)
Comparison operators for comparing string::view_or_value directly with const char *.
Definition: view_or_value.hpp:102
const char * data() const
Call data() on this view_or_value's string_view.
view_or_value(const char *str)
Construct a string::view_or_value using a null-terminated const char *.
Definition: view_or_value.hpp:57
view_or_value terminated() const
Return a string_view_or_value that is guaranteed to hold a null-terminated string.
Class representing a view-or-value variant type.
Definition: view_or_value.hpp:32
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:133
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19