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 T,
69  template <typename Arg> class SC = SettingConverter >
75 class Setting : public AbstrSetting
76 {
77  private:
78  Setting();
79  Setting(const Setting&);
80 
81  T _val;
82  T _default_val;
83 
84  public:
85 
86  /* Constructor */
87  template<SettingKey keyIndex>
88  Setting(const SettingIdentifier<T, keyIndex>& identifier, const char* db_key, T def) :
89  AbstrSetting(keyIndex, db_key)
90  {
91  Q_UNUSED(identifier);
92  _default_val = def;
93  _val = def;
94  }
95 
96  template<SettingKey keyIndex>
97  Setting(const SettingIdentifier<T, keyIndex>& identifier, T def) :
98  AbstrSetting(keyIndex)
99  {
100  Q_UNUSED(identifier);
101  _default_val = def;
102  _val = def;
103  }
104 
105  /* Destructor */
106  ~Setting() {}
107 
108  void assign_default_value() override
109  {
110  _val = _default_val;
111  }
112 
113  QString value_to_string() const override
114  {
115  return SC<T>::cvt_to_string(_val);
116  }
117 
118  bool load_value_from_string(const QString& str) override
119  {
120  return SC<T>::cvt_from_string(str, _val);
121  }
122 
123  /* ... */
124  const T& value() const
125  {
126  return _val;
127  }
128 
129  /* ... */
130  const T& default_value() const
131  {
132  return _default_val;
133  }
134 
135  /* ... */
136  bool assign_value(const T& val)
137  {
138  if( _val == val ){
139  return false;
140  }
141 
142  _val = val;
143  return true;
144  }
145 };
146 
147 #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: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: SoundcloudLibraryContainer.h:30
The SettingConverter class.
Definition: SettingConverter.h:38
Definition: SettingKey.h:197