diff options
author | Jonas Devlieghere <jonas@devlieghere.com> | 2023-05-04 09:26:58 -0700 |
---|---|---|
committer | Jonas Devlieghere <jonas@devlieghere.com> | 2023-05-04 16:42:46 -0700 |
commit | 6f8b33f6dfd0a0f8d2522b6c832bd6298ae2f3f3 (patch) | |
tree | e0c6024fb6ebf5acf5617f1256ecaea28052e774 /lldb/include | |
parent | 04aa943be8ed5c03092e2a90112ac638360ec253 (diff) | |
download | llvm-6f8b33f6dfd0a0f8d2522b6c832bd6298ae2f3f3.tar.gz |
[lldb] Use templates to simplify {Get,Set}PropertyAtIndex (NFC)
Use templates to simplify {Get,Set}PropertyAtIndex. It has always
bothered me how cumbersome those calls are when adding new properties.
After this patch, SetPropertyAtIndex infers the type from its arguments
and GetPropertyAtIndex required a single template argument for the
return value. As an added benefit, this enables us to remove a bunch of
wrappers from UserSettingsController and OptionValueProperties.
Differential revision: https://reviews.llvm.org/D149774
Diffstat (limited to 'lldb/include')
-rw-r--r-- | lldb/include/lldb/Core/Debugger.h | 10 | ||||
-rw-r--r-- | lldb/include/lldb/Core/UserSettingsController.h | 22 | ||||
-rw-r--r-- | lldb/include/lldb/Interpreter/OptionValue.h | 45 | ||||
-rw-r--r-- | lldb/include/lldb/Interpreter/OptionValueProperties.h | 67 |
4 files changed, 97 insertions, 47 deletions
diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h index a7487e159e2c..f05d8d4e6b23 100644 --- a/lldb/include/lldb/Core/Debugger.h +++ b/lldb/include/lldb/Core/Debugger.h @@ -269,7 +269,7 @@ public: const FormatEntity::Entry *GetFrameFormatUnique() const; - uint32_t GetStopDisassemblyMaxSize() const; + uint64_t GetStopDisassemblyMaxSize() const; const FormatEntity::Entry *GetThreadFormat() const; @@ -283,7 +283,7 @@ public: bool SetREPLLanguage(lldb::LanguageType repl_lang); - uint32_t GetTerminalWidth() const; + uint64_t GetTerminalWidth() const; bool SetTerminalWidth(uint32_t term_width); @@ -329,11 +329,11 @@ public: llvm::StringRef GetStopShowColumnAnsiSuffix() const; - uint32_t GetStopSourceLineCount(bool before) const; + uint64_t GetStopSourceLineCount(bool before) const; StopDisassemblyType GetStopDisassemblyDisplay() const; - uint32_t GetDisassemblyLineCount() const; + uint64_t GetDisassemblyLineCount() const; llvm::StringRef GetStopShowLineMarkerAnsiPrefix() const; @@ -349,7 +349,7 @@ public: bool SetPrintDecls(bool b); - uint32_t GetTabSize() const; + uint64_t GetTabSize() const; bool SetTabSize(uint32_t tab_size); diff --git a/lldb/include/lldb/Core/UserSettingsController.h b/lldb/include/lldb/Core/UserSettingsController.h index 19b080d125a3..f320057ef120 100644 --- a/lldb/include/lldb/Core/UserSettingsController.h +++ b/lldb/include/lldb/Core/UserSettingsController.h @@ -9,6 +9,7 @@ #ifndef LLDB_CORE_USERSETTINGSCONTROLLER_H #define LLDB_CORE_USERSETTINGSCONTROLLER_H +#include "lldb/Interpreter/OptionValueProperties.h" #include "lldb/Utility/Status.h" #include "lldb/lldb-forward.h" #include "lldb/lldb-private-enumerations.h" @@ -82,6 +83,27 @@ public: static bool IsSettingExperimental(llvm::StringRef setting); + template <typename T> + T GetPropertyAtIndexAs(uint32_t idx, T default_value, + const ExecutionContext *exe_ctx = nullptr) const { + return m_collection_sp->GetPropertyAtIndexAs<T>(idx, exe_ctx) + .value_or(default_value); + } + + template <typename T, typename U = typename std::remove_pointer<T>::type, + std::enable_if_t<std::is_pointer_v<T>, bool> = true> + const U * + GetPropertyAtIndexAs(uint32_t idx, + const ExecutionContext *exe_ctx = nullptr) const { + return m_collection_sp->GetPropertyAtIndexAs<T>(idx, exe_ctx); + } + + template <typename T> + bool SetPropertyAtIndex(uint32_t idx, T t, + const ExecutionContext *exe_ctx = nullptr) const { + return m_collection_sp->SetPropertyAtIndex<T>(idx, t, exe_ctx); + } + protected: lldb::OptionValuePropertiesSP m_collection_sp; }; diff --git a/lldb/include/lldb/Interpreter/OptionValue.h b/lldb/include/lldb/Interpreter/OptionValue.h index 7d6ab6c3f6f5..eef7d67eb034 100644 --- a/lldb/include/lldb/Interpreter/OptionValue.h +++ b/lldb/include/lldb/Interpreter/OptionValue.h @@ -322,6 +322,51 @@ public: m_callback(); } + template <typename T, std::enable_if_t<!std::is_pointer_v<T>, bool> = true> + std::optional<T> GetValueAs() const { + if constexpr (std::is_same_v<T, uint64_t>) + return GetUInt64Value(); + if constexpr (std::is_same_v<T, int64_t>) + return GetSInt64Value(); + if constexpr (std::is_same_v<T, bool>) + return GetBooleanValue(); + if constexpr (std::is_same_v<T, char>) + return GetCharValue(); + if constexpr (std::is_same_v<T, lldb::Format>) + return GetFormatValue(); + if constexpr (std::is_same_v<T, lldb::LanguageType>) + return GetLanguageValue(); + if constexpr (std::is_same_v<T, llvm::StringRef>) + return GetStringValue(); + if constexpr (std::is_enum_v<T>) + if (std::optional<int64_t> value = GetEnumerationValue()) + return static_cast<T>(*value); + return {}; + } + + template <typename T, + typename U = typename std::remove_const< + typename std::remove_pointer<T>::type>::type, + std::enable_if_t<std::is_pointer_v<T>, bool> = true> + T GetValueAs() const { + if constexpr (std::is_same_v<U, FormatEntity::Entry>) + return GetFormatEntity(); + if constexpr (std::is_same_v<U, RegularExpression>) + return GetRegexValue(); + return {}; + } + + bool SetValueAs(bool v) { return SetBooleanValue(v); } + + bool SetValueAs(llvm::StringRef v) { return SetStringValue(v); } + + bool SetValueAs(lldb::LanguageType v) { return SetLanguageValue(v); } + + template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true> + bool SetValueAs(T t) { + return SetEnumerationValue(t); + } + protected: using TopmostBase = OptionValue; diff --git a/lldb/include/lldb/Interpreter/OptionValueProperties.h b/lldb/include/lldb/Interpreter/OptionValueProperties.h index 4782d7eff947..3251a5ed5e2e 100644 --- a/lldb/include/lldb/Interpreter/OptionValueProperties.h +++ b/lldb/include/lldb/Interpreter/OptionValueProperties.h @@ -122,57 +122,15 @@ public: bool SetPropertyAtIndexFromArgs(uint32_t idx, const Args &args, const ExecutionContext *exe_ctx = nullptr); - std::optional<bool> - GetPropertyAtIndexAsBoolean(uint32_t idx, - const ExecutionContext *exe_ctx = nullptr) const; - - bool SetPropertyAtIndexAsBoolean(uint32_t idx, bool new_value, - const ExecutionContext *exe_ctx = nullptr); - OptionValueDictionary *GetPropertyAtIndexAsOptionValueDictionary( uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const; - std::optional<int64_t> GetPropertyAtIndexAsEnumeration( - uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const; - - bool - SetPropertyAtIndexAsEnumeration(uint32_t idx, int64_t new_value, - const ExecutionContext *exe_ctx = nullptr); - - const FormatEntity::Entry * - GetPropertyAtIndexAsFormatEntity(uint32_t idx, - const ExecutionContext *exe_ctx = nullptr); - - const RegularExpression *GetPropertyAtIndexAsOptionValueRegex( - uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const; - OptionValueSInt64 *GetPropertyAtIndexAsOptionValueSInt64( uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const; OptionValueUInt64 *GetPropertyAtIndexAsOptionValueUInt64( uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const; - std::optional<int64_t> - GetPropertyAtIndexAsSInt64(uint32_t idx, - const ExecutionContext *exe_ctx = nullptr) const; - - bool SetPropertyAtIndexAsSInt64(uint32_t idx, int64_t new_value, - const ExecutionContext *exe_ctx = nullptr); - - std::optional<uint64_t> - GetPropertyAtIndexAsUInt64(uint32_t idx, - const ExecutionContext *exe_ctx = nullptr) const; - - bool SetPropertyAtIndexAsUInt64(uint32_t idx, uint64_t new_value, - const ExecutionContext *exe_ctx = nullptr); - - std::optional<llvm::StringRef> - GetPropertyAtIndexAsString(uint32_t idx, - const ExecutionContext *exe_ctx = nullptr) const; - - bool SetPropertyAtIndexAsString(uint32_t idx, llvm::StringRef new_value, - const ExecutionContext *exe_ctx = nullptr); - OptionValueString *GetPropertyAtIndexAsOptionValueString( uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const; @@ -201,6 +159,31 @@ public: void SetValueChangedCallback(uint32_t property_idx, std::function<void()> callback); + template <typename T> + auto GetPropertyAtIndexAs(uint32_t idx, + const ExecutionContext *exe_ctx = nullptr) const { + if (const Property *property = GetPropertyAtIndex(idx, exe_ctx)) { + if (OptionValue *value = property->GetValue().get()) + return value->GetValueAs<T>(); + } + if constexpr (std::is_pointer_v<T>) + return T{nullptr}; + else + return std::optional<T>{std::nullopt}; + } + + template <typename T> + bool SetPropertyAtIndex(uint32_t idx, T t, + const ExecutionContext *exe_ctx = nullptr) const { + if (const Property *property = GetPropertyAtIndex(idx, exe_ctx)) { + if (OptionValue *value = property->GetValue().get()) { + value->SetValueAs(t); + return true; + } + } + return false; + } + protected: Property *ProtectedGetPropertyAtIndex(uint32_t idx) { assert(idx < m_properties.size() && "invalid property index"); |