Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

QSettings_impl.h 8.09 KB
Newer Older
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
/****************************************************************************
 * **
 * ** Copyright (C) 2017 MinMaxMedical.
 * ** All rights reserved.
 * ** Contact: MinMaxMedical <InCAS@MinMaxMedical.com>
 * **
 * ** This file is part of the modmedLog module.
 * **
 * ** $QT_BEGIN_LICENSE:BSD$
 * ** You may use this file under the terms of the BSD license as follows:
 * **
 * ** "Redistribution and use in source and binary forms, with or without
 * ** modification, are permitted provided that the following conditions are
 * ** met:
 * **   * Redistributions of source code must retain the above copyright
 * **     notice, this list of conditions and the following disclaimer.
 * **   * Redistributions in binary form must reproduce the above copyright
 * **     notice, this list of conditions and the following disclaimer in
 * **     the documentation and/or other materials provided with the
 * **     distribution.
 * **   * Neither the name of MinMaxMedical S.A.S. and its Subsidiary(-ies) nor
 * **     the names of its contributors may be used to endorse or promote
 * **     products derived from this software without specific prior written
 * **     permission.
 * **
 * ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 * ** $QT_END_LICENSE$
 * **
 * ****************************************************************************/
#pragma once

#include <QtCore/qsettings.h>
#include <QtCore/qvariant.h>
#include <QtCore/qstack.h>

#include "QBind_impl.h"

// //////////////////////////////////////////////////////////////////////////
// QBind<T,QVariant*> support for the fixed set of QSettingsWriter's BindNative types

class QSettingsWriter : public IWriter
{
    Q_DISABLE_COPY(QSettingsWriter)
public:
    QSettingsWriter(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level("")); }

    // Shortcuts
    /**/             Val<Cursor> value   (                  ) { return Cursor(this).value(); }
    /**/             Seq<Cursor> sequence(quint32* s=nullptr) { return Cursor(this).value().sequence            ( s); }
    template<typename T> Cursor  bind    (             T&& t) { return Cursor(this).value().bind(std::forward<T>(t)); }
protected:
    template<typename T>
63
64
65
66
67
68
    bool _bind    (         T&& t) { settings->setValue(key(), QVariant::fromValue( t) ); return true; }
    bool _bind    (    quint8&& t) { settings->setValue(key(), QVariant       (int( t))); return true; }
    bool _bind    (   quint16&& t) { settings->setValue(key(), QVariant       (int( t))); return true; }
    bool _bind    (   quint32&& t) { settings->setValue(key(), QVariant       (int( t))); return true; }
    bool _bind    (const char* u8) { settings->setValue(key(), QVariant           (u8) ); return true; }
    bool _null    (              ) { settings->setValue(key(), QVariant           (  ) ); return true; }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    bool _sequence(quint32* =nullptr) { settings->beginGroup(key()); levels.push(Level(  )); return true; }
    bool _record  (quint32* =nullptr) { settings->beginGroup(key()); levels.push(Level("")); return true; }

    bool _item(QName n) { levels.last().key=n; return true; }
    bool _item(       ) { levels.last().idx++; return true; }
    bool _out (       ) { levels.pop (); settings->endGroup(); return true; }
private:
    QString key() {
        Q_ASSERT(!levels.isEmpty());
        return levels.last().key!=nullptr
            ? QString        (levels.last().key)
            : QString::number(levels.last().idx);
    }

    QSettings* settings;
85
    struct Level { QName key; quint32 idx=0; Level(QName k=QName()) : key(k) {} };
86
87
88
89
90
91
92
93
94
95
96
97
    QStack<Level> levels = QStack<Level>(); //!< minimal dynamic context to implement out() and ensure actual building in case QSettingsWriter is abandoned
};

// --------------------------------------------------------------------------

class QSettingsReader : public IReader
{
    Q_DISABLE_COPY(QSettingsReader)
public:
    Q_ENABLE_MOVE(QSettingsReader, std::swap(isChoice, o.isChoice); )
    QSettingsReader(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level("")); }

98
    struct Error { QName error; QUtf8String path; template<class T> T bind(Val<T>&& value) { return value.bind(QUtf8String(error.utf8())+' '+path); } };
99
100
101
102
103
    QVector<Error> errors;

    QUtf8String currentPath() {
        QUtf8String path;
        Q_FOREACH(Level l, levels) {
104
105
            if (l.key.isNull()) { path.append('/').append(                    l.key.utf8()); }
            else                { path.append('/').append(QUtf8String::number(l.idx      )); }
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
        }
        return path;
    }

    // Shortcuts
    template<typename T> Cursor bind(T&& t) { return Cursor(this).value().bind(std::forward<T>(t)); }
protected:
    template<typename T>
    bool _bind(T& t) { return set(t, "Expected declared metatype T"); }

    bool _bind(QUtf8String& t) { return set(t, qBindExpectedText   ); }
    bool _bind(    QString& t) { return set(t, qBindExpectedText   ); }
    bool _bind(       bool& t) { return set(t, qBindExpectedBoolean); }
    bool _bind( QByteArray& t) { return set(t, qBindExpectedBytes  ); }
    // Convert numerical types to strictly larger ones // TODO convert all compatible values
    bool _bind(      qint8& t) { return set(t, qBindExpectedInteger       ); }
    bool _bind(     qint16& t) { return set(t, qBindExpectedInteger       ); }
    bool _bind(     qint32& t) { return set(t, qBindExpectedInteger       ); }
    bool _bind(     qint64& t) { return set(t, qBindExpectedInteger       ); }
    bool _bind(     quint8& t) { return set(t, qBindExpectedPositiveNumber); }
    bool _bind(    quint16& t) { return set(t, qBindExpectedPositiveNumber); }
    bool _bind(    quint32& t) { return set(t, qBindExpectedPositiveNumber); }
    bool _bind(    quint64& t) { return set(t, qBindExpectedPositiveNumber); }
    bool _bind(      float& t) { return set(t, qBindExpectedDecimal       ); }
    bool _bind(     double& t) { return set(t, qBindExpectedDecimal       ); }

    bool _sequence(quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level(  )); return true; } _reportError(qBindExpectedSequence); return false; }
    bool _record  (quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level("")); return true; } _reportError(qBindExpectedRecord  ); return false; }
    bool _null    (                 ) { if (settings->value(key()).isNull())               { return true; } _reportError(qBindExpectedNull    ); return false; }

136
137
    bool _item(QUtf8String& k) { levels.last().key=QName(k)        ; return true; }
    bool _item(              ) { levels.last().idx++               ; return true; }
138
139
140
141
    bool _out (              ) { levels.pop(); settings->endGroup(); return true; }

    bool _isOk() { return settings; }
    void _setChoice(bool v) { isChoice=v; }
142
    void _reportError(QName error) { if (!isChoice) errors.append(Error{ error, currentPath() }); }
143
144
private:
    template<typename T>
145
    bool set(T& t, QName error) { QVariant v = settings->value(key()); if (v.convert(qMetaTypeId<T>())) { t = v.value<T>(); return true; } _reportError(error); return false; }
146
147
148
149
150
151
152
153
    QString key() {
        Q_ASSERT(!levels.isEmpty());
        return levels.last().key!=nullptr
            ? QString        (levels.last().key)
            : QString::number(levels.last().idx);
    }

    QSettings* settings;
154
    struct Level { QName key; int idx=-1; bool isGroup=true; Level(QName n=QName(), bool isGroup=true) : key(n), isGroup(isGroup) {} };
155
156
157
    QStack<Level> levels;
    bool isChoice = false;
};