view_or_value.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #pragma once
  15. #include <string>
  16. #include <bsoncxx/stdx/string_view.hpp>
  17. #include <bsoncxx/view_or_value.hpp>
  18. #include <bsoncxx/config/prelude.hpp>
  19. namespace bsoncxx {
  20. BSONCXX_INLINE_NAMESPACE_BEGIN
  21. namespace string {
  22. ///
  23. /// Class representing a view-or-value variant type for strings.
  24. ///
  25. /// This class adds several string-specific methods to the bsoncxx::view_or_value template:
  26. /// - a constructor overload for const char*
  27. /// - a constructor overload for std::string by l-value reference
  28. /// - a safe c_str() operation to return null-terminated c-style strings.
  29. ///
  30. class BSONCXX_API view_or_value : public bsoncxx::view_or_value<stdx::string_view, std::string> {
  31. public:
  32. ///
  33. /// Forward all bsoncxx::view_or_value constructors.
  34. ///
  35. using bsoncxx::view_or_value<stdx::string_view, std::string>::view_or_value;
  36. ///
  37. /// Default constructor, equivalent to using an empty string.
  38. ///
  39. BSONCXX_INLINE view_or_value() = default;
  40. ///
  41. /// Construct a string::view_or_value using a null-terminated const char *.
  42. /// The resulting view_or_value will keep a string_view of 'str', so it is
  43. /// important that the passed-in string outlive this object.
  44. ///
  45. /// @param str A null-terminated string
  46. ///
  47. BSONCXX_INLINE view_or_value(const char* str)
  48. : bsoncxx::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) {}
  49. ///
  50. /// Allow construction with an l-value reference to a std::string. The resulting
  51. /// view_or_value will keep a string_view of 'str', so it is important that the
  52. /// passed-in string outlive this object.
  53. ///
  54. /// Construction calls passing a std::string by r-value reference will use the
  55. /// constructor defined in the parent view_or_value class.
  56. ///
  57. /// @param str A std::string l-value reference.
  58. ///
  59. BSONCXX_INLINE view_or_value(const std::string& str)
  60. : bsoncxx::view_or_value<stdx::string_view, std::string>(stdx::string_view(str)) {}
  61. ///
  62. /// Return a string_view_or_value that is guaranteed to hold a null-terminated
  63. /// string. The lifetime of the returned object must be a subset of this object's
  64. /// lifetime, because the new view_or_value might hold a view into this one.
  65. ///
  66. /// It is recommended that this method be used before calling .data() on a
  67. /// view_or_value, as that method may return a non-null-terminated string.
  68. ///
  69. /// @return A new view_or_value object.
  70. ///
  71. view_or_value terminated() const;
  72. ///
  73. /// Call data() on this view_or_value's string_view. This method is not
  74. /// guaranteed to return a null-terminated string unless it is used in
  75. /// combination with terminated().
  76. ///
  77. /// @return A const char* of this string.
  78. ///
  79. const char* data() const;
  80. };
  81. ///
  82. /// @{
  83. ///
  84. /// Comparison operators for comparing string::view_or_value directly with const char *.
  85. ///
  86. /// @relates view_or_value
  87. ///
  88. BSONCXX_INLINE bool operator==(const view_or_value& lhs, const char* rhs) {
  89. return lhs.view() == stdx::string_view(rhs);
  90. }
  91. BSONCXX_INLINE bool operator!=(const view_or_value& lhs, const char* rhs) {
  92. return !(lhs == rhs);
  93. }
  94. BSONCXX_INLINE bool operator==(const char* lhs, const view_or_value& rhs) {
  95. return rhs == lhs;
  96. }
  97. BSONCXX_INLINE bool operator!=(const char* lhs, const view_or_value& rhs) {
  98. return !(rhs == lhs);
  99. }
  100. ///
  101. /// @}
  102. ///
  103. } // namespace string
  104. BSONCXX_INLINE_NAMESPACE_END
  105. } // namespace bsoncxx
  106. #include <bsoncxx/config/postlude.hpp>