qtprotobuf.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "qtprotobuftypes.h"
  26. #include "qprotobufobject.h"
  27. #include <type_traits>
  28. #define registerProtobufType(X) qRegisterMetaType<X>(# X);\
  29. qRegisterMetaType<X>("QtProtobuf::"# X)
  30. namespace QtProtobuf {
  31. namespace {
  32. template<typename T, typename std::enable_if_t<sizeof(T) == sizeof(int32_t)
  33. && std::is_signed<T>::value, int> = 0>
  34. void registerBasicConverters() {
  35. QMetaType::registerConverter<int32_t, T>(T::fromType);
  36. QMetaType::registerConverter<T, int32_t>(T::toType);
  37. QMetaType::registerConverter<double, T>(T::fromType);
  38. QMetaType::registerConverter<T, double>(T::toType);
  39. QMetaType::registerConverter<T, QString>(T::toString);
  40. }
  41. template<typename T, typename std::enable_if_t<sizeof(T) == sizeof(int64_t)
  42. && std::is_signed<T>::value, int> = 0>
  43. void registerBasicConverters() {
  44. QMetaType::registerConverter<int64_t, T>(T::fromType);
  45. QMetaType::registerConverter<T, int64_t>(T::toType);
  46. QMetaType::registerConverter<qint64, T>(T::fromType);
  47. QMetaType::registerConverter<T, qint64>(T::toType);
  48. QMetaType::registerConverter<int32_t, T>(T::fromType);
  49. QMetaType::registerConverter<T, int32_t>(T::toType); //Required for enum handling
  50. QMetaType::registerConverter<T, QString>(T::toString);
  51. }
  52. template<typename T, typename std::enable_if_t<sizeof(T) == sizeof(int32_t)
  53. && !std::is_signed<T>::value, int> = 0>
  54. void registerBasicConverters() {
  55. QMetaType::registerConverter<uint32_t, T>(T::fromType);
  56. QMetaType::registerConverter<T, uint32_t>(T::toType);
  57. QMetaType::registerConverter<int32_t, T>(T::fromType);
  58. QMetaType::registerConverter<T, int32_t>(T::toType);
  59. QMetaType::registerConverter<double, T>(T::fromType);
  60. QMetaType::registerConverter<T, double>(T::toType);
  61. QMetaType::registerConverter<T, QString>(T::toString);
  62. }
  63. template<typename T, typename std::enable_if_t<sizeof(T) == sizeof(int64_t)
  64. && !std::is_signed<T>::value, int> = 0>
  65. void registerBasicConverters() {
  66. QMetaType::registerConverter<uint64_t, T>(T::fromType);
  67. QMetaType::registerConverter<T, uint64_t>(T::toType);
  68. QMetaType::registerConverter<quint64, T>(T::fromType);
  69. QMetaType::registerConverter<T, quint64>(T::toType);
  70. QMetaType::registerConverter<uint32_t, T>(T::fromType);
  71. QMetaType::registerConverter<T, QString>(T::toString);
  72. }
  73. }
  74. std::list<RegisterFunction>& registerFunctions() {
  75. static std::list<std::function<void(void)>> registrationList;
  76. return registrationList;
  77. }
  78. void qRegisterProtobufTypes() {
  79. static bool registred = false;
  80. if (registred) {
  81. return;
  82. }
  83. registred = true;
  84. registerProtobufType(int32);
  85. registerProtobufType(int64);
  86. registerProtobufType(uint32);
  87. registerProtobufType(uint64);
  88. registerProtobufType(sint32);
  89. registerProtobufType(sint64);
  90. registerProtobufType(fixed32);
  91. registerProtobufType(fixed64);
  92. registerProtobufType(sfixed32);
  93. registerProtobufType(sfixed64);
  94. registerProtobufType(int32List);
  95. registerProtobufType(int64List);
  96. registerProtobufType(uint32List);
  97. registerProtobufType(uint64List);
  98. registerProtobufType(sint32List);
  99. registerProtobufType(sint64List);
  100. registerProtobufType(fixed32List);
  101. registerProtobufType(fixed64List);
  102. registerProtobufType(sfixed32List);
  103. registerProtobufType(sfixed64List);
  104. registerProtobufType(DoubleList);
  105. registerProtobufType(FloatList);
  106. registerBasicConverters<int32>();
  107. registerBasicConverters<int64>();
  108. registerBasicConverters<sfixed32>();
  109. registerBasicConverters<sfixed64>();
  110. registerBasicConverters<fixed32>();
  111. registerBasicConverters<fixed64>();
  112. for (auto registerFunc : registerFunctions()) {
  113. registerFunc();
  114. }
  115. }
  116. }