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.65 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/****************************************************************************
 * **
 * ** 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"

// //////////////////////////////////////////////////////////////////////////
// QBind<QDataStream*,T> support using operator<< for all registered types and containers, or GenericBind with DataTag

//! \warning As with QDataStream, QDataWriter and QDataReader must:
//! - bind the same C++ root data type (when unknown, QVariant can be used on both side)
//! - use the same set of registered QMetaTypeIds with operator<< defined
//! - use compatible ByteOrder, FloatingPointPrecision, and Version
class QDataWriter : public IWriter // allows runtime flexibility using a compile-time QWriter but handles smaller numeric types
{
    Q_DISABLE_COPY(QDataWriter)
public:
    QDataWriter(QDataStream* io) : io(io) { Q_ASSERT(io); }

    // Shortcuts
    Val<Cur<QDataWriter>> value   (                  ) { return Cur<QDataWriter>(this).value()            ; }
    Val<Cur<QDataWriter>> meta    (     QMetaData&& m) { return Cur<QDataWriter>(this).value().meta    (m); }
    Val<Cur<QDataWriter>> meta    (     QMetaData&  m) { return Cur<QDataWriter>(this).value().meta    (m); }
    Seq<Cur<QDataWriter>> sequence(quint32* s=nullptr) { return Cur<QDataWriter>(this).value().sequence(s); }

    template<typename T> QWriter bind(T&& t) { return QWriter(this).value().bind(std::forward<T>(t)); } //!< Only the standard QWriter is compatible with QWritable
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    (     const char* s) { QByteArray ba = QByteArray::fromRawData(s, int(qstrlen(s))); return _bind(ba); } // TODO &&
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    template<typename T>
    bool _bind    (             T&& t) {        *io <<       t; return true; } // Use QVariant to explicit the parts that may be unknown to the reader

    bool _isOk() { return io && io->status()==QDataStream::Ok; }

    bool _out (            ) { return true; }
    bool _item(            ) { return true; }
    bool _item(QUtf8String&) { return true; } // record keys are implicit, maps should be serialized as a sequence of records with key and value items
    bool _item(       QName) { return true; } // for QBaseWriter
private:
    QDataStream* io;
};
template<typename T> struct BindSupport<QDataWriter,T> : BindSupport<IWriter,T> {};
// TODO BindNative smallest numeric types
89
90
template<>           struct BindSupport<QDataWriter, qint32> : BindNative {};
template<>           struct BindSupport<QDataWriter,quint32> : BindNative {};
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
91
// TODO BindNative all types for which T stream operators are defined : QMetaTypeId<typename std::remove_reference<typename std::remove_cv<T>>>::Defined ?
92
93
template<>           struct BindSupport<QDataWriter, QColor> : BindNative {};
template<typename T> struct BindSupport<QDataWriter,      T,typename std::enable_if<
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
94
95
96
97
98
99
        QtPrivate::IsAssociativeContainer<typename std::remove_reference<typename std::remove_cv<T>::type>::type>::Value // Not handled correctly by _record()._item(k) which makes the keys implicit
        >::type
        > : BindNative {};

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

EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
100
// Providing a general QDataReader may be deceiving because the QDataStream structure is implicit:
EXT Arnaud Clère's avatar
EXT Arnaud Clère committed
101
102
// A reader is assumed to specify exactly what to read based on the stream origin and version
// QVariant could be used though for parts which type are not known in advance