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/

QData_impl.h 5.33 KB
Newer Older
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
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
/****************************************************************************
 * **
 * ** Copyright (C) 2018 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 <type_traits>

#include <QtCore/qdatastream.h>
#include <QtCore/qvariant.h>

47
#include "QBind.h"
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
48
49

// //////////////////////////////////////////////////////////////////////////
50
// QBind<T,QDataStream*> support
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
51

EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
52
53
// TODO Use QByteArray directly

54
//! \warning As with QDataStream, QDataWriter and QDataReader must bind compatible C++ data types, and QDataStream ByteOrder, FloatingPointPrecision and Version
55
56
//! \remark When not statically known, such information can be transmitted using meta("type",...) although some IBind implementations may not support it
class QDataWriter : public IWriter // allows runtime flexibility but requires QBind<T> in addition to T::operator<<(QDataStream&) and does not warrant QDataStream operators compatibility if QBind<T> ignores qmDataStreamVersion in meta()
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
57
58
59
{
    Q_DISABLE_COPY(QDataWriter)
public:
60
    QDataWriter(QDataStream* io) : io(io) { Q_ASSERT(io); version = QAsciiData(QByteArray::number(io->version())); }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
61

62
    BindMode mode() const noexcept { return BindMode::Write; }
63

EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
64
    // Shortcuts
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
65
    Val<Cur<QDataWriter>> value   (                  ) { return Cur<QDataWriter>(this).value(); }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
66
67
    Seq<Cur<QDataWriter>> sequence(quint32* s=nullptr) { return Cur<QDataWriter>(this).value().sequence(s); }

EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
68
    template<typename T> Cur<QDataWriter>  bind(T&& t) { return Cur<QDataWriter>(this).value().bind(std::forward<T>(t)); }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
69
70
71
72
73
protected:
    friend class Cur<QDataWriter>;
    bool _sequence(quint32* s=nullptr) { if (s) *io <<      *s; return true; }
    bool _record  (quint32* s=nullptr) { if (s) *io <<      *s; return true; }
    bool _null    (                  ) {        *io << nullptr; return true; }
74
    bool _bind    (   QUtf8DataView u) { QByteArray ba = QByteArray::fromRawData(u.data(), u.size()+int(sizeof('\0'))); return _bind(ba); }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
75
    template<typename T>
76
    bool _bind    (             T&& t) {        *io <<       t; return true; }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
77

78
    bool _isOk() noexcept { return io && io->status()==QDataStream::Ok; }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
79

80
81
82
83
    bool _out (                  ) { return true; }
    bool _item(                  ) { return true; }
    bool _item(QIdentifier&      ) { return true; } // record keys are implicit, maps should be serialized as a sequence of records with key and value items
    bool _item(QIdentifierLiteral) { return true; } // for QBaseWriter
84

85
    void _meta(QIdentifierLiteral& n, QAsciiData& m) { if (n == qmDataStreamVersion) m=version; }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
86
87
private:
    QDataStream* io;
88
    QAsciiData version;
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
89
};
90
template<typename T> struct BindSupport<T     ,QDataWriter> : BindNative {}; //!< \remark Use meta() or QVariant to encapsulate Ts that are not statically known (provided qRegisterMetaType<T>() and qRegisterMetaTypeStreamOperators<T>() are called)
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
91
92
93

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

94
95
96
97
// TODO QDataReader
// \remark Providing a general QDataReader may be deceiving because the QDataStream structure is implicit, thus
// a reader is usually assumed to statically know the data type to read based on the stream origin and version.
// Though QVariant can be used for parts which type are not statically known though, such as the dynamic type of a message coming from a socket.
98
// Finally, QDataStream version can be retrieved from meta() to adapt to data schema changes over the time \see QBind<QColor> for an example