aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qtserialization/inc/qark/serialize_basic.h
blob: 06e69d143c20874ddf51af85f7dd577c7f4913fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/****************************************************************************
**
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#ifndef QARK_SERIALIZE_BASIC_H
#define QARK_SERIALIZE_BASIC_H

#include "parameters.h"
#include "qstringparser/qstringparser.h"

#include <QString>

#include <QPointF>
#include <QRectF>
#include <QDateTime>

#define QARK_BASIC_SAVELOAD(TYPE) \
    template<class Archive> \
    inline void save(Archive &archive, TYPE v, const Parameters &) \
    { \
        archive.write(v); \
    } \
    template<class Archive> \
    inline void load(Archive &archive, TYPE &v, const Parameters &) \
    { \
        archive.read(&v); \
    }

namespace qark {

QARK_BASIC_SAVELOAD(char)
QARK_BASIC_SAVELOAD(signed char)
QARK_BASIC_SAVELOAD(unsigned char)
QARK_BASIC_SAVELOAD(short int)
QARK_BASIC_SAVELOAD(unsigned short int)
QARK_BASIC_SAVELOAD(int)
QARK_BASIC_SAVELOAD(unsigned int)
QARK_BASIC_SAVELOAD(long int)
QARK_BASIC_SAVELOAD(unsigned long int)
QARK_BASIC_SAVELOAD(long long int)
QARK_BASIC_SAVELOAD(unsigned long long int)
QARK_BASIC_SAVELOAD(float)
QARK_BASIC_SAVELOAD(double)
QARK_BASIC_SAVELOAD(long double)
QARK_BASIC_SAVELOAD(bool)

QARK_BASIC_SAVELOAD(QString)

#undef QARK_BASIC_SAVELOAD

// QPointF

template<class Archive>
inline void save(Archive &archive, const QPointF &point, const Parameters &)
{
    archive.write(QString(QStringLiteral("x:%1;y:%2")).arg(point.x()).arg(point.y()));
}

template<class Archive>
inline void load(Archive &archive, QPointF &point, const Parameters &)
{
    QString s;
    archive.read(&s);
    if (QStringParser(s).parse(QStringLiteral("x:%1;y:%2"))
            .arg(point, &QPointF::setX).arg(point, &QPointF::setY).failed()) {
        throw typename Archive::FileFormatException();
    }
}

// QRectF

template<class Archive>
inline void save(Archive &archive, const QRectF &rect, const Parameters &)
{
    archive.write(QString(QStringLiteral("x:%1;y:%2;w:%3;h:%4"))
                  .arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height()));
}

template<class Archive>
inline void load(Archive &archive, QRectF &point, const Parameters &)
{
    QString s;
    archive.read(&s);
    if (QStringParser(s).parse(QStringLiteral("x:%1;y:%2;w:%3;h:%4"))
            .arg(point, &QRectF::setX).arg(point, &QRectF::setY)
            .arg(point, &QRectF::setWidth).arg(point, &QRectF::setHeight).failed()) {
        throw typename Archive::FileFormatException();
    }
}

// QDateTime

template<class Archive>
inline void save(Archive &archive, const QDateTime &dateTime, const Parameters &)
{
    archive << dateTime.toMSecsSinceEpoch();
}

template<class Archive>
inline void load(Archive &archive, QDateTime &dateTime, const Parameters &)
{
    qint64 t;
    archive >> t;
    dateTime = QDateTime::fromMSecsSinceEpoch(t);
}

} // namespace qark

#endif // QARK_SERIALIZE_BASIC_H