Coverage Report

Created: 2026-02-09 06:05

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