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