Sayonara Player
Setting.h
1 /* Setting.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 
21 #pragma once
22 #ifndef SAYONARA_SETTING_H_
23 #define SAYONARA_SETTING_H_
24 
25 #include "Utils/Settings/SettingConverter.h"
26 #include "Utils/Settings/SettingKey.h"
27 #include "Utils/Pimpl.h"
28 
29 class Settings;
30 
38 {
39  PIMPL(AbstrSetting)
40 
41  private:
42  AbstrSetting();
43  AbstrSetting(const AbstrSetting& other);
44  AbstrSetting& operator=(const AbstrSetting& other);
45 
46  protected:
47  AbstrSetting(SettingKey key);
48  AbstrSetting(SettingKey key, const char* dbKey);
49 
50 
51  public:
52  virtual ~AbstrSetting();
53 
54  SettingKey getKey() const;
55  QString dbKey() const;
56  bool isDatabaseSetting() const;
57 
58  void assignValue(const QString& value);
59 
60  /* Pure virtual function for DB load/save */
61  virtual bool loadValueFromString(const QString& str)=0;
62  virtual QString valueToString() const=0;
63  virtual void assignDefaultValue()=0;
64 };
65 
66 
67 template<typename KeyClass>
73 class Setting : public AbstrSetting
74 {
75  private:
76  Setting()=delete;
77  Setting(const Setting&)=delete;
78 
79  typename KeyClass::Data mValue;
80  typename KeyClass::Data mDefaultValue;
81 
82  public:
83 
84  /* Constructor */
85  Setting(const char* db_key, const typename KeyClass::Data& def) :
86  AbstrSetting(KeyClass::key, db_key)
87  {
88  mDefaultValue = def;
89  mValue = def;
90  }
91 
92  Setting(const typename KeyClass::Data& def) :
93  AbstrSetting(KeyClass::key)
94  {
95  mDefaultValue = def;
96  mValue = def;
97  }
98 
99  /* Destructor */
100  ~Setting() = default;
101 
102  void assignDefaultValue() override
103  {
104  mValue = mDefaultValue;
105  }
106 
107  QString valueToString() const override
108  {
109  return SettingConverter::toString(mValue);
110  }
111 
112  bool loadValueFromString(const QString& str) override
113  {
114  return SettingConverter::fromString(str, mValue);
115  }
116 
117  /* ... */
118  const typename KeyClass::Data& value() const
119  {
120  return mValue;
121  }
122 
123  /* ... */
124  const typename KeyClass::Data& default_value() const
125  {
126  return mDefaultValue;
127  }
128 
129  /* ... */
130  bool assignValue(const typename KeyClass::Data& val)
131  {
132  if( mValue == val ){
133  return false;
134  }
135 
136  mValue = val;
137  return true;
138  }
139 };
140 
141 #endif // SAYONARA_SETTING_H_
Settings
The Settings class.
Definition: Settings.h:43
Setting
The Setting class T is the pure value type e.g. QString.
Definition: Setting.h:74
AbstrSetting
The AbstrSetting class Every setting needs a key and a value The SettingKey is only used inside the s...
Definition: Setting.h:38