root/src/nwutils.h

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
131
132
133
134
135
#ifndef NW_UTILS_H
#define NW_UTILS_H

#include <N0Slib.h>
#include <QDate>
#include <QString>
#include <QStringList>

class NwUtils
{
public:
    NwUtils();

    template<class T> static void describe(T& d, const nw::String key, QDate& value) {
        if (d.isInWriteMode()) {
            std::string str = value.toString(Qt::ISODate).toStdString();
            d.describe(key, str);
        } else {
            std::string str;
            d.describe(key, str);
            value = QDate::fromString(QString(str.data()), Qt::ISODate);
        }
    }

    template<class T> static void describe(T& d, const nw::String key, QTime& value) {
        if (d.isInWriteMode()) {
            std::string str = value.toString(Qt::ISODate).toStdString();
            d.describe(key, str);
        } else {
            std::string str;
            d.describe(key, str);
            value = QTime::fromString(QString(str.data()), Qt::ISODate);
        }
    }

    template<class T> static void describe(T& d, const nw::String key, QDateTime& value) {
        if (d.isInWriteMode()) {
            std::string str = value.toString(Qt::ISODate).toStdString();
            d.describe(key, str);
        } else {
            std::string str;
            d.describe(key, str);
            value = QDateTime::fromString(QString(str.data()), Qt::ISODate);
        }
    }
    template<class T> static void describe(T& d, const nw::String key, QString& value) {
        d.describeName(key);
        describeValue(d, value);
    }

    template<class T> static void describeValue(T& d, QString& value) {
        if (d.isInWriteMode()) {
            std::string str = value.toStdString();
            d.describeValue(str);
        } else {
            std::string str;
            d.describeValue(str);
            value = QString(str.data());
        }
    }

    template<class T> static void describeConst(T& d, const nw::String key, const QString& value) {
        d.describeName(key);
        describeValueConst(d, value);
    }

    template<class T> static void describeValueConst(T& d, const QString& value) {
        std::string str = value.toStdString();
        d.describeValueConst(str);
    }

    template<class T> static void describe(T& d, const nw::String key, QStringList& value, char separator) {
        if (d.isInWriteMode()) {
            std::stringstream ss;
            for (int i=0; i < value.length(); ++i) {
                if (i != 0) {
                    ss << separator;
                }
                ss << value.at(i).toStdString();
            }
            std::string str = ss.str();
            d.describe(key, str);
        } else {
            std::string str;
            d.describe(key, str);
            value = QString(str.data()).split(separator);
        }
    }

    template<class T> static void describeValueArray(T& d, const nw::String arrayName, QStringList& value) {
        if (d.isInReadMode()) {
            value.clear();
        }

        d.describeValueArray(arrayName, value.length());
        for (int i=0; d.enterNextElement(i); ++i) {
            nw::String str;
            if (d.isInWriteMode()) {
                str = value[i].toStdString();
            }
            d.describeValue(str);
            if (d.isInReadMode()) {
                value.append(str.data());
            }
        }
    }

    template<class T, class T2> static void describeValueArray(T& d, const nw::String arrayName, QList<T2>& value) {
        if (d.isInReadMode()) {
            value.clear();
        }

        d.describeValueArray(arrayName, value.length());
        for (int i=0; d.enterNextElement(i); ++i) {
            T2 val;
            if (d.isInWriteMode()) {
                val = value[i];
            }
            d.describeValue(val);
            if (d.isInReadMode()) {
                value.append(val);
            }
        }
    }

    template<class T, class T2> inline static void describe(T& d, const nw::String key, T2& value) {
        d.describe(key, value);
    }

    template<class T, class T2> inline static void describeConst(T& d, const nw::String key, const T2& value) {
        d.describeConst(key, value);
    }
};

#endif // NW_UTILS_H