1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Jochen Becher
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #pragma once
27
28 #include "parameters.h"
29 #include "qstringparser/qstringparser.h"
30
31 #include <QString>
32
33 #include <QPointF>
34 #include <QRectF>
35 #include <QDateTime>
36
37 #define QARK_BASIC_SAVELOAD(TYPE) \
38 template<class Archive> \
39 inline void save(Archive &archive, TYPE v, const Parameters &) \
40 { \
41 archive.write(v); \
42 } \
43 template<class Archive> \
44 inline void load(Archive &archive, TYPE &v, const Parameters &) \
45 { \
46 archive.read(&v); \
47 }
48
49 namespace qark {
50
51 QARK_BASIC_SAVELOAD(char)
QARK_BASIC_SAVELOAD(signed char)52 QARK_BASIC_SAVELOAD(signed char)
53 QARK_BASIC_SAVELOAD(unsigned char)
54 QARK_BASIC_SAVELOAD(short int)
55 QARK_BASIC_SAVELOAD(unsigned short int)
56 QARK_BASIC_SAVELOAD(int)
57 QARK_BASIC_SAVELOAD(unsigned int)
58 QARK_BASIC_SAVELOAD(long int)
59 QARK_BASIC_SAVELOAD(unsigned long int)
60 QARK_BASIC_SAVELOAD(long long int)
61 QARK_BASIC_SAVELOAD(unsigned long long int)
62 QARK_BASIC_SAVELOAD(float)
63 QARK_BASIC_SAVELOAD(double)
64 QARK_BASIC_SAVELOAD(long double)
65 QARK_BASIC_SAVELOAD(bool)
66
67 QARK_BASIC_SAVELOAD(QString)
68
69 #undef QARK_BASIC_SAVELOAD
70
71 // QPointF
72
73 template<class Archive>
74 inline void save(Archive &archive, const QPointF &point, const Parameters &)
75 {
76 archive.write(QString("x:%1;y:%2").arg(point.x()).arg(point.y()));
77 }
78
79 template<class Archive>
load(Archive & archive,QPointF & point,const Parameters &)80 inline void load(Archive &archive, QPointF &point, const Parameters &)
81 {
82 QString s;
83 archive.read(&s);
84 if (QStringParser(s).parse("x:%1;y:%2")
85 .arg(point, &QPointF::setX).arg(point, &QPointF::setY).failed()) {
86 throw typename Archive::FileFormatException();
87 }
88 }
89
90 // QRectF
91
92 template<class Archive>
save(Archive & archive,const QRectF & rect,const Parameters &)93 inline void save(Archive &archive, const QRectF &rect, const Parameters &)
94 {
95 archive.write(QString("x:%1;y:%2;w:%3;h:%4")
96 .arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height()));
97 }
98
99 template<class Archive>
load(Archive & archive,QRectF & point,const Parameters &)100 inline void load(Archive &archive, QRectF &point, const Parameters &)
101 {
102 QString s;
103 archive.read(&s);
104 if (QStringParser(s).parse("x:%1;y:%2;w:%3;h:%4")
105 .arg(point, &QRectF::setX).arg(point, &QRectF::setY)
106 .arg(point, &QRectF::setWidth).arg(point, &QRectF::setHeight).failed()) {
107 throw typename Archive::FileFormatException();
108 }
109 }
110
111 // QDateTime
112
113 template<class Archive>
save(Archive & archive,const QDateTime & dateTime,const Parameters &)114 inline void save(Archive &archive, const QDateTime &dateTime, const Parameters &)
115 {
116 archive << dateTime.toMSecsSinceEpoch();
117 }
118
119 template<class Archive>
load(Archive & archive,QDateTime & dateTime,const Parameters &)120 inline void load(Archive &archive, QDateTime &dateTime, const Parameters &)
121 {
122 qint64 t;
123 archive >> t;
124 dateTime = QDateTime::fromMSecsSinceEpoch(t);
125 }
126
127 } // namespace qark
128