Sayonara Player
Settings.h
1 /* Settings.h */
2 
3 /* Copyright (C) 2011-2017 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 using SettingArray=std::array<AbstrSetting*, static_cast<unsigned int>(SettingKey::Num_Setting_Keys)>;
32 
37 class Settings
38 {
39  SINGLETON(Settings)
40  PIMPL(Settings)
41 
42  public:
43  AbstrSetting* setting(SettingKey keyIndex) const;
44 
45  /* get all settings (used by database) */
46  const SettingArray& settings();
47 
48  /* before you want to access a setting you have to register it */
49  void register_setting(AbstrSetting* s);
50 
51  /* checks if all settings are registered */
52  bool check_settings();
53 
54  /* get a setting, defined by a unique, REGISTERED key */
55  template<typename KeyClass>
56  const typename KeyClass::Data& get() const
57  {
58  using SettingPtr=Setting<KeyClass>*;
59  SettingPtr s = static_cast<SettingPtr>( setting(KeyClass::key) );
60  return s->value();
61  }
62 
63  /* set a setting, define by a unique, REGISTERED key */
64  template<typename KeyClass>
65  void set(const typename KeyClass::Data& val)
66  {
67  using SettingPtr=Setting<KeyClass>*;
68  SettingPtr s = static_cast<SettingPtr>( setting(KeyClass::key) );
69 
70  if( s->assign_value(val))
71  {
73  sn->val_changed();
74  }
75  }
76 
77  /* get a setting, defined by a unique, REGISTERED key */
78  template<typename KeyClass>
79  void shout() const
80  {
82  sn->val_changed();
83  }
84 
85  void apply_fixes();
86 
87 };
88 
89 
90 #endif // SAYONARA_SETTINGS_H_
The Setting class T is the pure value type e.g. QString.
Definition: Setting.h:74
Definition: SettingNotifier.h:47
The Settings class.
Definition: Settings.h:37
The AbstrSetting class Every setting needs a key and a value The SettingKey is only used inside the s...
Definition: Setting.h:37