Sayonara Player
SettingConverter.h
1 /* SettingConverter.h */
2 
3 /* Copyright (C) 2011-2019 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 "Utils/typedefs.h"
25 
26 #include <QPair>
27 #include <QStringList>
28 
29 #include <exception>
30 #include <iostream>
31 
32 class QSize;
33 class QPoint;
34 class SettingConvertible;
35 
41 {
42  QString to_string(const SettingConvertible& t);
43  bool from_string(const QString& val, SettingConvertible& t);
44 
45  QString to_string(const bool& val);
46  bool from_string(const QString& val, bool& b);
47 
48  QString to_string(const int& val);
49  bool from_string(const QString& val, int& i);
50 
51  QString to_string(const float& val);
52  bool from_string(const QString& val, float& i);
53 
54  QString to_string(const QStringList& val);
55  bool from_string(const QString& val, QStringList& lst);
56 
57  QString to_string(const QString& val);
58  bool from_string(const QString& val, QString& b);
59 
60  QString to_string(const QSize& val);
61  bool from_string(const QString& val, QSize& sz);
62 
63  QString to_string(const QPoint& val);
64  bool from_string(const QString& val, QPoint& sz);
65 
66  QString to_string(const QByteArray& arr);
67  bool from_string(const QString& str, QByteArray& arr);
68 
69  template<typename A, typename B>
70  QString to_string(const QPair<A, B>& arr)
71  {
72  return to_string(arr.first) + "," + to_string(arr.second);
73  }
74 
75  template<typename A, typename B>
76  bool from_string(const QString& str, QPair<A, B>& pair)
77  {
78  QStringList lst = str.split(",");
79 
80  if(lst.size() >= 2)
81  {
82  from_string(lst[0], pair.first);
83  from_string(lst[1], pair.second);
84  }
85 
86  return (lst.size() >= 2);
87  }
88 
89  template<typename T>
90  QString to_string(const QList<T>& val)
91  {
92  QStringList lst;
93 
94  for(const T& v : val) {
95  lst << to_string(v);
96  }
97 
98  return lst.join(",");
99  }
100 
101  template<typename T>
102  bool from_string(const QString& val, QList<T>& ret)
103  {
104  ret.clear();
105  QStringList lst = val.split(",");
106 
107  for(const QString& l : lst)
108  {
109  try
110  {
111  T v;
112  if(from_string(l, v)){
113  ret << v;
114  }
115  } catch (std::exception& e) {
116  std::cerr << e.what() << std::endl;
117  }
118  }
119 
120  return true;
121  }
122 }
123 
124 #endif // SETTINGCONVERTER_H
Definition: SettingConvertible.h:28
The SettingConverter<bool> class.
Definition: SettingConverter.h:40
Definition: typedefs.h:32
Definition: EngineUtils.h:33