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 <QPair>
25 #include <QStringList>
26 #include "Utils/typedefs.h"
27 
28 #include <exception>
29 #include <iostream>
30 
31 class QSize;
32 class QString;
33 class QPoint;
34 
35 // generic
36 template<typename T>
42 {
43 public:
44  static QString cvt_to_string(const T& val){
45  return val.toString();
46  }
47 
48  static bool cvt_from_string(const QString& val, T& ret){
49  try {
50  ret = T::fromString(val);
51  return true;
52  }
53  catch(std::exception& e)
54  {
55  std::cerr << e.what() << std::endl;
56  return false;
57  }
58  }
59 };
60 
61 
62 // from bool
63 template<>
68 class SettingConverter<bool>{
69 public:
70  static QString cvt_to_string(const bool& val);
71  static bool cvt_from_string(const QString& val, bool& b);
72 };
73 
74 
75 // for int
76 
81 template<>
82 class SettingConverter<int>{
83 public:
84  static QString cvt_to_string(const int& val);
85  static bool cvt_from_string(const QString& val, int& i);
86 };
87 
88 template<>
89 class SettingConverter<float>{
90 public:
91  static QString cvt_to_string(const float& val);
92  static bool cvt_from_string(const QString& val, float& i);
93 };
94 
95 
96 // for QStringList
97 template<>
102 class SettingConverter<QStringList>{
103 public:
104  static QString cvt_to_string(const QStringList& val);
105  static bool cvt_from_string(const QString& val, QStringList& lst);
106 };
107 
108 
109 // for QString
110 template<>
115 class SettingConverter<QString>{
116 public:
117  static QString cvt_to_string(const QString& val);
118  static bool cvt_from_string(const QString& val, QString& b);
119 };
120 
121 
122 // for QSize
123 template<>
128 class SettingConverter<QSize>{
129 public:
130  static QString cvt_to_string(const QSize& val);
131  static bool cvt_from_string(const QString& val, QSize& sz);
132 };
133 
134 
135 // for QPoint
136 template<>
141 class SettingConverter<QPoint>{
142 public:
143  static QString cvt_to_string(const QPoint& val);
144  static bool cvt_from_string(const QString& val, QPoint& sz);
145 };
146 
147 
148 // for QByteArray
149 template<>
154 class SettingConverter<QByteArray>{
155 public:
156  static QString cvt_to_string(const QByteArray& arr);
157  static bool cvt_from_string(const QString& str, QByteArray& arr);
158 };
159 
160 
161 // generic for lists
162 template<typename T>
168 public:
169  static QString cvt_to_string(const QList<T>& val)
170  {
172  QStringList lst;
173 
174  for(const T& v : val){
175  lst << sc.cvt_to_string(v);
176  }
177 
178  return lst.join(",");
179  }
180 
181 
182  static bool cvt_from_string(const QString& val, QList<T>& ret)
183  {
185  ret.clear();
186  QStringList lst = val.split(",");
187 
188  for(const QString& l : lst)
189  {
190  T v;
191  try {
192  if(sc.cvt_from_string(l, v)){
193  ret << v;
194  }
195  } catch (std::exception& e) {
196  std::cerr << e.what() << std::endl;
197  }
198  }
199 
200  return true;
201  }
202 };
203 
204 // generic for lists
205 template<>
210 class SettingConverter< BoolList >{
211 public:
212  static QString cvt_to_string(const BoolList& val)
213  {
215  QStringList lst;
216 
217  for(const bool& v : val){
218  lst << sc.cvt_to_string(v);
219  }
220 
221  return lst.join(",");
222  }
223 
224 
225  static bool cvt_from_string(const QString& val, BoolList& ret)
226  {
228  ret.clear();
229  QStringList lst = val.split(",");
230 
231  for(const QString& l : lst){
232  bool v;
233  sc.cvt_from_string(l, v);
234  ret.push_back(v);
235  }
236 
237  return true;
238  }
239 };
240 
241 template<typename A, typename B>
246 class SettingConverter< QPair<A,B> >{
247 public:
248  static QString cvt_to_string(const QPair<A,B>& val){
249  A a = val.first;
250  B b = val.second;
251  SettingConverter<A> sc_a;
252  SettingConverter<B> sc_b;
253 
254  return sc_a.cvt_to_string(val.first) + "," + sc_b.cvt_to_string(b);
255  }
256 
257  static bool cvt_from_string(const QString& val, QPair<A,B>& ret){
258  SettingConverter<A> sc_a;
259  SettingConverter<B> sc_b;
260 
261  QStringList lst = val.split(",");
262  QString a, b;
263  bool success = true;
264  if(lst.size() > 0){
265  a = lst[0];
266  }
267 
268  if(lst.size() > 1){
269  b = lst[1];
270  }
271  else
272  {
273  success = false;
274  }
275 
276  sc_a.cvt_from_string (a, ret.first);
277  sc_b.cvt_from_string (b, ret.second);
278 
279  return success;
280  }
281 };
282 
283 #endif // SETTINGCONVERTER_H
Definition: typedefs.h:28
The SettingConverter<bool> class.
Definition: SettingConverter.h:68
The SettingConverter class.
Definition: SettingConverter.h:41
Definition: org_mpris_media_player2_adaptor.h:20