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