From 61a99b8fd795c44ec8258697b8c9291a89d99ea4 Mon Sep 17 00:00:00 2001 From: NamorNiradnug Date: Fri, 7 Nov 2025 02:54:17 +0300 Subject: [PATCH] Don't copy accessed values --- include/wayfire/config/compound-option.hpp | 10 +++++----- include/wayfire/config/option-wrapper.hpp | 4 ++-- include/wayfire/config/option.hpp | 12 +++++++++--- src/option.cpp | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/wayfire/config/compound-option.hpp b/include/wayfire/config/compound-option.hpp index 7c06254..bd630c9 100644 --- a/include/wayfire/config/compound-option.hpp +++ b/include/wayfire/config/compound-option.hpp @@ -47,19 +47,19 @@ class compound_option_entry_base_t virtual ~compound_option_entry_base_t() = default; /** @return The prefix of the tuple entry. */ - std::string get_prefix() const + const std::string& get_prefix() const WF_LIFETIMEBOUND { return prefix; } /** @return The name of the tuple entry. */ - std::string get_name() const + const std::string& get_name() const WF_LIFETIMEBOUND { return name; } /** @return The untyped default value of the tuple entry. */ - std::optional get_default_value() const + const std::optional& get_default_value() const WF_LIFETIMEBOUND { return default_value; } @@ -224,12 +224,12 @@ class compound_option_t : public option_base_t /** * Get the type information about entries in the option. */ - const entries_t& get_entries() const; + const entries_t& get_entries() const WF_LIFETIMEBOUND; /** * Check if this compound option has named tuples. */ - std::string get_type_hint() const + const std::string& get_type_hint() const WF_LIFETIMEBOUND { return list_type_hint; } diff --git a/include/wayfire/config/option-wrapper.hpp b/include/wayfire/config/option-wrapper.hpp index 9b467de..6027401 100644 --- a/include/wayfire/config/option-wrapper.hpp +++ b/include/wayfire/config/option-wrapper.hpp @@ -95,12 +95,12 @@ class base_option_wrapper_t } /** Implicitly convertible to the value of the option */ - operator Type() const + operator decltype(auto)() const WF_LIFETIMEBOUND { return this->value(); } - Type value() const + decltype(auto) value() const WF_LIFETIMEBOUND { if constexpr (is_std_vector::value) { diff --git a/include/wayfire/config/option.hpp b/include/wayfire/config/option.hpp index 5ac0031..94bcd9c 100644 --- a/include/wayfire/config/option.hpp +++ b/include/wayfire/config/option.hpp @@ -6,6 +6,12 @@ #include +#ifdef __clang__ + #define WF_LIFETIMEBOUND [[clang::lifetimebound]] +#else + #define WF_LIFETIMEBOUND +#endif + namespace wf { namespace config @@ -21,7 +27,7 @@ class option_base_t option_base_t& operator =(const option_base_t& other) = delete; /** @return The name of the option */ - std::string get_name() const; + const std::string& get_name() const WF_LIFETIMEBOUND; /** @return A copy of the option */ virtual std::shared_ptr clone_option() const = 0; @@ -253,12 +259,12 @@ class option_t : public option_base_t, } } - Type get_value() const + const Type& get_value() const WF_LIFETIMEBOUND { return value; } - Type get_default_value() const + const Type& get_default_value() const WF_LIFETIMEBOUND { return default_value; } diff --git a/src/option.cpp b/src/option.cpp index 90377a5..2f2cce7 100644 --- a/src/option.cpp +++ b/src/option.cpp @@ -5,7 +5,7 @@ #include "option-impl.hpp" #include "wayfire/util/log.hpp" -std::string wf::config::option_base_t::get_name() const +const std::string& wf::config::option_base_t::get_name() const { return this->priv->name; }