Sayonara Player
SettingConverter.h
1 /* SettingConverter.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 #ifndef SETTINGCONVERTER_H
22 #define SETTINGCONVERTER_H
23 
24 #include <QList>
25 #include <QPair>
26 #include <QStringList>
27 
28 class QSize;
29 class QString;
30 class QPoint;
31 // generic
32 template<typename T>
38 {
39 public:
40  static QString cvt_to_string(const T& val){
41  return val.toString();
42  }
43 
44  static bool cvt_from_string(const QString& val, T& ret){
45  ret = T::fromString(val);
46  return true;
47  }
48 };
49 
50 
51 // from bool
52 template<>
57 class SettingConverter<bool>{
58 public:
59  static QString cvt_to_string(const bool& val);
60  static bool cvt_from_string(const QString& val, bool& b);
61 };
62 
63 
64 // for int
65 
70 template<>
71 class SettingConverter<int>{
72 public:
73  static QString cvt_to_string(const int& val);
74  static bool cvt_from_string(const QString& val, int& i);
75 };
76 
77 template<>
78 class SettingConverter<float>{
79 public:
80  static QString cvt_to_string(const float& val);
81  static bool cvt_from_string(const QString& val, float& i);
82 };
83 
84 
85 // for QStringList
86 template<>
91 class SettingConverter<QStringList>{
92 public:
93  static QString cvt_to_string(const QStringList& val);
94  static bool cvt_from_string(const QString& val, QStringList& lst);
95 };
96 
97 
98 // for QString
99 template<>
104 class SettingConverter<QString>{
105 public:
106  static QString cvt_to_string(const QString& val);
107  static bool cvt_from_string(const QString& val, QString& b);
108 };
109 
110 
111 // for QSize
112 template<>
117 class SettingConverter<QSize>{
118 public:
119  static QString cvt_to_string(const QSize& val);
120  static bool cvt_from_string(const QString& val, QSize& sz);
121 };
122 
123 
124 // for QPoint
125 template<>
130 class SettingConverter<QPoint>{
131 public:
132  static QString cvt_to_string(const QPoint& val);
133  static bool cvt_from_string(const QString& val, QPoint& sz);
134 };
135 
136 
137 // for QByteArray
138 template<>
143 class SettingConverter<QByteArray>{
144 public:
145  static QString cvt_to_string(const QByteArray& arr);
146  static bool cvt_from_string(const QString& str, QByteArray& arr);
147 };
148 
149 
150 // generic for lists
151 template<typename T>
157 public:
158  static QString cvt_to_string(const QList<T>& val){
160  QStringList lst;
161 
162  for(const T& v : val){
163  lst << sc.cvt_to_string(v);
164  }
165 
166  return lst.join(",");
167  }
168 
169 
170  static bool cvt_from_string(const QString& val, QList<T>& ret){
172  ret.clear();
173  QStringList lst = val.split(",");
174 
175  for(const QString& l : lst){
176  T v;
177  sc.cvt_from_string(l, v);
178  ret << v;
179  }
180 
181  return true;
182  }
183 };
184 
185 template<typename A, typename B>
190 class SettingConverter< QPair<A,B> >{
191 public:
192  static QString cvt_to_string(const QPair<A,B>& val){
193  A a = val.first;
194  B b = val.second;
195  SettingConverter<A> sc_a;
196  SettingConverter<B> sc_b;
197 
198  return sc_a.cvt_to_string(val.first) + "," + sc_b.cvt_to_string(b);
199  }
200 
201  static bool cvt_from_string(const QString& val, QPair<A,B>& ret){
202  SettingConverter<A> sc_a;
203  SettingConverter<B> sc_b;
204 
205  QStringList lst = val.split(",");
206  QString a, b;
207  bool success = true;
208  if(lst.size() > 0){
209  a = lst[0];
210  }
211 
212  if(lst.size() > 1){
213  b = lst[1];
214  }
215  else
216  {
217  success = false;
218  }
219 
220  sc_a.cvt_from_string (a, ret.first);
221  sc_b.cvt_from_string (b, ret.second);
222 
223  return success;
224  }
225 };
226 
227 #endif // SETTINGCONVERTER_H
The SettingConverter class.
Definition: SettingConverter.h:37
Definition: org_mpris_media_player2_adaptor.h:20