Skip to content

Commit 44bb886

Browse files
committed
Simplify plugin settings interfaces
1 parent 7dd2a5b commit 44bb886

7 files changed

Lines changed: 57 additions & 75 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ plugins.setPluginPaths(paths);
7575

7676
stdc::pluginsystem::PluginSettings globalSettings;
7777
globalSettings.setPluginEnabled("org.example.experimental", true);
78-
plugins.setGlobalPluginSettings(globalSettings);
78+
plugins.setPluginSettings(stdc::pluginsystem::PluginSystem::Global, globalSettings);
7979
stdc::pluginsystem::PluginSettings localSettings;
8080
localSettings.setPluginEnabled("org.example.diagnostics", false);
81-
plugins.setLocalPluginSettings(localSettings);
81+
plugins.setPluginSettings(stdc::pluginsystem::PluginSystem::Local, localSettings);
8282

8383
plugins.loadPlugins();
8484
for (const auto *spec : plugins.plugins()) {

include/stdcorelib/pluginsystem/pluginsettings.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ namespace stdc::pluginsystem {
1818
/// One source of plugin enabled-state overrides, keyed by stable plugin ID.
1919
class STDC_PLUGIN_EXPORT PluginSettings {
2020
public:
21-
/// Records an explicit enabled state for \a id, replacing its previous override.
21+
/// Replaces the enabled-state override for \a id.
2222
///
2323
/// \pre \a id is not empty.
24-
void setPluginEnabled(std::string id, bool enabled);
24+
/// \param id The stable plugin identifier to update.
25+
/// \param enabled The new override, or nothing to remove the existing override.
26+
void setPluginEnabled(std::string id, std::optional<bool> enabled);
2527

26-
/// Removes the override for \a id so that plugin metadata decides its state again.
27-
void resetPlugin(std::string_view id);
28-
29-
/// Returns the explicit override for \a id, or nothing when metadata decides its state.
28+
/// Returns the explicit override for \a id, or nothing when a lower-priority source
29+
/// decides its state.
3030
std::optional<bool> pluginEnabled(std::string_view id) const;
3131

32-
/// Applies the override for \a id to its metadata default.
33-
bool isPluginEnabled(std::string_view id, bool enabledByDefault) const;
34-
3532
std::vector<std::string> enabledPlugins() const;
3633
std::vector<std::string> disabledPlugins() const;
3734

include/stdcorelib/pluginsystem/pluginsystem.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ namespace stdc::pluginsystem {
2828
Directory,
2929
};
3030

31+
/// Which plugin settings source to access.
32+
enum SettingsScope {
33+
/// System-wide settings that override plugin metadata defaults.
34+
Global,
35+
/// Per-user settings that override global settings.
36+
Local,
37+
};
38+
3139
/// Creates a system that accepts only \a iid.
3240
///
3341
/// \pre \a iid is not empty.
@@ -50,23 +58,14 @@ namespace stdc::pluginsystem {
5058
void setPluginPaths(array_view<std::filesystem::path> paths);
5159
std::vector<std::filesystem::path> pluginPaths() const;
5260

53-
/// Replaces system-wide enabled-state overrides before loadPlugins() starts.
54-
///
55-
/// Global settings override plugin metadata. Calls after loadPlugins() starts have no
56-
/// effect.
57-
void setGlobalPluginSettings(PluginSettings settings);
58-
59-
/// Returns the global settings used as system-wide plugin defaults.
60-
PluginSettings globalPluginSettings() const;
61-
62-
/// Replaces per-user enabled-state overrides before loadPlugins() starts.
61+
/// Replaces enabled-state overrides in \a scope before loadPlugins() starts.
6362
///
64-
/// Local settings override global settings. Calls after loadPlugins() starts have no
65-
/// effect.
66-
void setLocalPluginSettings(PluginSettings settings);
63+
/// Local settings override global settings, which override plugin metadata. Calls after
64+
/// loadPlugins() starts have no effect.
65+
void setPluginSettings(SettingsScope scope, PluginSettings settings);
6766

68-
/// Returns the local settings that override global settings for the current user.
69-
PluginSettings localPluginSettings() const;
67+
/// Returns the settings stored in \a scope.
68+
PluginSettings pluginSettings(SettingsScope scope) const;
7069

7170
/// Returns every discovered spec, preserving each pointer for this system's lifetime.
7271
std::vector<PluginSpec *> plugins() const;

src/pluginsystem/pluginsettings.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
namespace stdc::pluginsystem {
1010

11-
void PluginSettings::setPluginEnabled(std::string id, bool enabled) {
12-
_overrides[std::move(id)] = enabled;
13-
}
14-
15-
void PluginSettings::resetPlugin(std::string_view id) {
16-
if (auto it = _overrides.find(id); it != _overrides.end()) {
11+
void PluginSettings::setPluginEnabled(std::string id, std::optional<bool> enabled) {
12+
if (enabled) {
13+
_overrides[std::move(id)] = *enabled;
14+
} else if (auto it = _overrides.find(id); it != _overrides.end()) {
1715
_overrides.erase(it);
1816
}
1917
}
@@ -23,11 +21,6 @@ namespace stdc::pluginsystem {
2321
return it == _overrides.end() ? std::optional<bool>() : it->second;
2422
}
2523

26-
bool PluginSettings::isPluginEnabled(std::string_view id, bool enabledByDefault) const {
27-
auto enabled = pluginEnabled(id);
28-
return enabled.value_or(enabledByDefault);
29-
}
30-
3124
std::vector<std::string> PluginSettings::enabledPlugins() const {
3225
std::vector<std::string> result;
3326
for (const auto &[id, enabled] : _overrides) {

src/pluginsystem/pluginsystem.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ namespace stdc::pluginsystem {
9595
void PluginSystem::Impl::applySettings() const {
9696
for (auto &item : pluginData) {
9797
auto &data = item.second;
98-
data.enabledByDefault = globalSettings.isPluginEnabled(data.id, data.enabledByMetadata);
99-
data.enabled = localSettings.isPluginEnabled(data.id, data.enabledByDefault);
98+
data.enabledByDefault =
99+
globalSettings.pluginEnabled(data.id).value_or(data.enabledByMetadata);
100+
data.enabled = localSettings.pluginEnabled(data.id).value_or(data.enabledByDefault);
100101
}
101102
}
102103

@@ -362,36 +363,24 @@ namespace stdc::pluginsystem {
362363
return impl.factory->pluginPaths(impl.iid);
363364
}
364365

365-
void PluginSystem::setGlobalPluginSettings(PluginSettings settings) {
366+
void PluginSystem::setPluginSettings(SettingsScope scope, PluginSettings settings) {
366367
stdc_impl_t;
367368
std::unique_lock<std::shared_mutex> lock(impl.configMtx);
368369
if (impl.loadStarted) {
369370
return;
370371
}
371-
impl.globalSettings = std::move(settings);
372-
impl.applySettings();
373-
}
374-
375-
PluginSettings PluginSystem::globalPluginSettings() const {
376-
stdc_impl_t;
377-
std::shared_lock<std::shared_mutex> lock(impl.configMtx);
378-
return impl.globalSettings;
379-
}
380-
381-
void PluginSystem::setLocalPluginSettings(PluginSettings settings) {
382-
stdc_impl_t;
383-
std::unique_lock<std::shared_mutex> lock(impl.configMtx);
384-
if (impl.loadStarted) {
385-
return;
372+
if (scope == Local) {
373+
impl.localSettings = std::move(settings);
374+
} else {
375+
impl.globalSettings = std::move(settings);
386376
}
387-
impl.localSettings = std::move(settings);
388377
impl.applySettings();
389378
}
390379

391-
PluginSettings PluginSystem::localPluginSettings() const {
380+
PluginSettings PluginSystem::pluginSettings(SettingsScope scope) const {
392381
stdc_impl_t;
393382
std::shared_lock<std::shared_mutex> lock(impl.configMtx);
394-
return impl.localSettings;
383+
return scope == Local ? impl.localSettings : impl.globalSettings;
395384
}
396385

397386
std::vector<PluginSpec *> PluginSystem::plugins() const {

tests/auto/test_pluginsettings.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ BOOST_AUTO_TEST_SUITE(test_pluginsettings)
99
BOOST_AUTO_TEST_CASE(test_overrides) {
1010
stdc::pluginsystem::PluginSettings settings;
1111
BOOST_CHECK(!settings.pluginEnabled("Unknown"));
12-
BOOST_CHECK(settings.isPluginEnabled("Unknown", true));
13-
BOOST_CHECK(!settings.isPluginEnabled("Unknown", false));
12+
BOOST_CHECK(settings.pluginEnabled("Unknown").value_or(true));
13+
BOOST_CHECK(!settings.pluginEnabled("Unknown").value_or(false));
1414

1515
settings.setPluginEnabled("Plugin", false);
1616
BOOST_REQUIRE(settings.pluginEnabled("Plugin"));
1717
BOOST_CHECK(!*settings.pluginEnabled("Plugin"));
18-
BOOST_CHECK(!settings.isPluginEnabled("Plugin", true));
18+
BOOST_CHECK(!settings.pluginEnabled("Plugin").value_or(true));
1919

2020
settings.setPluginEnabled("Plugin", true);
2121
BOOST_CHECK(*settings.pluginEnabled("Plugin"));
22-
settings.resetPlugin("Plugin");
22+
settings.setPluginEnabled("Plugin", std::nullopt);
2323
BOOST_CHECK(!settings.pluginEnabled("Plugin"));
2424
}
2525

tests/auto/test_pluginsystem.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,20 +271,20 @@ BOOST_AUTO_TEST_CASE(test_global_and_local_settings_precedence_and_freeze_at_loa
271271
globalSettings.setPluginEnabled("Plugin", true);
272272
stdc::pluginsystem::PluginSettings localSettings;
273273
localSettings.setPluginEnabled("Plugin", false);
274-
system.setGlobalPluginSettings(globalSettings);
275-
system.setLocalPluginSettings(localSettings);
274+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Global, globalSettings);
275+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Local, localSettings);
276276
BOOST_CHECK(spec->enabledByDefault());
277277
BOOST_CHECK(!spec->isEnabled());
278278

279-
localSettings.resetPlugin("Plugin");
280-
system.setLocalPluginSettings(localSettings);
279+
localSettings.setPluginEnabled("Plugin", std::nullopt);
280+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Local, localSettings);
281281
BOOST_CHECK(spec->enabledByDefault());
282282
BOOST_CHECK(spec->isEnabled());
283283

284284
globalSettings.setPluginEnabled("Plugin", false);
285285
localSettings.setPluginEnabled("Plugin", true);
286-
system.setGlobalPluginSettings(globalSettings);
287-
system.setLocalPluginSettings(localSettings);
286+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Global, globalSettings);
287+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Local, localSettings);
288288
BOOST_CHECK(!spec->enabledByDefault());
289289
BOOST_CHECK(spec->isEnabled());
290290

@@ -293,14 +293,18 @@ BOOST_AUTO_TEST_CASE(test_global_and_local_settings_precedence_and_freeze_at_loa
293293

294294
globalSettings.setPluginEnabled("Plugin", true);
295295
localSettings.setPluginEnabled("Plugin", false);
296-
system.setGlobalPluginSettings(globalSettings);
297-
system.setLocalPluginSettings(localSettings);
296+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Global, globalSettings);
297+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Local, localSettings);
298298
BOOST_CHECK(!spec->enabledByDefault());
299299
BOOST_CHECK(spec->isEnabled());
300-
BOOST_REQUIRE(system.globalPluginSettings().pluginEnabled("Plugin"));
301-
BOOST_CHECK(!*system.globalPluginSettings().pluginEnabled("Plugin"));
302-
BOOST_REQUIRE(system.localPluginSettings().pluginEnabled("Plugin"));
303-
BOOST_CHECK(*system.localPluginSettings().pluginEnabled("Plugin"));
300+
const auto frozenGlobal =
301+
system.pluginSettings(stdc::pluginsystem::PluginSystem::Global).pluginEnabled("Plugin");
302+
const auto frozenLocal =
303+
system.pluginSettings(stdc::pluginsystem::PluginSystem::Local).pluginEnabled("Plugin");
304+
BOOST_REQUIRE(frozenGlobal);
305+
BOOST_CHECK(!*frozenGlobal);
306+
BOOST_REQUIRE(frozenLocal);
307+
BOOST_CHECK(*frozenLocal);
304308
}
305309

306310
BOOST_AUTO_TEST_CASE(test_disabled_dependencies) {
@@ -319,7 +323,7 @@ BOOST_AUTO_TEST_CASE(test_disabled_dependencies) {
319323
stdc::pluginsystem::PluginSystem system("org.stdcorelib.PluginSystem",
320324
stdc::pluginsystem::PluginSystem::Directory);
321325
system.setPluginPaths(directory.path());
322-
system.setLocalPluginSettings(settings);
326+
system.setPluginSettings(stdc::pluginsystem::PluginSystem::Local, settings);
323327
system.loadPlugins();
324328

325329
const auto specs = system.plugins();

0 commit comments

Comments
 (0)