Coverage Report

Created: 2026-10-28 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmValue.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst 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
  static cmValue True;
17
  static cmValue False;
18
19
213k
  cmValue() noexcept = default;
20
14.2M
  cmValue(std::nullptr_t) noexcept {}
21
  explicit cmValue(std::string const* value) noexcept
22
506k
    : Value(value)
23
506k
  {
24
506k
  }
25
  explicit cmValue(std::string const& value) noexcept
26
4.61M
    : Value(&value)
27
4.61M
  {
28
4.61M
  }
29
  cmValue(cmValue const& other) noexcept = default;
30
31
  cmValue& operator=(cmValue const& other) noexcept = default;
32
  cmValue& operator=(std::nullptr_t) noexcept
33
4.09k
  {
34
4.09k
    this->Value = nullptr;
35
4.09k
    return *this;
36
4.09k
  }
37
38
0
  std::string const* Get() const noexcept { return this->Value; }
39
  char const* GetCStr() const noexcept
40
6.40M
  {
41
6.40M
    return this->Value ? this->Value->c_str() : nullptr;
42
6.40M
  }
43
44
  std::string const* operator->() const noexcept
45
11.8k
  {
46
11.8k
    return this->Value ? this->Value : &cmValue::Empty;
47
11.8k
  }
48
  std::string const& operator*() const noexcept
49
1.98M
  {
50
1.98M
    return this->Value ? *this->Value : cmValue::Empty;
51
1.98M
  }
52
53
19.5M
  explicit operator bool() const noexcept { return this->Value != nullptr; }
54
1.38M
  operator std::string const&() const noexcept { return this->operator*(); }
55
  explicit operator cm::string_view() const noexcept
56
0
  {
57
0
    return this->operator*();
58
0
  }
59
60
  /**
61
   * Does the value indicate a true or ON value?
62
   */
63
  bool IsOn() const noexcept
64
194k
  {
65
194k
    return this->Value && cmValue::IsOn(cm::string_view(*this->Value));
66
194k
  }
67
  /**
68
   * Does the value indicate a false or off value ? Note that this is
69
   * not the same as !IsOn(...) because there are a number of
70
   * ambiguous values such as "/usr/local/bin" a path will result in
71
   * IsOn and IsOff both returning false. Note that the special path
72
   * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
73
   */
74
  bool IsOff() const noexcept
75
47.4k
  {
76
47.4k
    return !this->Value || cmValue::IsOff(cm::string_view(*this->Value));
77
47.4k
  }
78
  /** Return true if value is NOTFOUND or ends in -NOTFOUND.  */
79
  bool IsNOTFOUND() const noexcept
80
0
  {
81
0
    return this->Value && cmValue::IsNOTFOUND(cm::string_view(*this->Value));
82
0
  }
83
  bool IsEmpty() const noexcept
84
203k
  {
85
203k
    return !this->Value || this->Value->empty();
86
203k
  }
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
0
  {
95
0
    return this->Value &&
96
0
      cmValue::IsInternallyOn(cm::string_view(*this->Value));
97
0
  }
98
99
  bool IsSet() const noexcept
100
555
  {
101
555
    return !this->IsEmpty() && !this->IsNOTFOUND();
102
555
  }
103
104
  /**
105
   * Does a string indicate a true or ON value?
106
   */
107
  static bool IsOn(char const* value) noexcept
108
0
  {
109
0
    return value && IsOn(cm::string_view(value));
110
0
  }
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(char const* value) noexcept
127
0
  {
128
0
    return !value || IsOff(cm::string_view(value));
129
0
  }
130
  static bool IsOff(cm::string_view) noexcept;
131
132
  /** Return true if value is NOTFOUND or ends in -NOTFOUND.  */
133
  static bool IsNOTFOUND(char const* value) noexcept
134
0
  {
135
0
    return !value || IsNOTFOUND(cm::string_view(value));
136
0
  }
137
  static bool IsNOTFOUND(cm::string_view) noexcept;
138
139
  static constexpr bool IsEmpty(char const* value) noexcept
140
1.13k
  {
141
1.13k
    return !value || *value == '\0';
142
1.13k
  }
143
  static constexpr bool IsEmpty(cm::string_view value) noexcept
144
0
  {
145
0
    return value.empty();
146
0
  }
147
148
  /**
149
   * Does a string indicates that CMake/CPack/CTest internally
150
   * forced this value. This is not the same as On, but this
151
   * may be considered as "internally switched on".
152
   */
153
  static bool IsInternallyOn(char const* value) noexcept
154
0
  {
155
0
    return value && IsInternallyOn(cm::string_view(value));
156
0
  }
157
  static bool IsInternallyOn(cm::string_view) noexcept;
158
159
private:
160
  static std::string Empty;
161
  std::string const* Value = nullptr;
162
};
163
164
std::ostream& operator<<(std::ostream& o, cmValue v);
165
166
inline bool operator==(cmValue l, cmValue r) noexcept
167
0
{
168
0
  return l.Compare(r) == 0;
169
0
}
170
inline bool operator!=(cmValue l, cmValue r) noexcept
171
0
{
172
0
  return l.Compare(r) != 0;
173
0
}
174
inline bool operator<(cmValue l, cmValue r) noexcept
175
0
{
176
0
  return l.Compare(r) < 0;
177
0
}
178
inline bool operator<=(cmValue l, cmValue r) noexcept
179
0
{
180
0
  return l.Compare(r) <= 0;
181
0
}
182
inline bool operator>(cmValue l, cmValue r) noexcept
183
0
{
184
0
  return l.Compare(r) > 0;
185
0
}
186
inline bool operator>=(cmValue l, cmValue r) noexcept
187
0
{
188
0
  return l.Compare(r) >= 0;
189
0
}
190
191
inline bool operator==(cmValue l, cm::string_view r) noexcept
192
1.62k
{
193
1.62k
  return l.Compare(r) == 0;
194
1.62k
}
195
inline bool operator!=(cmValue l, cm::string_view r) noexcept
196
0
{
197
0
  return l.Compare(r) != 0;
198
0
}
199
inline bool operator<(cmValue l, cm::string_view r) noexcept
200
0
{
201
0
  return l.Compare(r) < 0;
202
0
}
203
inline bool operator<=(cmValue l, cm::string_view r) noexcept
204
0
{
205
0
  return l.Compare(r) <= 0;
206
0
}
207
inline bool operator>(cmValue l, cm::string_view r) noexcept
208
0
{
209
0
  return l.Compare(r) > 0;
210
0
}
211
inline bool operator>=(cmValue l, cm::string_view r) noexcept
212
0
{
213
0
  return l.Compare(r) >= 0;
214
0
}
215
216
inline bool operator==(cmValue l, std::nullptr_t) noexcept
217
0
{
218
0
  return l.Compare(cmValue{}) == 0;
219
0
}
220
inline bool operator!=(cmValue l, std::nullptr_t) noexcept
221
5.92k
{
222
5.92k
  return l.Compare(cmValue{}) != 0;
223
5.92k
}
224
inline bool operator<(cmValue l, std::nullptr_t) noexcept
225
0
{
226
0
  return l.Compare(cmValue{}) < 0;
227
0
}
228
inline bool operator<=(cmValue l, std::nullptr_t) noexcept
229
0
{
230
0
  return l.Compare(cmValue{}) <= 0;
231
0
}
232
inline bool operator>(cmValue l, std::nullptr_t) noexcept
233
0
{
234
0
  return l.Compare(cmValue{}) > 0;
235
0
}
236
inline bool operator>=(cmValue l, std::nullptr_t) noexcept
237
0
{
238
0
  return l.Compare(cmValue{}) >= 0;
239
0
}
240
241
/**
242
 * Does a string indicate a true or ON value? This is not the same as ifdef.
243
 */
244
inline bool cmIsOn(cm::string_view val)
245
5.92k
{
246
5.92k
  return cmValue::IsOn(val);
247
5.92k
}
248
inline bool cmIsOn(char const* val)
249
0
{
250
0
  return cmValue::IsOn(val);
251
0
}
252
inline bool cmIsOn(cmValue val)
253
0
{
254
0
  return val.IsOn();
255
0
}
256
257
/**
258
 * Does a string indicate a false or off value ? Note that this is
259
 * not the same as !IsOn(...) because there are a number of
260
 * ambiguous values such as "/usr/local/bin" a path will result in
261
 * IsON and IsOff both returning false. Note that the special path
262
 * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
263
 */
264
inline bool cmIsOff(cm::string_view val)
265
209
{
266
209
  return cmValue::IsOff(val);
267
209
}
268
inline bool cmIsOff(char const* val)
269
0
{
270
0
  return cmValue::IsOff(val);
271
0
}
272
inline bool cmIsOff(cmValue val)
273
0
{
274
0
  return val.IsOff();
275
0
}
276
277
/** Return true if value is NOTFOUND or ends in -NOTFOUND.  */
278
inline bool cmIsNOTFOUND(cm::string_view val)
279
11.8k
{
280
11.8k
  return cmValue::IsNOTFOUND(val);
281
11.8k
}
282
inline bool cmIsNOTFOUND(cmValue val)
283
0
{
284
0
  return val.IsNOTFOUND();
285
0
}
286
287
/** Check for non-empty Property/Variable value.  */
288
inline bool cmNonempty(cm::string_view val)
289
0
{
290
0
  return !cmValue::IsEmpty(val);
291
0
}
292
inline bool cmNonempty(char const* val)
293
1.13k
{
294
1.13k
  return !cmValue::IsEmpty(val);
295
1.13k
}
296
inline bool cmNonempty(cmValue val)
297
185k
{
298
185k
  return !val.IsEmpty();
299
185k
}
300
301
/**
302
 * Does a string indicates that CMake/CPack/CTest internally
303
 * forced this value. This is not the same as On, but this
304
 * may be considered as "internally switched on".
305
 */
306
inline bool cmIsInternallyOn(cm::string_view val)
307
0
{
308
0
  return cmValue::IsInternallyOn(val);
309
0
}
310
inline bool cmIsInternallyOn(char const* val)
311
0
{
312
0
  return cmValue::IsInternallyOn(val);
313
0
}
314
inline bool cmIsInternallyOn(cmValue val)
315
0
{
316
0
  return val.IsInternallyOn();
317
0
}