cmProperty.h 5.6 KB

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