cmValue.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <iosfwd>
  7. #include <string>
  8. #include <cm/string_view>
  9. class cmValue
  10. {
  11. public:
  12. cmValue() noexcept = default;
  13. cmValue(std::nullptr_t) noexcept {}
  14. explicit cmValue(const std::string* value) noexcept
  15. : Value(value)
  16. {
  17. }
  18. explicit cmValue(const std::string& value) noexcept
  19. : Value(&value)
  20. {
  21. }
  22. cmValue(const cmValue& other) noexcept = default;
  23. cmValue& operator=(const cmValue& other) noexcept = default;
  24. cmValue& operator=(std::nullptr_t) noexcept
  25. {
  26. this->Value = nullptr;
  27. return *this;
  28. }
  29. const std::string* Get() const noexcept { return this->Value; }
  30. const char* GetCStr() const noexcept
  31. {
  32. return this->Value == nullptr ? nullptr : this->Value->c_str();
  33. }
  34. const std::string* operator->() const noexcept
  35. {
  36. return this->Value == nullptr ? &cmValue::Empty : this->Value;
  37. }
  38. const std::string& operator*() const noexcept
  39. {
  40. return this->Value == nullptr ? cmValue::Empty : *this->Value;
  41. }
  42. explicit operator bool() const noexcept { return this->Value != nullptr; }
  43. operator const std::string&() const noexcept { return this->operator*(); }
  44. explicit operator cm::string_view() const noexcept
  45. {
  46. return this->operator*();
  47. }
  48. /**
  49. * Does the value indicate a true or ON value?
  50. */
  51. bool IsOn() const noexcept
  52. {
  53. return this->Value != nullptr &&
  54. cmValue::IsOn(cm::string_view(*this->Value));
  55. }
  56. /**
  57. * Does the value indicate a false or off value ? Note that this is
  58. * not the same as !IsOn(...) because there are a number of
  59. * ambiguous values such as "/usr/local/bin" a path will result in
  60. * IsOn and IsOff both returning false. Note that the special path
  61. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  62. */
  63. bool IsOff() const noexcept
  64. {
  65. return this->Value == nullptr ||
  66. cmValue::IsOff(cm::string_view(*this->Value));
  67. }
  68. /** Return true if value is NOTFOUND or ends in -NOTFOUND. */
  69. bool IsNOTFOUND() const noexcept
  70. {
  71. return this->Value != nullptr &&
  72. cmValue::IsNOTFOUND(cm::string_view(*this->Value));
  73. }
  74. bool IsEmpty() const noexcept
  75. {
  76. return this->Value == nullptr || this->Value->empty();
  77. }
  78. /**
  79. * Does a string indicates that CMake/CPack/CTest internally
  80. * forced this value. This is not the same as On, but this
  81. * may be considered as "internally switched on".
  82. */
  83. bool IsInternallyOn() const noexcept
  84. {
  85. return this->Value != nullptr &&
  86. cmValue::IsInternallyOn(cm::string_view(*this->Value));
  87. }
  88. bool IsSet() const noexcept
  89. {
  90. return !this->IsEmpty() && !this->IsNOTFOUND();
  91. }
  92. /**
  93. * Does a string indicate a true or ON value?
  94. */
  95. static bool IsOn(const char* value) noexcept
  96. {
  97. return value != nullptr && IsOn(cm::string_view(value));
  98. }
  99. static bool IsOn(cm::string_view) noexcept;
  100. /**
  101. * Compare method has same semantic as std::optional::compare
  102. */
  103. int Compare(cmValue value) const noexcept;
  104. int Compare(cm::string_view value) const noexcept;
  105. /**
  106. * Does a string indicate a false or off value ? Note that this is
  107. * not the same as !IsOn(...) because there are a number of
  108. * ambiguous values such as "/usr/local/bin" a path will result in
  109. * IsOn and IsOff both returning false. Note that the special path
  110. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  111. */
  112. static bool IsOff(const char* value) noexcept
  113. {
  114. return value == nullptr || IsOff(cm::string_view(value));
  115. }
  116. static bool IsOff(cm::string_view) noexcept;
  117. /** Return true if value is NOTFOUND or ends in -NOTFOUND. */
  118. static bool IsNOTFOUND(const char* value) noexcept
  119. {
  120. return value == nullptr || IsNOTFOUND(cm::string_view(value));
  121. }
  122. static bool IsNOTFOUND(cm::string_view) noexcept;
  123. static bool IsEmpty(const char* value) noexcept
  124. {
  125. return value == nullptr || *value == '\0';
  126. }
  127. static bool IsEmpty(cm::string_view value) noexcept { return value.empty(); }
  128. /**
  129. * Does a string indicates that CMake/CPack/CTest internally
  130. * forced this value. This is not the same as On, but this
  131. * may be considered as "internally switched on".
  132. */
  133. static bool IsInternallyOn(const char* value) noexcept
  134. {
  135. return value != nullptr && IsInternallyOn(cm::string_view(value));
  136. }
  137. static bool IsInternallyOn(cm::string_view) noexcept;
  138. private:
  139. static std::string Empty;
  140. const std::string* Value = nullptr;
  141. };
  142. std::ostream& operator<<(std::ostream& o, cmValue v);
  143. inline bool operator==(cmValue l, cmValue r) noexcept
  144. {
  145. return l.Compare(r) == 0;
  146. }
  147. inline bool operator!=(cmValue l, cmValue r) noexcept
  148. {
  149. return l.Compare(r) != 0;
  150. }
  151. inline bool operator<(cmValue l, cmValue r) noexcept
  152. {
  153. return l.Compare(r) < 0;
  154. }
  155. inline bool operator<=(cmValue l, cmValue r) noexcept
  156. {
  157. return l.Compare(r) <= 0;
  158. }
  159. inline bool operator>(cmValue l, cmValue r) noexcept
  160. {
  161. return l.Compare(r) > 0;
  162. }
  163. inline bool operator>=(cmValue l, cmValue r) noexcept
  164. {
  165. return l.Compare(r) >= 0;
  166. }
  167. inline bool operator==(cmValue l, cm::string_view r) noexcept
  168. {
  169. return l.Compare(r) == 0;
  170. }
  171. inline bool operator!=(cmValue l, cm::string_view r) noexcept
  172. {
  173. return l.Compare(r) != 0;
  174. }
  175. inline bool operator<(cmValue l, cm::string_view r) noexcept
  176. {
  177. return l.Compare(r) < 0;
  178. }
  179. inline bool operator<=(cmValue l, cm::string_view r) noexcept
  180. {
  181. return l.Compare(r) <= 0;
  182. }
  183. inline bool operator>(cmValue l, cm::string_view r) noexcept
  184. {
  185. return l.Compare(r) > 0;
  186. }
  187. inline bool operator>=(cmValue l, cm::string_view r) noexcept
  188. {
  189. return l.Compare(r) >= 0;
  190. }
  191. inline bool operator==(cmValue l, std::nullptr_t) noexcept
  192. {
  193. return l.Compare(cmValue{}) == 0;
  194. }
  195. inline bool operator!=(cmValue l, std::nullptr_t) noexcept
  196. {
  197. return l.Compare(cmValue{}) != 0;
  198. }
  199. inline bool operator<(cmValue l, std::nullptr_t) noexcept
  200. {
  201. return l.Compare(cmValue{}) < 0;
  202. }
  203. inline bool operator<=(cmValue l, std::nullptr_t) noexcept
  204. {
  205. return l.Compare(cmValue{}) <= 0;
  206. }
  207. inline bool operator>(cmValue l, std::nullptr_t) noexcept
  208. {
  209. return l.Compare(cmValue{}) > 0;
  210. }
  211. inline bool operator>=(cmValue l, std::nullptr_t) noexcept
  212. {
  213. return l.Compare(cmValue{}) >= 0;
  214. }