Sayonara Player
Loading...
Searching...
No Matches
SettingConverter.h
1/* SettingConverter.h */
2
3/* Copyright (C) 2011-2024 Michael Lugmair (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 SAYONARA_PLAYER_SETTINGCONVERTER_H
22#define SAYONARA_PLAYER_SETTINGCONVERTER_H
23
24#include "Utils/typedefs.h"
25
26#include <QPair>
27#include <QStringList>
28
29#include <exception>
30#include <iostream>
31
32class QSize;
33class QPoint;
35
36namespace SettingConverter
37{
38 QString toString(const SettingConvertible& t);
39 bool fromString(const QString& val, SettingConvertible& t);
40
41 QString toString(const bool& val);
42 bool fromString(const QString& val, bool& b);
43
44 QString toString(const int& val);
45 bool fromString(const QString& val, int& i);
46
47 template<typename T, typename = typename std::enable_if<std::is_enum<T>::value, T>::type>
48 QString toString(const T& val)
49 {
50 int i = static_cast<int>(val);
51 return toString(i);
52 }
53
54 template<typename T, typename = typename std::enable_if<std::is_enum<T>::value, T>::type>
55 bool fromString(const QString& val, T& e)
56 {
57 int i = 0;
58 const auto b = fromString(val, i);
59
60 e = static_cast<T>(i);
61 return b;
62 }
63
64 QString toString(const float& val);
65 bool fromString(const QString& val, float& i);
66
67 QString toString(const QStringList& val);
68 bool fromString(const QString& val, QStringList& lst);
69
70 QString toString(const QString& val);
71 bool fromString(const QString& val, QString& b);
72
73 QString toString(const QSize& val);
74 bool fromString(const QString& val, QSize& sz);
75
76 QString toString(const QPoint& val);
77 bool fromString(const QString& val, QPoint& sz);
78
79 QString toString(const QByteArray& arr);
80 bool fromString(const QString& str, QByteArray& arr);
81
82 template<typename A, typename B>
83 QString toString(const QPair<A, B>& arr)
84 {
85 return toString(arr.first) + "," + toString(arr.second);
86 }
87
88 template<typename A, typename B>
89 bool fromString(const QString& str, QPair<A, B>& pair)
90 {
91 QStringList lst = str.split(",");
92
93 if(lst.size() >= 2)
94 {
95 fromString(lst[0], pair.first);
96 fromString(lst[1], pair.second);
97 }
98
99 return (lst.size() >= 2);
100 }
101
102 template<typename T>
103 QString toString(const QList<T>& val)
104 {
105 QStringList lst;
106
107 for(const T& v: val)
108 {
109 lst << toString(v);
110 }
111
112 return lst.join(",");
113 }
114
115 template<typename T>
116 bool fromString(const QString& val, QList<T>& ret)
117 {
118 ret.clear();
119 QStringList lst = val.split(",");
120
121 for(const auto& item: lst)
122 {
123 try
124 {
125 T v;
126 if(fromString(item, v))
127 {
128 ret << v;
129 }
130 } catch(std::exception& e)
131 {
132 std::cerr << e.what() << std::endl;
133 }
134 }
135
136 return true;
137 }
138}
139
140#endif // SAYONARA_PLAYER_SETTINGCONVERTER_H
Definition SettingConvertible.h:27