QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qtprotobuftypes.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once //QtProtobufTypes
27 
28 #include "qtprotobufglobal.h"
29 
30 #include <QList>
31 #include <QMetaType>
32 
33 #include <unordered_map>
34 #include <functional>
35 #include <list>
36 #include <type_traits>
37 
38 namespace QtProtobuf {
39 
47 enum WireTypes {
49  Varint = 0,
50  Fixed64 = 1,
52  StartGroup = 3,
53  EndGroup = 4,
54  Fixed32 = 5
55 };
56 
58 struct PropertyOrderingInfo {
59  PropertyOrderingInfo(int _qtProperty, const QString &_jsonName) : qtProperty(_qtProperty)
60  , jsonName(_jsonName) {}
61 
62  int qtProperty;
63  QString jsonName;
64  template<typename T,
65  typename std::enable_if_t<std::is_integral<T>::value, int> = 0>
66  operator T() const { return qtProperty; }
67  operator QString() const { return jsonName; }
68 
69  template<typename T,
70  typename std::enable_if_t<std::is_integral<T>::value, int> = 0>
71  bool operator==(const T _qtProperty) const { return _qtProperty == qtProperty; }
72  bool operator==(const QString &_jsonName) const { return _jsonName == jsonName; }
73 };
74 
77 using QProtobufPropertyOrdering = std::unordered_map<int, PropertyOrderingInfo>;
78 
84 template<typename T, int = 0>
85 struct transparent {
86  transparent(T t = T()) : _t(t) {}
87  T _t;
88  operator T &() { return _t; }
89  operator T() const { return _t; }
90  transparent &operator =(const T &t) { _t = t; return *this; }
91 
92  static T toType(transparent t) { return t._t; }
93  static transparent fromType(T _t) { return transparent(_t); }
94 
95  static QString toString(transparent t) { return QString::number(t._t); }
96 };
97 
104 using int32 = transparent<int32_t>;
105 
112 using int64 = transparent<int64_t>;
113 
120 using uint32 = uint32_t;
121 
128 using uint64 = uint64_t;
129 
138 using sint32 = int32_t;
139 
148 using sint64 = int64_t;
149 
156 using fixed32 = transparent<uint32_t, 1>;
157 
164 using fixed64 = transparent<uint64_t, 1>;
165 
172 using sfixed32 = transparent<int32_t, 1>;
173 
180 using sfixed64 = transparent<int64_t, 1>;
181 
186 using int32List = QList<int32>;
187 
192 using int64List = QList<int64>;
193 
198 using uint32List = QList<uint32>;
199 
204 using uint64List = QList<uint64>;
205 
210 using sint32List = QList<sint32>;
211 
216 using sint64List = QList<sint64>;
217 
222 using fixed32List = QList<fixed32>;
223 
228 using fixed64List = QList<fixed64>;
229 
234 using sfixed32List = QList<sfixed32>;
235 
240 using sfixed64List = QList<sfixed64>;
241 
246 using FloatList = QList<float>;
247 
252 using DoubleList = QList<double>;
253 
259 Q_PROTOBUF_EXPORT void qRegisterProtobufTypes();
260 
264 using RegisterFunction = std::function<void(void)>;
265 
267 Q_PROTOBUF_EXPORT std::list<RegisterFunction>& registerFunctions();
268 
270 template <typename T>
271 struct ProtoTypeRegistrar {
272  ProtoTypeRegistrar(RegisterFunction initializer) {
273  registerFunctions().push_back(initializer);
274  }
275 };
276 
277 }
278 
279 Q_DECLARE_METATYPE(QtProtobuf::int32)
280 Q_DECLARE_METATYPE(QtProtobuf::int64)
281 Q_DECLARE_METATYPE(QtProtobuf::sint32)
282 Q_DECLARE_METATYPE(QtProtobuf::sint64)
283 Q_DECLARE_METATYPE(QtProtobuf::uint32)
284 Q_DECLARE_METATYPE(QtProtobuf::uint64)
285 Q_DECLARE_METATYPE(QtProtobuf::fixed32)
286 Q_DECLARE_METATYPE(QtProtobuf::fixed64)
287 Q_DECLARE_METATYPE(QtProtobuf::sfixed32)
288 Q_DECLARE_METATYPE(QtProtobuf::sfixed64)
289 
290 Q_DECLARE_METATYPE(QtProtobuf::int32List)
291 Q_DECLARE_METATYPE(QtProtobuf::int64List)
292 Q_DECLARE_METATYPE(QtProtobuf::sint32List)
293 Q_DECLARE_METATYPE(QtProtobuf::sint64List)
294 Q_DECLARE_METATYPE(QtProtobuf::uint32List)
295 Q_DECLARE_METATYPE(QtProtobuf::uint64List)
296 Q_DECLARE_METATYPE(QtProtobuf::fixed32List)
297 Q_DECLARE_METATYPE(QtProtobuf::fixed64List)
298 Q_DECLARE_METATYPE(QtProtobuf::sfixed32List)
299 Q_DECLARE_METATYPE(QtProtobuf::sfixed64List)
300 
301 Q_DECLARE_METATYPE(QtProtobuf::FloatList)
302 Q_DECLARE_METATYPE(QtProtobuf::DoubleList)
303 
304 namespace std {
306 template<>
307 struct make_unsigned<QtProtobuf::int32> { typedef typename make_unsigned<decltype(QtProtobuf::int32::_t)>::type type; };
309 template<>
310 struct make_unsigned<QtProtobuf::int64> { typedef typename make_unsigned<decltype(QtProtobuf::int64::_t)>::type type; };
312 template<>
313 struct make_unsigned<QtProtobuf::fixed32> { typedef typename make_unsigned<decltype(QtProtobuf::fixed32::_t)>::type type; };
315 template<>
316 struct make_unsigned<QtProtobuf::fixed64> { typedef typename make_unsigned<decltype(QtProtobuf::fixed64::_t)>::type type; };
318 template<>
319 struct make_unsigned<QtProtobuf::sfixed32> { typedef typename make_unsigned<decltype(QtProtobuf::sfixed32::_t)>::type type; };
321 template<>
322 struct make_unsigned<QtProtobuf::sfixed64> { typedef typename make_unsigned<decltype(QtProtobuf::sfixed64::_t)>::type type; };
323 
325 template<>
326 struct make_signed<QtProtobuf::int32> { typedef typename make_signed<decltype(QtProtobuf::int32::_t)>::type type; };
328 template<>
329 struct make_signed<QtProtobuf::int64> { typedef typename make_signed<decltype(QtProtobuf::int64::_t)>::type type; };
331 template<>
332 struct make_signed<QtProtobuf::fixed32> { typedef typename make_signed<decltype(QtProtobuf::fixed32::_t)>::type type; };
334 template<>
335 struct make_signed<QtProtobuf::fixed64> { typedef typename make_signed<decltype(QtProtobuf::fixed64::_t)>::type type; };
337 template<>
338 struct make_signed<QtProtobuf::sfixed32> { typedef typename make_signed<decltype(QtProtobuf::sfixed32::_t)>::type type; };
340 template<>
341 struct make_signed<QtProtobuf::sfixed64> { typedef typename make_signed<decltype(QtProtobuf::sfixed64::_t)>::type type; };
342 
344 template<>
345 struct is_signed<QtProtobuf::int32> : public is_signed<decltype(QtProtobuf::int32::_t)> {};
347 template<>
348 struct is_signed<QtProtobuf::int64> : public is_signed<decltype(QtProtobuf::int64::_t)> {};
350 template<>
351 struct is_signed<QtProtobuf::fixed32> : public is_signed<decltype(QtProtobuf::fixed32::_t)> {};
353 template<>
354 struct is_signed<QtProtobuf::fixed64> : public is_signed<decltype(QtProtobuf::fixed64::_t)> {};
356 template<>
357 struct is_signed<QtProtobuf::sfixed32> : public is_signed<decltype(QtProtobuf::sfixed32::_t)> {};
359 template<>
360 struct is_signed<QtProtobuf::sfixed64> : public is_signed<decltype(QtProtobuf::sfixed64::_t)> {};
361 
362 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
363 template<> struct hash<QString> {
364  std::size_t operator()(const QString &s) const {
365  return std::hash<std::string>()(s.toStdString());
366  }
367 };
368 #endif
369 }
QList< double > DoubleList
alias for list of QtProtobuf::double
Definition: qtprotobuftypes.h:252
QList< sint64 > sint64List
alias for list of QtProtobuf::sint64
Definition: qtprotobuftypes.h:216
transparent< uint32_t, 1 > fixed32
fixed32 unsigned 32-bit fixed size integer
Definition: qtprotobuftypes.h:156
void qRegisterProtobufTypes()
qRegisterProtobufTypes This method should be called in all applications that supposed to use QtProtob...
Definition: qtprotobuf.cpp:62
uint64_t uint64
uint64 unsigned 64-bit integer
Definition: qtprotobuftypes.h:128
WireTypes
The WireTypes enumeration reflects protobuf default wiretypes.
Definition: qtprotobuftypes.h:47
transparent< int32_t > int32
int32 signed 32-bit integer
Definition: qtprotobuftypes.h:104
transparent< uint64_t, 1 > fixed64
fixed64 unsigned 64-bit fixed size integer
Definition: qtprotobuftypes.h:164
QList< sfixed32 > sfixed32List
alias for list of QtProtobuf::sfixed32
Definition: qtprotobuftypes.h:234
transparent< int64_t, 1 > sfixed64
fixed64 signed 64-bit fixed size integer
Definition: qtprotobuftypes.h:180
QList< fixed32 > fixed32List
alias for list of QtProtobuf::fixed32
Definition: qtprotobuftypes.h:222
QList< float > FloatList
alias for list of QtProtobuf::float
Definition: qtprotobuftypes.h:246
QList< sint32 > sint32List
alias for list of QtProtobuf::sint32
Definition: qtprotobuftypes.h:210
int64_t sint64
sint64 signed 64-bit ZigZag integer
Definition: qtprotobuftypes.h:148
QList< int64 > int64List
alias for list of QtProtobuf::int64
Definition: qtprotobuftypes.h:192
transparent< int32_t, 1 > sfixed32
fixed32 signed 32-bit fixed size integer
Definition: qtprotobuftypes.h:172
QList< fixed64 > fixed64List
alias for list of QtProtobuf::fixed64
Definition: qtprotobuftypes.h:228
QList< int32 > int32List
alias for list of QtProtobuf::int32
Definition: qtprotobuftypes.h:186
QList< uint64 > uint64List
alias for list of QtProtobuf::uint64
Definition: qtprotobuftypes.h:204
QList< sfixed64 > sfixed64List
alias for list of QtProtobuf::sfixed64
Definition: qtprotobuftypes.h:240
QList< uint32 > uint32List
alias for list of QtProtobuf::uint32
Definition: qtprotobuftypes.h:198
int32_t sint32
sint32 signed 32-bit ZigZag integer
Definition: qtprotobuftypes.h:138
uint32_t uint32
uint32 unsigned 32-bit integer
Definition: qtprotobuftypes.h:120
@ StartGroup
groups
Definition: qtprotobuftypes.h:52
@ UnknownWireType
Invalid wire type.
Definition: qtprotobuftypes.h:48
@ Varint
int32, int64, uint32, uint64, sint32, sint64, bool, enum
Definition: qtprotobuftypes.h:49
@ Fixed32
fixed32, sfixed32, float
Definition: qtprotobuftypes.h:54
@ Fixed64
fixed64, sfixed64, double
Definition: qtprotobuftypes.h:50
@ LengthDelimited
string, bytes, embedded messages, packed repeated fields
Definition: qtprotobuftypes.h:51
@ EndGroup
groups
Definition: qtprotobuftypes.h:53