Skip to content

Commit

Permalink
Configuration: Split options into global and per-module configuration.
Browse files Browse the repository at this point in the history
This lets each module define its own options, without needing the option names
to be unique across all modules.

Also organises the module-specific options into their own sections at the end
of the help text.
  • Loading branch information
RAOF committed Sep 9, 2024
1 parent b9ceb10 commit 9367dc7
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 121 deletions.
21 changes: 15 additions & 6 deletions include/platform/mir/options/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace mir
{
class SharedLibrary;

namespace options
{
extern char const* const arw_server_socket_opt;
Expand Down Expand Up @@ -66,17 +68,24 @@ extern char const* const auto_console;

extern char const* const vt_option_name;

class Configuration
class OptionsProvider
{
public:
virtual std::shared_ptr<options::Option> the_options() const = 0;
/**
* The options not associated with a specific loaded module
*/
virtual std::shared_ptr<options::Option> global_options() const = 0;
/**
* All options, including those added by the specified module
*/
virtual auto the_options_for(SharedLibrary const& module) const -> std::shared_ptr<options::Option> = 0;

protected:
OptionsProvider() = default;
virtual ~OptionsProvider() = default;

Configuration() = default;
virtual ~Configuration() = default;
Configuration(Configuration const&) = delete;
Configuration& operator=(Configuration const&) = delete;
OptionsProvider(OptionsProvider const&) = delete;
OptionsProvider& operator=(OptionsProvider const&) = delete;
};
}
}
Expand Down
48 changes: 45 additions & 3 deletions include/platform/mir/options/default_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

#include "mir/options/configuration.h"
#include "mir/options/program_option.h"
#include "mir/shared_library.h"
#include <boost/program_options/options_description.hpp>
#include <vector>

namespace mir
{
class SharedLibrary;
namespace options
{
class DefaultConfiguration : public Configuration
class DefaultConfiguration : public OptionsProvider
{
public:
DefaultConfiguration(int argc, char const* argv[]);
Expand All @@ -54,7 +54,8 @@ class DefaultConfiguration : public Configuration

void add_platform_options();
// accessed via the base interface, when access to add_options() has been "lost"
std::shared_ptr<options::Option> the_options() const override;
std::shared_ptr<options::Option> global_options() const override;
auto the_options_for(SharedLibrary const& module) const -> std::shared_ptr<Option> override;

virtual void parse_arguments(
boost::program_options::options_description desc,
Expand All @@ -74,6 +75,47 @@ class DefaultConfiguration : public Configuration
char const** const argv;
std::function<void(int argc, char const* const* argv)> const unparsed_arguments_handler;
std::shared_ptr<boost::program_options::options_description> const program_options;

template<typename Value>
class LibraryMap
{
private:
std::vector<std::pair<mir::SharedLibrary::Handle, Value>> values;

auto find(SharedLibrary const&) -> decltype(values)::iterator;
auto find(SharedLibrary const&) const -> decltype(values)::const_iterator;
public:
LibraryMap() = default;
~LibraryMap() = default;

auto operator[](SharedLibrary const& lib) -> Value&;

auto at(SharedLibrary const& lib) const -> Value const&;

auto begin() const -> decltype(values)::const_iterator;
auto end() const -> decltype(values)::const_iterator;

template<typename ...Args>
auto emplace(SharedLibrary const& lib, Args&& ...args) -> std::pair<typename decltype(values)::iterator, bool>
{
auto existing = find(lib);

if (existing != values.end())
{
return {existing, false};
}

values.emplace_back(
std::piecewise_construct,
std::forward_as_tuple(lib.get_handle()),
std::forward<Args>(args)...);

return {values.end() - 1, true};
}
};

LibraryMap<boost::program_options::options_description> module_options_desc;
LibraryMap<std::shared_ptr<Option>> mutable module_options;
std::shared_ptr<Option> mutable options;
};
}
Expand Down
2 changes: 1 addition & 1 deletion include/platform/mir/options/program_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ProgramOption : public Option

void parse_environment(
boost::program_options::options_description const& description,
char const* prefix);
std::function<std::string(std::string)> env_var_name_mapper);

void parse_file(
boost::program_options::options_description const& description,
Expand Down
8 changes: 5 additions & 3 deletions src/include/server/mir/default_server_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MIR_DEFAULT_SERVER_CONFIGURATION_H_

#include "mir/cached_ptr.h"
#include "mir/options/option.h"
#include "mir/server_configuration.h"
#include "mir/shell/window_manager_builder.h"

Expand Down Expand Up @@ -137,7 +138,7 @@ class Logger;
namespace options
{
class Option;
class Configuration;
class OptionsProvider;
}

namespace report
Expand All @@ -160,7 +161,7 @@ class DefaultServerConfiguration : public virtual ServerConfiguration
{
public:
DefaultServerConfiguration(int argc, char const* argv[]);
explicit DefaultServerConfiguration(std::shared_ptr<options::Configuration> const& configuration_options);
explicit DefaultServerConfiguration(std::shared_ptr<options::OptionsProvider> const& configuration_options);

/** @name DisplayServer dependencies
* dependencies of DisplayServer on the rest of the Mir
Expand Down Expand Up @@ -347,6 +348,7 @@ class DefaultServerConfiguration : public virtual ServerConfiguration

protected:
std::shared_ptr<options::Option> the_options() const;
auto the_options_provider() const -> std::shared_ptr<options::OptionsProvider>;
std::shared_ptr<input::DefaultInputDeviceHub> the_default_input_device_hub();
std::shared_ptr<graphics::DisplayConfigurationObserver> the_display_configuration_observer();
std::shared_ptr<input::SeatObserver> the_seat_observer();
Expand Down Expand Up @@ -434,7 +436,7 @@ class DefaultServerConfiguration : public virtual ServerConfiguration
std::shared_ptr<DecorationStrategy> decoration_strategy;

private:
std::shared_ptr<options::Configuration> const configuration_options;
std::shared_ptr<options::OptionsProvider> const configuration_options;
std::shared_ptr<input::EventFilter> default_filter;
CachedPtr<ObserverMultiplexer<graphics::DisplayConfigurationObserver>>
display_configuration_observer_multiplexer;
Expand Down
Loading

0 comments on commit 9367dc7

Please sign in to comment.