resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmValue.h
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
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <cstddef>
8 #include <iosfwd>
9 #include <string>
10
11 #include <cm/string_view>
12
13 class cmValue
14 {
15 public:
16   cmValue() noexcept = default;
17   cmValue(std::nullptr_t) noexcept {}
18   explicit cmValue(const std::string* value) noexcept
19     : Value(value)
20   {
21   }
22   explicit cmValue(const std::string& value) noexcept
23     : Value(&value)
24   {
25   }
26   cmValue(const cmValue& other) noexcept = default;
27
28   cmValue& operator=(const cmValue& other) noexcept = default;
29   cmValue& operator=(std::nullptr_t) noexcept
30   {
31     this->Value = nullptr;
32     return *this;
33   }
34
35   const std::string* Get() const noexcept { return this->Value; }
36   const char* GetCStr() const noexcept
37   {
38     return this->Value == nullptr ? nullptr : this->Value->c_str();
39   }
40
41   const std::string* operator->() const noexcept
42   {
43     return this->Value == nullptr ? &cmValue::Empty : this->Value;
44   }
45   const std::string& operator*() const noexcept
46   {
47     return this->Value == nullptr ? cmValue::Empty : *this->Value;
48   }
49
50   explicit operator bool() const noexcept { return this->Value != nullptr; }
51   operator const std::string&() const noexcept { return this->operator*(); }
52   explicit operator cm::string_view() const noexcept
53   {
54     return this->operator*();
55   }
56
57   /**
58    * Does the value indicate a true or ON value?
59    */
60   bool IsOn() const noexcept
61   {
62     return this->Value != nullptr &&
63       cmValue::IsOn(cm::string_view(*this->Value));
64   }
65   /**
66    * Does the value indicate a false or off value ? Note that this is
67    * not the same as !IsOn(...) because there are a number of
68    * ambiguous values such as "/usr/local/bin" a path will result in
69    * IsOn and IsOff both returning false. Note that the special path
70    * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
71    */
72   bool IsOff() const noexcept
73   {
74     return this->Value == nullptr ||
75       cmValue::IsOff(cm::string_view(*this->Value));
76   }
77   /** Return true if value is NOTFOUND or ends in -NOTFOUND.  */
78   bool IsNOTFOUND() const noexcept
79   {
80     return this->Value != nullptr &&
81       cmValue::IsNOTFOUND(cm::string_view(*this->Value));
82   }
83   bool IsEmpty() const noexcept
84   {
85     return this->Value == nullptr || this->Value->empty();
86   }
87
88   /**
89    * Does a string indicates that CMake/CPack/CTest internally
90    *  forced this value. This is not the same as On, but this
91    * may be considered as "internally switched on".
92    */
93   bool IsInternallyOn() const noexcept
94   {
95     return this->Value != nullptr &&
96       cmValue::IsInternallyOn(cm::string_view(*this->Value));
97   }
98
99   bool IsSet() const noexcept
100   {
101     return !this->IsEmpty() && !this->IsNOTFOUND();
102   }
103
104   /**
105    * Does a string indicate a true or ON value?
106    */
107   static bool IsOn(const char* value) noexcept
108   {
109     return value != nullptr && IsOn(cm::string_view(value));
110   }
111   static bool IsOn(cm::string_view) noexcept;
112
113   /**
114    * Compare method has same semantic as std::optional::compare
115    */
116   int Compare(cmValue value) const noexcept;
117   int Compare(cm::string_view value) const noexcept;
118
119   /**
120    * Does a string indicate a false or off value ? Note that this is
121    * not the same as !IsOn(...) because there are a number of
122    * ambiguous values such as "/usr/local/bin" a path will result in
123    * IsOn and IsOff both returning false. Note that the special path
124    * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
125    */
126   static bool IsOff(const char* value) noexcept
127   {
128     return value == nullptr || IsOff(cm::string_view(value));
129   }
130   static bool IsOff(cm::string_view) noexcept;
131
132   /** Return true if value is NOTFOUND or ends in -NOTFOUND.  */
133   static bool IsNOTFOUND(const char* value) noexcept
134   {
135     return value == nullptr || IsNOTFOUND(cm::string_view(value));
136   }
137   static bool IsNOTFOUND(cm::string_view) noexcept;
138
139   static bool IsEmpty(const char* value) noexcept
140   {
141     return value == nullptr || *value == '\0';
142   }
143   static bool IsEmpty(cm::string_view value) noexcept { return value.empty(); }
144
145   /**
146    * Does a string indicates that CMake/CPack/CTest internally
147    * forced this value. This is not the same as On, but this
148    * may be considered as "internally switched on".
149    */
150   static bool IsInternallyOn(const char* value) noexcept
151   {
152     return value != nullptr && IsInternallyOn(cm::string_view(value));
153   }
154   static bool IsInternallyOn(cm::string_view) noexcept;
155
156 private:
157   static std::string Empty;
158   const std::string* Value = nullptr;
159 };
160
161 std::ostream& operator<<(std::ostream& o, cmValue v);
162
163 inline bool operator==(cmValue l, cmValue r) noexcept
164 {
165   return l.Compare(r) == 0;
166 }
167 inline bool operator!=(cmValue l, cmValue r) noexcept
168 {
169   return l.Compare(r) != 0;
170 }
171 inline bool operator<(cmValue l, cmValue r) noexcept
172 {
173   return l.Compare(r) < 0;
174 }
175 inline bool operator<=(cmValue l, cmValue r) noexcept
176 {
177   return l.Compare(r) <= 0;
178 }
179 inline bool operator>(cmValue l, cmValue r) noexcept
180 {
181   return l.Compare(r) > 0;
182 }
183 inline bool operator>=(cmValue l, cmValue r) noexcept
184 {
185   return l.Compare(r) >= 0;
186 }
187
188 inline bool operator==(cmValue l, cm::string_view r) noexcept
189 {
190   return l.Compare(r) == 0;
191 }
192 inline bool operator!=(cmValue l, cm::string_view r) noexcept
193 {
194   return l.Compare(r) != 0;
195 }
196 inline bool operator<(cmValue l, cm::string_view r) noexcept
197 {
198   return l.Compare(r) < 0;
199 }
200 inline bool operator<=(cmValue l, cm::string_view r) noexcept
201 {
202   return l.Compare(r) <= 0;
203 }
204 inline bool operator>(cmValue l, cm::string_view r) noexcept
205 {
206   return l.Compare(r) > 0;
207 }
208 inline bool operator>=(cmValue l, cm::string_view r) noexcept
209 {
210   return l.Compare(r) >= 0;
211 }
212
213 inline bool operator==(cmValue l, std::nullptr_t) noexcept
214 {
215   return l.Compare(cmValue{}) == 0;
216 }
217 inline bool operator!=(cmValue l, std::nullptr_t) noexcept
218 {
219   return l.Compare(cmValue{}) != 0;
220 }
221 inline bool operator<(cmValue l, std::nullptr_t) noexcept
222 {
223   return l.Compare(cmValue{}) < 0;
224 }
225 inline bool operator<=(cmValue l, std::nullptr_t) noexcept
226 {
227   return l.Compare(cmValue{}) <= 0;
228 }
229 inline bool operator>(cmValue l, std::nullptr_t) noexcept
230 {
231   return l.Compare(cmValue{}) > 0;
232 }
233 inline bool operator>=(cmValue l, std::nullptr_t) noexcept
234 {
235   return l.Compare(cmValue{}) >= 0;
236 }
237
238 /**
239  * Does a string indicate a true or ON value? This is not the same as ifdef.
240  */
241 inline bool cmIsOn(cm::string_view val)
242 {
243   return cmValue::IsOn(val);
244 }
245 inline bool cmIsOn(const char* val)
246 {
247   return cmValue::IsOn(val);
248 }
249 inline bool cmIsOn(cmValue val)
250 {
251   return val.IsOn();
252 }
253
254 /**
255  * Does a string indicate a false or off value ? Note that this is
256  * not the same as !IsOn(...) because there are a number of
257  * ambiguous values such as "/usr/local/bin" a path will result in
258  * IsON and IsOff both returning false. Note that the special path
259  * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
260  */
261 inline bool cmIsOff(cm::string_view val)
262 {
263   return cmValue::IsOff(val);
264 }
265 inline bool cmIsOff(const char* val)
266 {
267   return cmValue::IsOff(val);
268 }
269 inline bool cmIsOff(cmValue val)
270 {
271   return val.IsOff();
272 }
273
274 /** Return true if value is NOTFOUND or ends in -NOTFOUND.  */
275 inline bool cmIsNOTFOUND(cm::string_view val)
276 {
277   return cmValue::IsNOTFOUND(val);
278 }
279 inline bool cmIsNOTFOUND(cmValue val)
280 {
281   return val.IsNOTFOUND();
282 }
283
284 /** Check for non-empty Property/Variable value.  */
285 inline bool cmNonempty(cm::string_view val)
286 {
287   return !cmValue::IsEmpty(val);
288 }
289 inline bool cmNonempty(const char* val)
290 {
291   return !cmValue::IsEmpty(val);
292 }
293 inline bool cmNonempty(cmValue val)
294 {
295   return !val.IsEmpty();
296 }
297
298 /**
299  * Does a string indicates that CMake/CPack/CTest internally
300  * forced this value. This is not the same as On, but this
301  * may be considered as "internally switched on".
302  */
303 inline bool cmIsInternallyOn(cm::string_view val)
304 {
305   return cmValue::IsInternallyOn(val);
306 }
307 inline bool cmIsInternallyOn(const char* val)
308 {
309   return cmValue::IsInternallyOn(val);
310 }
311 inline bool cmIsInternallyOn(cmValue val)
312 {
313   return val.IsInternallyOn();
314 }