Skip to content

Commit a4d7f5a

Browse files
committed
option-wrapper: add option to load via section+option name
1 parent 5ae61ab commit a4d7f5a

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

include/wayfire/config/option-wrapper.hpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdexcept>
44
#include <wayfire/config/option.hpp>
5+
#include <wayfire/config/section.hpp>
56
#include <wayfire/config/compound-option.hpp>
67

78
namespace wf
@@ -77,25 +78,12 @@ class base_option_wrapper_t
7778
*/
7879
void load_option(const std::string& name)
7980
{
80-
if (raw_option)
81-
{
82-
throw std::logic_error(
83-
"Loading an option into option wrapper twice!");
84-
}
85-
86-
auto untyped_option = load_raw_option(name);
87-
if (untyped_option == nullptr)
88-
{
89-
throw std::runtime_error("No such option: " + std::string(name));
90-
}
91-
92-
raw_option = std::dynamic_pointer_cast<OptionType>(untyped_option);
93-
if (raw_option == nullptr)
94-
{
95-
throw std::runtime_error("Bad option type: " + std::string(name));
96-
}
81+
_load_option(load_raw_option(name), name);
82+
}
9783

98-
raw_option->add_updated_handler(&option_update_listener);
84+
void load_option(std::shared_ptr<wf::config::section_t> section, const std::string& name)
85+
{
86+
_load_option(section->get_option_or(name), section->get_name() + "/" + name);
9987
}
10088

10189
virtual ~base_option_wrapper_t()
@@ -162,5 +150,27 @@ class base_option_wrapper_t
162150
*/
163151
virtual std::shared_ptr<wf::config::option_base_t> load_raw_option(
164152
const std::string& name) = 0;
153+
154+
void _load_option(std::shared_ptr<wf::config::option_base_t> _option, const std::string& name)
155+
{
156+
if (raw_option)
157+
{
158+
throw std::logic_error(
159+
"Loading an option into option wrapper twice!");
160+
}
161+
162+
if (_option == nullptr)
163+
{
164+
throw std::runtime_error("No such option: " + std::string(name));
165+
}
166+
167+
raw_option = std::dynamic_pointer_cast<OptionType>(_option);
168+
if (raw_option == nullptr)
169+
{
170+
throw std::runtime_error("Bad option type: " + name);
171+
}
172+
173+
raw_option->add_updated_handler(&option_update_listener);
174+
}
165175
};
166176
}

0 commit comments

Comments
 (0)