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 
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 
44  AbstrSetting* setting(SettingKey keyIndex) const;
45 
46  /* get all settings (used by database) */
47  const SettingArray& settings();
48 
49 
50  /* before you want to access a setting you have to register it */
51  void register_setting(AbstrSetting* s);
52 
53 
54  /* checks if all settings are registered */
55  bool check_settings();
56 
57 
58  /* get a setting, defined by a unique, REGISTERED key */
59  template< typename T>
60  const typename T::Data& get() const
61  {
62  using DataType = typename T::Data;
63  constexpr SettingKey keyIndex = T::key;
64 
65  using SettingPtr=Setting<DataType, keyIndex>*;
66 
67  SettingPtr s = static_cast<SettingPtr>( setting(keyIndex) );
68  return s->value();
69  }
70 
71  /* set a setting, define by a unique, REGISTERED key */
72  template< typename T>
73  void set(const typename T::Data& val)
74  {
75  using DataType = typename T::Data;
76  constexpr SettingKey keyIndex = T::key;
77 
78  using SettingPtr=Setting<DataType, keyIndex>*;
79  SettingPtr s = static_cast<SettingPtr>( setting(keyIndex) );
80 
81  if( s->assign_value(val))
82  {
84 
86  sn->val_changed();
87  }
88  }
89 
90  /* get a setting, defined by a unique, REGISTERED key */
91  template< typename T>
92  void shout() const
93  {
94  using DataType = typename T::Data;
95  constexpr SettingKey keyIndex = T::key;
97 
99  sn->val_changed();
100  }
101 
102  void apply_fixes();
103 
104 };
105 
106 
107 #endif // SAYONARA_SETTINGS_H_
The Setting class T is the pure value type e.g. QString.
Definition: Setting.h:75
Definition: SettingNotifier.h:49
SettingKey
The SK namespace is used to access setting keys.
Definition: SettingKey.h:53
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:38
Definition: SettingKey.h:213