|
| 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 <bsoncxx/config/prelude.hpp> |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include <bsoncxx/stdx/string_view.hpp> |
| 22 | +#include <bsoncxx/view_or_value.hpp> |
| 23 | + |
| 24 | +namespace bsoncxx { |
| 25 | +BSONCXX_INLINE_NAMESPACE_BEGIN |
| 26 | +namespace string { |
| 27 | + |
| 28 | +/// |
| 29 | +/// Class representing a view-or-value variant type for strings. |
| 30 | +/// |
| 31 | +/// This class adds several string-specific methods to the bsoncxx::view_or_value template: |
| 32 | +/// - a constructor overload for const char* |
| 33 | +/// - a constructor overload for std::string by l-value reference |
| 34 | +/// - a safe c_str() operation to return null-terminated c-style strings. |
| 35 | +/// |
| 36 | +class BSONCXX_API view_or_value : public bsoncxx::view_or_value<stdx::string_view, std::string> { |
| 37 | + public: |
| 38 | + /// |
| 39 | + /// Forward all bsoncxx::view_or_value constructors. |
| 40 | + /// |
| 41 | + using bsoncxx::view_or_value<stdx::string_view, std::string>::view_or_value; |
| 42 | + |
| 43 | + /// |
| 44 | + /// Construct a string::view_or_value using a null-terminated const char *. |
| 45 | + /// The resulting view_or_value will keep a string_view of 'str', so it is |
| 46 | + /// important that the passed-in string outlive this object. |
| 47 | + /// |
| 48 | + /// @param str A null-terminated string |
| 49 | + /// |
| 50 | + BSONCXX_INLINE view_or_value(const char* str) |
| 51 | + : bsoncxx::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) { |
| 52 | + } |
| 53 | + |
| 54 | + /// |
| 55 | + /// Allow construction with an l-value reference to a std::string. The resulting |
| 56 | + /// view_or_value will keep a string_view of 'str', so it is important that the |
| 57 | + /// passed-in string outlive this object. |
| 58 | + /// |
| 59 | + /// Construction calls passing a std::string by r-value reference will use the |
| 60 | + /// constructor defined in the parent view_or_value class. |
| 61 | + /// |
| 62 | + /// @param str A std::string l-value reference. |
| 63 | + /// |
| 64 | + BSONCXX_INLINE view_or_value(const std::string& str) |
| 65 | + : bsoncxx::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) { |
| 66 | + } |
| 67 | + |
| 68 | + /// |
| 69 | + /// Return a null-terminated string from this string::view_or_value. |
| 70 | + /// |
| 71 | + /// @note if we are non-owning, we cannot assume our string_view is null-terminated. |
| 72 | + /// In this case we must make a new, owning view_or_value with a null-terminated |
| 73 | + /// copy of our view. |
| 74 | + /// |
| 75 | + /// @return A pointer to the original string, or a null-terminated copy. |
| 76 | + /// |
| 77 | + const char* c_str(); |
| 78 | +}; |
| 79 | + |
| 80 | +/// |
| 81 | +/// Compare string::view_or_value directly with const char *. |
| 82 | +/// |
| 83 | + |
| 84 | +BSONCXX_INLINE bool operator==(const view_or_value& lhs, const char* rhs) { |
| 85 | + return lhs.view() == stdx::string_view(rhs); |
| 86 | +} |
| 87 | + |
| 88 | +BSONCXX_INLINE bool operator!=(const view_or_value& lhs, const char* rhs) { |
| 89 | + return !(lhs == rhs); |
| 90 | +} |
| 91 | + |
| 92 | +BSONCXX_INLINE bool operator==(const char* lhs, const view_or_value& rhs) { |
| 93 | + return rhs == lhs; |
| 94 | +} |
| 95 | + |
| 96 | +BSONCXX_INLINE bool operator!=(const char* lhs, const view_or_value& rhs) { |
| 97 | + return !(rhs == lhs); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace string |
| 101 | +BSONCXX_INLINE_NAMESPACE_END |
| 102 | +} // namespace bsoncxx |
| 103 | + |
| 104 | +#include <bsoncxx/config/postlude.hpp> |
0 commit comments