MongoDB C++ Driver  mongocxx-3.4.0
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/stdx/string_view.hpp>
20 #include <bsoncxx/view_or_value.hpp>
21 
22 #include <bsoncxx/config/prelude.hpp>
23 
24 namespace bsoncxx {
25 BSONCXX_INLINE_NAMESPACE_BEGIN
26 namespace string {
27 
36 class BSONCXX_API view_or_value : public bsoncxx::view_or_value<stdx::string_view, std::string> {
37  public:
42 
46  BSONCXX_INLINE view_or_value() = default;
47 
55  BSONCXX_INLINE view_or_value(const char* str)
56  : bsoncxx::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) {}
57 
68  BSONCXX_INLINE view_or_value(const std::string& str)
69  : bsoncxx::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) {}
70 
81  view_or_value terminated() const;
82 
90  const char* data() const;
91 };
92 
100 BSONCXX_INLINE bool operator==(const view_or_value& lhs, const char* rhs) {
101  return lhs.view() == stdx::string_view(rhs);
102 }
103 
104 BSONCXX_INLINE bool operator!=(const view_or_value& lhs, const char* rhs) {
105  return !(lhs == rhs);
106 }
107 
108 BSONCXX_INLINE bool operator==(const char* lhs, const view_or_value& rhs) {
109  return rhs == lhs;
110 }
111 
112 BSONCXX_INLINE bool operator!=(const char* lhs, const view_or_value& rhs) {
113  return !(rhs == lhs);
114 }
118 
119 } // namespace string
120 BSONCXX_INLINE_NAMESPACE_END
121 } // namespace bsoncxx
122 
123 #include <bsoncxx/config/postlude.hpp>
view_or_value(const char *str)
Construct a string::view_or_value using a null-terminated const char *.
Definition: view_or_value.hpp:55
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:132
Definition: error_code.hpp:117
view_or_value(const std::string &str)
Allow construction with an l-value reference to a std::string.
Definition: view_or_value.hpp:68
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:100
Class representing a view-or-value variant type.
Definition: view_or_value.hpp:30
Class representing a view-or-value variant type for strings.
Definition: view_or_value.hpp:36
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24