hint.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <bsoncxx/document/value.hpp>
  16. #include <bsoncxx/document/view_or_value.hpp>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <bsoncxx/string/view_or_value.hpp>
  19. #include <bsoncxx/types/bson_value/view.hpp>
  20. #include <mongocxx/stdx.hpp>
  21. #include <mongocxx/config/prelude.hpp>
  22. namespace mongocxx {
  23. MONGOCXX_INLINE_NAMESPACE_BEGIN
  24. ///
  25. /// Class representing a hint to be passed to a database operation.
  26. ///
  27. class MONGOCXX_API hint {
  28. public:
  29. ///
  30. /// Constructs a new hint.
  31. ///
  32. /// Note: this constructor is purposefully not explicit, to allow conversion
  33. /// from either document::view or document::value to view_or_value.
  34. ///
  35. /// @param index
  36. /// Document view or value representing the index to be used.
  37. ///
  38. hint(bsoncxx::document::view_or_value index);
  39. ///
  40. /// Constructs a new hint.
  41. ///
  42. /// @param index
  43. /// String representing the name of the index to be used.
  44. ///
  45. explicit hint(bsoncxx::string::view_or_value index);
  46. ///
  47. /// @{
  48. ///
  49. /// Compare this hint to a string for (in)-equality
  50. ///
  51. /// @relates hint
  52. ///
  53. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const hint& index_hint, std::string index);
  54. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const hint& index_hint,
  55. bsoncxx::document::view index);
  56. ///
  57. /// @}
  58. ///
  59. ///
  60. /// Returns a types::bson_value::view representing this hint.
  61. ///
  62. /// @return Hint, as a types::bson_value::view. The caller must ensure that the returned object
  63. /// not outlive
  64. /// the hint object that it was created from.
  65. ///
  66. bsoncxx::types::bson_value::view to_value() const;
  67. ///
  68. /// Returns a types::bson_value::view representing this hint.
  69. ///
  70. /// @return Hint, as a types::bson_value::view. The caller must ensure that the returned object
  71. /// not outlive
  72. /// the hint object that it was created from.
  73. ///
  74. MONGOCXX_INLINE operator bsoncxx::types::bson_value::view() const;
  75. private:
  76. stdx::optional<bsoncxx::document::view_or_value> _index_doc;
  77. stdx::optional<bsoncxx::string::view_or_value> _index_string;
  78. };
  79. ///
  80. /// Convenience methods to compare for equality against an index name.
  81. ///
  82. /// Return true if this hint contains an index name that matches.
  83. ///
  84. /// @relates hint
  85. ///
  86. MONGOCXX_API bool MONGOCXX_CALL operator==(std::string index, const hint& index_hint);
  87. ///
  88. /// @{
  89. ///
  90. /// Convenience methods to compare for inequality against an index name.
  91. ///
  92. /// Return true if this hint contains an index name that matches.
  93. ///
  94. /// @relates hint
  95. ///
  96. MONGOCXX_API bool MONGOCXX_CALL operator!=(const hint& index_hint, std::string index);
  97. MONGOCXX_API bool MONGOCXX_CALL operator!=(std::string index, const hint& index_index);
  98. ///
  99. /// @}
  100. ///
  101. ///
  102. /// Convenience methods to compare for equality against an index document.
  103. ///
  104. /// Return true if this hint contains an index document that matches.
  105. ///
  106. /// @relates hint
  107. ///
  108. MONGOCXX_API bool MONGOCXX_CALL operator==(bsoncxx::document::view index, const hint& index_hint);
  109. ///
  110. /// @{
  111. ///
  112. /// Convenience methods to compare for equality against an index document.
  113. ///
  114. /// Return true if this hint contains an index document that matches.
  115. ///
  116. ///
  117. /// @relates hint
  118. ///
  119. MONGOCXX_API bool MONGOCXX_CALL operator!=(const hint& index_hint, bsoncxx::document::view index);
  120. MONGOCXX_API bool MONGOCXX_CALL operator!=(bsoncxx::document::view index, const hint& index_hint);
  121. ///
  122. /// @}
  123. ///
  124. MONGOCXX_INLINE hint::operator bsoncxx::types::bson_value::view() const {
  125. return to_value();
  126. }
  127. MONGOCXX_INLINE_NAMESPACE_END
  128. } // namespace mongocxx
  129. #include <mongocxx/config/postlude.hpp>