Sayonara Player
Settings.h
1/* Settings.h */
2
3/* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#pragma once
21#ifndef SAYONARA_SETTINGS_H_
22#define SAYONARA_SETTINGS_H_
23
24#include "Utils/Settings/SettingKey.h"
25#include "Utils/Settings/Setting.h"
26#include "Utils/Settings/SettingNotifier.h"
27#include "Utils/Singleton.h"
28
29#include <array>
30#include <cassert>
31
32#define GetSetting(x) Settings::instance()->get<x>()
33#define SetSetting(x, y) Settings::instance()->set<x>(y)
34#define ListenSetting(x, y) Set::listen<x>(this, &y)
35#define ListenSettingNoCall(x, y) Set::listen<x>(this, &y, false)
36
37using SettingArray = std::array<AbstrSetting*, static_cast<unsigned int>(SettingKey::Num_Setting_Keys)>;
38
44{
45 SINGLETON(Settings)
46 PIMPL(Settings)
47
48 public:
49 [[nodiscard]] AbstrSetting* setting(SettingKey keyIndex) const;
50
51 /* get all settings (used by database) */
52 const SettingArray& settings();
53
54 /* before you want to access a setting you have to register it */
55 void registerSetting(AbstrSetting* s);
56
57 /* checks if all settings are registered */
58 bool checkSettings();
59
60 /* get a setting, defined by a unique, REGISTERED key */
61 template<typename KeyClass>
62 const typename KeyClass::Data& get() const
63 {
64 using SettingPtr = Setting<KeyClass>*;
65 auto* s = static_cast<SettingPtr>(setting(KeyClass::key));
66 assert(s);
67 return s->value();
68 }
69
70 /* set a setting, define by a unique, REGISTERED key */
71 template<typename KeyClass>
72 void set(const typename KeyClass::Data& val)
73 {
74 using SettingPtr = Setting<KeyClass>*;
75 auto* s = static_cast<SettingPtr>(setting(KeyClass::key));
76 assert(s);
77 if(s->assignValue(val))
78 {
80 sn->valueChanged();
81 }
82 }
83
84 /* get a setting, defined by a unique, REGISTERED key */
85 template<typename KeyClass>
86 void shout() const
87 {
88 auto* settingNotifier = SettingNotifier<KeyClass>::instance();
89 settingNotifier->valueChanged();
90 }
91
92 void applyFixes();
93};
94
95#endif // SAYONARA_SETTINGS_H_
The AbstrSetting class Every setting needs a key and a value The SettingKey is only used inside the s...
Definition: Setting.h:38
Definition: SettingNotifier.h:48
The Setting class T is the pure value type e.g. QString.
Definition: Setting.h:74
The Settings class.
Definition: Settings.h:44