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 DataType, SettingKey keyIndex>
60  const DataType& get(const SettingIdentifier<DataType, keyIndex>& k) const
61  {
62  Q_UNUSED(k);
63  using SettingPtr=Setting<DataType>*;
64 
65  SettingPtr s = static_cast<SettingPtr>( setting(keyIndex) );
66  return s->value();
67  }
68 
69  /* set a setting, define by a unique, REGISTERED key */
70  template<typename DataType, SettingKey keyIndex>
71  void set(const SettingIdentifier<DataType,keyIndex>& key, const DataType& val)
72  {
73  using SettingPtr=Setting<DataType>*;
74  SettingPtr s = static_cast<SettingPtr>( setting(keyIndex) );
75 
76  if( s->assign_value(val))
77  {
78  using KeyClass=decltype(key);
79 
81  sn->val_changed();
82  }
83  }
84 
85  /* get a setting, defined by a unique, REGISTERED key */
86  template<typename DataType, SettingKey keyIndex>
87  void shout(const SettingIdentifier<DataType,keyIndex>& key) const
88  {
89  using KeyClass=decltype(key);
90 
92  sn->val_changed();
93  }
94 };
95 
96 
97 #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:51
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:197