swift
Loading...
Searching...
No Matches
mixindatastream.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_MIXIN_MIXINDATASTREAM_H
7#define SWIFT_MISC_MIXIN_MIXINDATASTREAM_H
8
9#include <QDataStream>
10
12#include "misc/metaclass.h"
13
14namespace swift::misc
15{
16 class CEmpty;
17
18 namespace mixin
19 {
26 template <class Derived>
28 {
29 public:
31 friend QDataStream &operator<<(QDataStream &stream, const Derived &value)
32 {
33 value.marshalToDataStream(stream);
34 return stream;
35 }
36
38 friend QDataStream &operator>>(QDataStream &stream, Derived &value)
39 {
40 value.unmarshalFromDataStream(stream);
41 return stream;
42 }
43 };
44
50 template <class Derived>
52 {
53 public:
55 void marshalToDataStream(QDataStream &stream) const;
56
59
60 private:
61 const Derived *derived() const;
62 Derived *derived();
63
64 template <typename T>
65 static void baseMarshal(const T *base, QDataStream &stream);
66 template <typename T>
67 static void baseUnmarshal(T *base, QDataStream &stream);
68 static void baseMarshal(const void *, QDataStream &);
69 static void baseUnmarshal(void *, QDataStream &);
70 static void baseMarshal(const CEmpty *, QDataStream &);
71 static void baseUnmarshal(CEmpty *, QDataStream &);
72 };
73
74 template <class Derived>
76 {
77 baseMarshal(static_cast<const TBaseOfT<Derived> *>(derived()), stream);
78 introspect<Derived>().forEachMember([&, this](auto member) {
79 if constexpr (!decltype(member)::has(MetaFlags<DisabledForMarshalling>()))
80 {
81 stream << member.in(*this->derived());
82 }
83 });
84 }
85
86 template <class Derived>
88 {
89 baseUnmarshal(static_cast<TBaseOfT<Derived> *>(derived()), stream);
90 introspect<Derived>().forEachMember([&, this](auto member) {
91 if constexpr (!decltype(member)::has(MetaFlags<DisabledForMarshalling>()))
92 {
93 stream >> member.in(*this->derived());
94 }
95 });
96 }
97
98 template <class Derived>
99 const Derived *DataStreamByMetaClass<Derived>::derived() const
100 {
101 return static_cast<const Derived *>(this);
102 }
103
104 template <class Derived>
105 Derived *DataStreamByMetaClass<Derived>::derived()
106 {
107 return static_cast<Derived *>(this);
108 }
109
110 template <class Derived>
111 template <typename T>
112 void DataStreamByMetaClass<Derived>::baseMarshal(const T *base, QDataStream &stream)
113 {
114 base->marshalToDataStream(stream);
115 }
116
117 template <class Derived>
118 template <typename T>
119 void DataStreamByMetaClass<Derived>::baseUnmarshal(T *base, QDataStream &stream)
120 {
121 base->unmarshalFromDataStream(stream);
122 }
123
124 template <class Derived>
125 void DataStreamByMetaClass<Derived>::baseMarshal(const void *, QDataStream &)
126 {}
127
128 template <class Derived>
129 void DataStreamByMetaClass<Derived>::baseUnmarshal(void *, QDataStream &)
130 {}
131
132 template <class Derived>
133 void DataStreamByMetaClass<Derived>::baseMarshal(const CEmpty *, QDataStream &)
134 {}
135
136 template <class Derived>
137 void DataStreamByMetaClass<Derived>::baseUnmarshal(CEmpty *, QDataStream &)
138 {}
139
144#define SWIFT_MISC_DECLARE_USING_MIXIN_DATASTREAM(DERIVED) \
145 using ::swift::misc::mixin::DataStreamByMetaClass<DERIVED>::marshalToDataStream; \
146 using ::swift::misc::mixin::DataStreamByMetaClass<DERIVED>::unmarshalFromDataStream;
147 } // namespace mixin
148} // namespace swift::misc
149
150#endif // SWIFT_MISC_MIXIN_MIXINDATASTREAM_H
Default base class for CValueObject.
Definition valueobject.h:76
CRTP class template to generate QDataStream marshalling methods using CMetaClass.
void marshalToDataStream(QDataStream &stream) const
Marshal a value to a QDataStream.
void unmarshalFromDataStream(QDataStream &stream)
Unmarshal a value from a QDataStream.
CRTP class template to generate non-member QDataStream streaming operators.
friend QDataStream & operator>>(QDataStream &stream, Derived &value)
Unmarshal a value from a QDataStream.
friend QDataStream & operator<<(QDataStream &stream, const Derived &value)
Marshal a value to a QDataStream.
constexpr auto introspect()
Obtain the CMetaClassIntrospector for the metaclass of T.
Definition metaclass.h:285
Mixin classes which implement common operations for value classes.
Free functions in swift::misc.
typename TBaseOf< T >::type TBaseOfT
Alias for typename TBaseOf<T>::type.
Type wrapper for passing MetaFlag to CMetaMember::has.
Definition metaclass.h:117