Sayonara Player
Setting.h
1 /* Setting.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 
21 #pragma once
22 #ifndef SAYONARA_SETTING_H_
23 #define SAYONARA_SETTING_H_
24 
25 
26 #include "Utils/Settings/SettingKey.h"
27 #include "Utils/Settings/SettingConverter.h"
28 #include "Utils/Pimpl.h"
29 
30 class Settings;
31 
39 {
40  PIMPL(AbstrSetting)
41 
42  private:
43  AbstrSetting();
44  AbstrSetting(const AbstrSetting& other);
45  AbstrSetting& operator=(const AbstrSetting& other);
46 
47  protected:
49  AbstrSetting(SettingKey key, const char* db_key);
50 
51 
52  public:
53  virtual ~AbstrSetting();
54 
55  SettingKey get_key() const;
56  QString db_key() const;
57  bool is_db_setting() const;
58 
59  void assign_value(const QString& value);
60 
61  /* Pure virtual function for DB load/save */
62  virtual bool load_value_from_string(const QString& str)=0;
63  virtual QString value_to_string() const=0;
64  virtual void assign_default_value()=0;
65 };
66 
67 
68 template< typename DataType, SettingKey keyIndex,
69  template <typename Arg> class SC = SettingConverter >
75 class Setting : public AbstrSetting
76 {
77  private:
78  Setting();
79  Setting(const Setting&);
80 
81  DataType _val;
82  DataType _default_val;
83 
84  public:
85 
86  /* Constructor */
87  Setting(const char* db_key, const DataType& def) :
88  AbstrSetting(keyIndex, db_key)
89  {
90  _default_val = def;
91  _val = def;
92  }
93 
94  Setting(const DataType& def) :
95  AbstrSetting(keyIndex)
96  {
97  _default_val = def;
98  _val = def;
99  }
100 
101  /* Destructor */
102  ~Setting() {}
103 
104  void assign_default_value() override
105  {
106  _val = _default_val;
107  }
108 
109  QString value_to_string() const override
110  {
111  return SC<DataType>::cvt_to_string(_val);
112  }
113 
114  bool load_value_from_string(const QString& str) override
115  {
116  return SC<DataType>::cvt_from_string(str, _val);
117  }
118 
119  /* ... */
120  const DataType& value() const
121  {
122  return _val;
123  }
124 
125  /* ... */
126  const DataType& default_value() const
127  {
128  return _default_val;
129  }
130 
131  /* ... */
132  bool assign_value(const DataType& val)
133  {
134  if( _val == val ){
135  return false;
136  }
137 
138  _val = val;
139  return true;
140  }
141 };
142 
143 #endif // SAYONARA_SETTING_H_
The Setting class T is the pure value type e.g. QString.
Definition: Setting.h:75
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: SoundcloudLibraryContainer.h:30
The SettingConverter class.
Definition: SettingConverter.h:41