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 
31 #define GetSetting(x) Settings::instance()->get<x>()
32 #define SetSetting(x, y) Settings::instance()->set<x>(y)
33 #define ListenSetting(x, y) Set::listen<x>(this, &y)
34 #define ListenSettingNoCall(x, y) Set::listen<x>(this, &y, false)
35 
36 using SettingArray=std::array<AbstrSetting*, static_cast<unsigned int>(SettingKey::Num_Setting_Keys)>;
37 
38 /**
39  * @brief The Settings class
40  * @ingroup Settings
41  */
42 class Settings
43 {
44 	SINGLETON(Settings)
45 	PIMPL(Settings)
46 
47 	public:
48 		AbstrSetting* setting(SettingKey keyIndex) const;
49 
50 		/* get all settings (used by database) */
51 		const SettingArray& settings();
52 
53 		/* before you want to access a setting you have to register it */
54 		void registerSetting(AbstrSetting* s);
55 
56 		/* checks if all settings are registered */
57 		bool checkSettings();
58 
59 		/* get a setting, defined by a unique, REGISTERED key */
60 		template<typename KeyClass>
get()61 		const typename KeyClass::Data& get() const
62 		{
63 			using SettingPtr=Setting<KeyClass>*;
64 			SettingPtr s = static_cast<SettingPtr>( setting(KeyClass::key) );
65 			return s->value();
66 		}
67 
68 		/* set a setting, define by a unique, REGISTERED key */
69 		template<typename KeyClass>
set(const typename KeyClass::Data & val)70 		void set(const typename KeyClass::Data& val)
71 		{
72 			using SettingPtr=Setting<KeyClass>*;
73 			SettingPtr s = static_cast<SettingPtr>( setting(KeyClass::key) );
74 
75 			if( s->assignValue(val))
76 			{
77 				SettingNotifier< KeyClass >* sn = SettingNotifier< KeyClass >::instance();
78 				sn->valueChanged();
79 			}
80 		}
81 
82 		/* get a setting, defined by a unique, REGISTERED key */
83 		template<typename KeyClass>
shout()84 		void shout() const
85 		{
86 			SettingNotifier<KeyClass >* sn = SettingNotifier< KeyClass >::instance();
87 			sn->valueChanged();
88 		}
89 
90 		void applyFixes();
91 };
92 
93 
94 #endif // SAYONARA_SETTINGS_H_
95