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.41 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
47
48
49
/****************************************************************************
 * **
 * ** 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>

#include "QBind_impl.h"

// //////////////////////////////////////////////////////////////////////////
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
55
//! \warning As with QDataStream, QDataWriter and QDataReader must bind compatible C++ data types, and QDataStream ByteOrder, FloatingPointPrecision and Version
//! \remark When not statically known, such information can be transmitted using meta({{"type",...}}) although some IBind implementations may not support it
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
56
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(QMetaData&)
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 = QAsciiPrintable(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
66
    Val<Cur<QDataWriter>> value   (                  ) { return Cur<QDataWriter>(this).value(); }
    Val<Cur<QDataWriter>> meta    (     QMetaData&& m) { return Cur<QDataWriter>(this).value().meta(m); }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
67
68
    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
69
    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
70
71
72
73
74
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; }
75
    bool _bind    (    const char* u8) { QByteArray ba = QByteArray::fromRawData(u8, int(qstrlen(u8)+sizeof('\0'))); return _bind(ba); }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
76
    template<typename T>
77
    bool _bind    (             T&& t) {        *io <<       t; return true; }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
78

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

    bool _out (            ) { return true; }
    bool _item(            ) { return true; }
83
    bool _item(QIdentifier&) { return true; } // record keys are implicit, maps should be serialized as a sequence of records with key and value items
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
84
    bool _item(       QName) { return true; } // for QBaseWriter
85
86

    void _meta(QMetaData& m) { m[qmDataStreamVersion]=version; }
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
87
88
private:
    QDataStream* io;
89
    QAsciiPrintable version;
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
90
};
91
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
92
93
94

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

95
96
97
98
99
// 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.
// Finally, QDataStream version can be retrieved from meta(QMetaData&) to adapt to data schema changes over the time \see QBind<QColor> for an example