DataStreamHelpers.hpp
1 #pragma once
2 
3 #include <score/tools/Debug.hpp>
4 
5 #include <QByteArray>
6 #include <QColor>
7 #include <QDataStream>
8 #include <QPoint>
9 #include <QPointF>
10 #include <QPointer>
11 #include <QRect>
12 #include <QRectF>
13 #include <QSize>
14 #include <QSizeF>
15 #include <QString>
16 #include <QVariant>
17 #include <QVector2D>
18 #include <QVector3D>
19 #include <QVector4D>
20 #include <QtContainerFwd>
21 
22 #include <score_lib_base_export.h>
23 
24 #include <memory>
25 #include <string>
26 #include <type_traits>
27 
28 template <typename T>
29 struct is_shared_ptr : std::false_type
30 {
31 };
32 template <typename T>
33 struct is_shared_ptr<std::shared_ptr<T>> : std::true_type
34 {
35 };
36 template <typename T>
37 static constexpr bool is_shared_ptr_v = is_shared_ptr<T>::value;
38 template <typename T>
39 struct is_qpointer : std::false_type
40 {
41 };
42 template <typename T>
43 struct is_qpointer<QPointer<T>> : std::true_type
44 {
45 };
46 template <typename T>
47 static constexpr bool is_qpointer_v = is_qpointer<T>::value;
48 
49 #if defined(SCORE_DEBUG_DELIMITERS)
50 #define SCORE_DEBUG_INSERT_DELIMITER \
51  do \
52  { \
53  insertDelimiter(); \
54  } while(0)
55 #define SCORE_DEBUG_INSERT_DELIMITER2(Vis) \
56  do \
57  { \
58  Vis.insertDelimiter(); \
59  } while(0)
60 #define SCORE_DEBUG_CHECK_DELIMITER \
61  do \
62  { \
63  checkDelimiter(); \
64  } while(0)
65 #define SCORE_DEBUG_CHECK_DELIMITER2(Vis) \
66  do \
67  { \
68  Vis.checkDelimiter(); \
69  } while(0)
70 #else
71 #define SCORE_DEBUG_INSERT_DELIMITER
72 #define SCORE_DEBUG_INSERT_DELIMITER2(Vis)
73 #define SCORE_DEBUG_CHECK_DELIMITER
74 #define SCORE_DEBUG_CHECK_DELIMITER2(Vis)
75 #endif
76 
85 template <typename T>
86 concept is_QDataStreamSerializable
87  = std::is_arithmetic<T>::value || std::is_same<T, QStringList>::value
88  || std::is_same<T, QVector2D>::value || std::is_same<T, QVector3D>::value
89  || std::is_same<T, QVector4D>::value || std::is_same<T, QPointF>::value
90  || std::is_same<T, QPoint>::value || std::is_same<T, std::string>::value;
91 
92 SCORE_LIB_BASE_EXPORT QDataStream&
93 operator<<(QDataStream& stream, const std::string& obj);
94 SCORE_LIB_BASE_EXPORT QDataStream& operator>>(QDataStream& stream, std::string& obj);
95 
96 class QIODevice;
97 
99 {
100  QDataStream& stream;
101 };
103 {
104  QDataStream& stream;
105 };
106 
107 #if(INTPTR_MAX == INT64_MAX) && !defined(__APPLE__) && !defined(_WIN32)
108 #define SCORE_INT64_IS_QINT64
109 #endif
110 
111 #if defined(SCORE_INT64_IS_QINT64)
112 inline QDataStream& operator<<(QDataStream& s, uint64_t val)
113 {
114  s << (quint64)val;
115  return s;
116 }
117 inline QDataStream& operator>>(QDataStream& s, uint64_t& val)
118 {
119  s >> (quint64&)val;
120  return s;
121 }
122 inline QDataStream& operator<<(QDataStream& s, int64_t val)
123 {
124  s << (qint64)val;
125  return s;
126 }
127 inline QDataStream& operator>>(QDataStream& s, int64_t& val)
128 {
129  s >> (qint64&)val;
130  return s;
131 }
132 #endif
133 
134 #if defined(__APPLE__) || defined(__EMSCRIPTEN__)
135 inline QDataStream& operator<<(QDataStream& s, std::size_t val)
136 {
137  s << (quint64)val;
138  return s;
139 }
140 inline QDataStream& operator>>(QDataStream& s, std::size_t& val)
141 {
142  s >> (quint64&)val;
143  return s;
144 }
145 #endif
146 
147 template <typename T>
148  requires(!std::is_enum_v<T>)
149 DataStreamInput& operator<<(DataStreamInput& s, const T& obj);
150 
151 template <typename T>
152  requires(!std::is_enum_v<T>)
153 DataStreamOutput& operator>>(DataStreamOutput& s, T& obj);
154 
155 #define DATASTREAM_QT_BUILTIN(T) \
156  OSSIA_INLINE \
157  DataStreamInput& operator<<(DataStreamInput& s, const T& obj) \
158  { \
159  s.stream << obj; \
160  return s; \
161  } \
162  OSSIA_INLINE \
163  DataStreamOutput& operator>>(DataStreamOutput& s, T& obj) \
164  { \
165  s.stream >> obj; \
166  return s; \
167  }
168 
169 DATASTREAM_QT_BUILTIN(bool)
170 DATASTREAM_QT_BUILTIN(char)
171 DATASTREAM_QT_BUILTIN(qint8)
172 DATASTREAM_QT_BUILTIN(qint16)
173 DATASTREAM_QT_BUILTIN(qint32)
174 DATASTREAM_QT_BUILTIN(qint64)
175 
176 #if defined(SCORE_INT64_IS_QINT64)
177 DATASTREAM_QT_BUILTIN(int64_t)
178 #endif
179 
180 DATASTREAM_QT_BUILTIN(quint8)
181 DATASTREAM_QT_BUILTIN(quint16)
182 DATASTREAM_QT_BUILTIN(quint32)
183 DATASTREAM_QT_BUILTIN(quint64)
184 
185 #if defined(SCORE_INT64_IS_QINT64)
186 DATASTREAM_QT_BUILTIN(uint64_t)
187 #endif
188 
189 DATASTREAM_QT_BUILTIN(float)
190 DATASTREAM_QT_BUILTIN(double)
191 DATASTREAM_QT_BUILTIN(QString)
192 DATASTREAM_QT_BUILTIN(QStringList)
193 DATASTREAM_QT_BUILTIN(QPoint)
194 DATASTREAM_QT_BUILTIN(QPointF)
195 DATASTREAM_QT_BUILTIN(QRect)
196 DATASTREAM_QT_BUILTIN(QRectF)
197 DATASTREAM_QT_BUILTIN(QVector2D)
198 DATASTREAM_QT_BUILTIN(QVector3D)
199 DATASTREAM_QT_BUILTIN(QVector4D)
200 DATASTREAM_QT_BUILTIN(QByteArray)
201 DATASTREAM_QT_BUILTIN(QSizeF)
202 DATASTREAM_QT_BUILTIN(QColor)
203 DATASTREAM_QT_BUILTIN(QVariant)
204 DATASTREAM_QT_BUILTIN(std::string)
205 
206 template <typename T>
207 DataStreamInput& operator<<(DataStreamInput& s, const QList<T>& obj) = delete;
208 template <typename T>
209 DataStreamOutput& operator>>(DataStreamOutput& s, const QList<T>& obj) = delete;
210 
211 template <typename K, typename V>
212 DataStreamInput& operator<<(DataStreamInput& s, const QMap<K, V>& obj) = delete;
213 template <typename K, typename V>
214 DataStreamOutput& operator>>(DataStreamOutput& s, const QMap<K, V>& obj) = delete;
215 
216 template <typename K, typename V>
217 DataStreamInput& operator<<(DataStreamInput& s, const QHash<K, V>& obj) = delete;
218 template <typename K, typename V>
219 DataStreamOutput& operator>>(DataStreamOutput& s, const QHash<K, V>& obj) = delete;
220 
221 template <typename T>
222  requires(std::is_enum_v<T>)
223 OSSIA_INLINE DataStreamInput& operator<<(DataStreamInput& s, const T& obj)
224 {
225  s.stream << obj;
226  return s;
227 }
228 
229 template <typename T>
230  requires(std::is_enum_v<T>)
231 OSSIA_INLINE DataStreamOutput& operator>>(DataStreamOutput& s, T& obj)
232 {
233  s.stream >> obj;
234  return s;
235 }
Definition: ClipMode.hpp:10
Definition: DataStreamHelpers.hpp:99
Definition: DataStreamHelpers.hpp:103
Definition: DataStreamHelpers.hpp:30