// Copyright 2007-2020 David Robillard // SPDX-License-Identifier: GPL-3.0-or-later #ifndef PATCHAGE_CONFIGURATION_HPP #define PATCHAGE_CONFIGURATION_HPP #include "Coord.hpp" #include "Setting.hpp" #include #include #include #include #include #include namespace patchage { enum class SignalDirection; enum class PortType; class Configuration { public: static constexpr unsigned n_port_types = 5U; explicit Configuration(std::function on_change); void load(); void save(); bool get_module_location(const std::string& name, SignalDirection type, Coord& loc) const; void set_module_location(const std::string& name, SignalDirection type, Coord loc); void set_module_split(const std::string& name, bool split); bool get_module_split(const std::string& name, bool default_val) const; uint32_t get_port_color(PortType type) const { return _port_colors[static_cast(type)]; } void set_port_color(PortType type, uint32_t rgba) { _port_colors[static_cast(type)] = rgba; _on_change(setting::PortColor{type, rgba}); } // Set a global configuration setting template void set(decltype(S::value) value) { S& setting = std::get(_settings); if (setting.value != value) { setting.value = std::move(value); _on_change(setting); } } // Set a global configuration setting template void set_setting(S new_setting) { set(new_setting.value); } // Set a global port color setting void set_setting(setting::PortColor new_setting) { auto& color = _port_colors[static_cast(new_setting.type)]; if (color != new_setting.color) { set_port_color(new_setting.type, new_setting.color); } } // Get a global configuration setting template decltype(S::value) get() const { return std::get(_settings).value; } /// Call `visitor` once with each configuration setting template void each(Visitor visitor) { visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); visitor(std::get(_settings)); for (auto i = 0u; i < n_port_types; ++i) { visitor(setting::PortColor{static_cast(i), _port_colors[i]}); } } private: struct ModuleSettings { explicit ModuleSettings(bool s = false) : split(s) {} std::optional input_location; std::optional output_location; std::optional inout_location; bool split; }; std::map _module_settings; uint32_t _default_port_colors[n_port_types] = {}; uint32_t _port_colors[n_port_types] = {}; using Settings = std::tuple; Settings _settings; std::function _on_change; }; } // namespace patchage #endif // PATCHAGE_CONFIGURATION_HPP