Loading...
Searching...
No Matches
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
28template <typename T>
29struct is_shared_ptr : std::false_type
30{
31};
32template <typename T>
33struct is_shared_ptr<std::shared_ptr<T>> : std::true_type
34{
35};
36template <typename T>
37static constexpr bool is_shared_ptr_v = is_shared_ptr<T>::value;
38template <typename T>
39struct is_qpointer : std::false_type
40{
41};
42template <typename T>
43struct is_qpointer<QPointer<T>> : std::true_type
44{
45};
46template <typename T>
47static 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
85template <typename T>
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
92SCORE_LIB_BASE_EXPORT QDataStream&
93operator<<(QDataStream& stream, const std::string& obj);
94SCORE_LIB_BASE_EXPORT QDataStream& operator>>(QDataStream& stream, std::string& obj);
95
96class 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)
112inline QDataStream& operator<<(QDataStream& s, uint64_t val)
113{
114 s << (quint64)val;
115 return s;
116}
117inline QDataStream& operator>>(QDataStream& s, uint64_t& val)
118{
119 s >> (quint64&)val;
120 return s;
121}
122inline QDataStream& operator<<(QDataStream& s, int64_t val)
123{
124 s << (qint64)val;
125 return s;
126}
127inline 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__)
135inline QDataStream& operator<<(QDataStream& s, std::size_t val)
136{
137 s << (quint64)val;
138 return s;
139}
140inline QDataStream& operator>>(QDataStream& s, std::size_t& val)
141{
142 s >> (quint64&)val;
143 return s;
144}
145#endif
146
147template <typename T>
148 requires(!std::is_enum_v<T>)
149DataStreamInput& operator<<(DataStreamInput& s, const T& obj);
150
151template <typename T>
152 requires(!std::is_enum_v<T>)
153DataStreamOutput& 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
169DATASTREAM_QT_BUILTIN(bool)
170DATASTREAM_QT_BUILTIN(char)
171DATASTREAM_QT_BUILTIN(qint8)
172DATASTREAM_QT_BUILTIN(qint16)
173DATASTREAM_QT_BUILTIN(qint32)
174DATASTREAM_QT_BUILTIN(qint64)
175
176#if defined(SCORE_INT64_IS_QINT64)
177DATASTREAM_QT_BUILTIN(int64_t)
178#endif
179
180DATASTREAM_QT_BUILTIN(quint8)
181DATASTREAM_QT_BUILTIN(quint16)
182DATASTREAM_QT_BUILTIN(quint32)
183DATASTREAM_QT_BUILTIN(quint64)
184
185#if defined(SCORE_INT64_IS_QINT64)
186DATASTREAM_QT_BUILTIN(uint64_t)
187#endif
188
189DATASTREAM_QT_BUILTIN(float)
190DATASTREAM_QT_BUILTIN(double)
191DATASTREAM_QT_BUILTIN(QString)
192DATASTREAM_QT_BUILTIN(QStringList)
193DATASTREAM_QT_BUILTIN(QPoint)
194DATASTREAM_QT_BUILTIN(QPointF)
195DATASTREAM_QT_BUILTIN(QRect)
196DATASTREAM_QT_BUILTIN(QRectF)
197DATASTREAM_QT_BUILTIN(QVector2D)
198DATASTREAM_QT_BUILTIN(QVector3D)
199DATASTREAM_QT_BUILTIN(QVector4D)
200DATASTREAM_QT_BUILTIN(QByteArray)
201DATASTREAM_QT_BUILTIN(QSizeF)
202DATASTREAM_QT_BUILTIN(QColor)
203DATASTREAM_QT_BUILTIN(QVariant)
204DATASTREAM_QT_BUILTIN(std::string)
205
206template <typename T>
207DataStreamInput& operator<<(DataStreamInput& s, const QList<T>& obj) = delete;
208template <typename T>
209DataStreamOutput& operator>>(DataStreamOutput& s, const QList<T>& obj) = delete;
210
211template <typename K, typename V>
212DataStreamInput& operator<<(DataStreamInput& s, const QMap<K, V>& obj) = delete;
213template <typename K, typename V>
214DataStreamOutput& operator>>(DataStreamOutput& s, const QMap<K, V>& obj) = delete;
215
216template <typename K, typename V>
217DataStreamInput& operator<<(DataStreamInput& s, const QHash<K, V>& obj) = delete;
218template <typename K, typename V>
219DataStreamOutput& operator>>(DataStreamOutput& s, const QHash<K, V>& obj) = delete;
220
221template <typename T>
222 requires(std::is_enum_v<T>)
223OSSIA_INLINE DataStreamInput& operator<<(DataStreamInput& s, const T& obj)
224{
225 s.stream << obj;
226 return s;
227}
228
229template <typename T>
230 requires(std::is_enum_v<T>)
231OSSIA_INLINE DataStreamOutput& operator>>(DataStreamOutput& s, T& obj)
232{
233 s.stream >> obj;
234 return s;
235}
Definition ClipMode.hpp:10
Definition DataStreamHelpers.hpp:87
STL namespace.
Definition DataStreamHelpers.hpp:99
Definition DataStreamHelpers.hpp:103
Definition DataStreamHelpers.hpp:30