diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2021-08-04 13:08:17 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2021-08-08 14:19:08 (GMT) |
commit | e5cd39ca80c8c234118ded33a1576c1be281aa9e (patch) | |
tree | 0494dad04188959dd6cefc9497688718ff88006b /Source/cmProperty.h | |
parent | 350065bb855a26692ed6af04870f144ae1f6a886 (diff) | |
download | CMake-e5cd39ca80c8c234118ded33a1576c1be281aa9e.zip CMake-e5cd39ca80c8c234118ded33a1576c1be281aa9e.tar.gz CMake-e5cd39ca80c8c234118ded33a1576c1be281aa9e.tar.bz2 |
cmProp: refactoring: transform alias in class
To handle safely the values used by CMake variables and properties,
introduce the class cmProp as a replacement from the simple pointer
to std::string instance.
Diffstat (limited to 'Source/cmProperty.h')
-rw-r--r-- | Source/cmProperty.h | 210 |
1 files changed, 205 insertions, 5 deletions
diff --git a/Source/cmProperty.h b/Source/cmProperty.h index 1e03c3f..3a0a5be 100644 --- a/Source/cmProperty.h +++ b/Source/cmProperty.h @@ -4,8 +4,12 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include <cstddef> +#include <iosfwd> #include <string> +#include <cm/string_view> + class cmProperty { public: @@ -23,14 +27,210 @@ public: }; }; -using cmProp = const std::string*; +class cmProp +{ +public: + cmProp() noexcept = default; + cmProp(std::nullptr_t) noexcept {} + explicit cmProp(const std::string* value) noexcept + : Value(value) + { + } + explicit cmProp(const std::string& value) noexcept + : Value(&value) + { + } + cmProp(const cmProp& other) noexcept = default; -inline const char* cmToCStr(cmProp p) + cmProp& operator=(const cmProp& other) noexcept = default; + cmProp& operator=(std::nullptr_t) noexcept + { + this->Value = nullptr; + return *this; + } + + const std::string* Get() const noexcept { return this->Value; } + const char* GetCStr() const noexcept + { + return this->Value == nullptr ? nullptr : this->Value->c_str(); + } + + const std::string* operator->() const noexcept + { + return this->Value == nullptr ? &cmProp::Empty : this->Value; + } + const std::string& operator*() const noexcept + { + return this->Value == nullptr ? cmProp::Empty : *this->Value; + } + + explicit operator bool() const noexcept { return this->Value != nullptr; } + operator const std::string&() const noexcept { return this->operator*(); } + operator cm::string_view() const noexcept { return this->operator*(); } + + /** + * Does the value indicate a true or ON value? + */ + bool IsOn() const noexcept + { + return this->Value != nullptr && + cmProp::IsOn(cm::string_view(*this->Value)); + } + /** + * Does the value indicate a false or off value ? Note that this is + * not the same as !IsOn(...) because there are a number of + * ambiguous values such as "/usr/local/bin" a path will result in + * IsOn and IsOff both returning false. Note that the special path + * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true. + */ + bool IsOff() const noexcept + { + return this->Value == nullptr || + cmProp::IsOff(cm::string_view(*this->Value)); + } + /** Return true if value is NOTFOUND or ends in -NOTFOUND. */ + bool IsNOTFOUND() const noexcept + { + return this->Value != nullptr && + cmProp::IsNOTFOUND(cm::string_view(*this->Value)); + } + bool IsEmpty() const noexcept + { + return this->Value == nullptr || this->Value->empty(); + } + + bool IsSet() const noexcept + { + return !this->IsEmpty() && !this->IsNOTFOUND(); + } + + /** + * Does a string indicate a true or ON value? + */ + static bool IsOn(const char* value) noexcept + { + return value != nullptr && IsOn(cm::string_view(value)); + } + static bool IsOn(cm::string_view) noexcept; + + /** + * Compare method has same semantic as std::optional::compare + */ + int Compare(cmProp value) const noexcept; + int Compare(cm::string_view value) const noexcept; + + /** + * Does a string indicate a false or off value ? Note that this is + * not the same as !IsOn(...) because there are a number of + * ambiguous values such as "/usr/local/bin" a path will result in + * IsOn and IsOff both returning false. Note that the special path + * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true. + */ + static bool IsOff(const char* value) noexcept + { + return value == nullptr || IsOff(cm::string_view(value)); + } + static bool IsOff(cm::string_view) noexcept; + + /** Return true if value is NOTFOUND or ends in -NOTFOUND. */ + static bool IsNOTFOUND(const char* value) noexcept + { + return value == nullptr || IsNOTFOUND(cm::string_view(value)); + } + static bool IsNOTFOUND(cm::string_view) noexcept; + + static bool IsEmpty(const char* value) noexcept + { + return value == nullptr || *value == '\0'; + } + static bool IsEmpty(cm::string_view value) noexcept { return value.empty(); } + +private: + static std::string Empty; + const std::string* Value = nullptr; +}; + +std::ostream& operator<<(std::ostream& o, cmProp v); + +inline bool operator==(cmProp l, cmProp r) noexcept +{ + return l.Compare(r) == 0; +} +inline bool operator!=(cmProp l, cmProp r) noexcept { - return p ? p->c_str() : nullptr; + return l.Compare(r) != 0; +} +inline bool operator<(cmProp l, cmProp r) noexcept +{ + return l.Compare(r) < 0; +} +inline bool operator<=(cmProp l, cmProp r) noexcept +{ + return l.Compare(r) <= 0; +} +inline bool operator>(cmProp l, cmProp r) noexcept +{ + return l.Compare(r) > 0; +} +inline bool operator>=(cmProp l, cmProp r) noexcept +{ + return l.Compare(r) >= 0; } -inline const char* cmToCStrSafe(cmProp p) +inline bool operator==(cmProp l, cm::string_view r) noexcept +{ + return l.Compare(r) == 0; +} +inline bool operator!=(cmProp l, cm::string_view r) noexcept +{ + return l.Compare(r) != 0; +} +inline bool operator<(cmProp l, cm::string_view r) noexcept +{ + return l.Compare(r) < 0; +} +inline bool operator<=(cmProp l, cm::string_view r) noexcept +{ + return l.Compare(r) <= 0; +} +inline bool operator>(cmProp l, cm::string_view r) noexcept +{ + return l.Compare(r) > 0; +} +inline bool operator>=(cmProp l, cm::string_view r) noexcept +{ + return l.Compare(r) >= 0; +} + +inline bool operator==(cmProp l, std::nullptr_t) noexcept +{ + return l.Compare(cmProp{}) == 0; +} +inline bool operator!=(cmProp l, std::nullptr_t) noexcept +{ + return l.Compare(cmProp{}) != 0; +} +inline bool operator<(cmProp l, std::nullptr_t) noexcept +{ + return l.Compare(cmProp{}) < 0; +} +inline bool operator<=(cmProp l, std::nullptr_t) noexcept +{ + return l.Compare(cmProp{}) <= 0; +} +inline bool operator>(cmProp l, std::nullptr_t) noexcept +{ + return l.Compare(cmProp{}) > 0; +} +inline bool operator>=(cmProp l, std::nullptr_t) noexcept +{ + return l.Compare(cmProp{}) >= 0; +} + +/** + * Temporary wrapper + */ +inline const char* cmToCStr(cmProp p) { - return p ? p->c_str() : ""; + return p.GetCStr(); } |