/thirdparty/qxt/qxtweb-standalone/qxtweb/qxtmetatype.h

http://github.com/tomahawk-player/tomahawk · C Header · 132 lines · 87 code · 20 blank · 25 comment · 0 complexity · 8eb145bb50412611f6bee070ebe5222f MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) Qxt Foundation. Some rights reserved.
  4. **
  5. ** This file is part of the QxtCore module of the Qxt library.
  6. **
  7. ** This library is free software; you can redistribute it and/or modify it
  8. ** under the terms of the Common Public License, version 1.0, as published
  9. ** by IBM, and/or under the terms of the GNU Lesser General Public License,
  10. ** version 2.1, as published by the Free Software Foundation.
  11. **
  12. ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
  13. ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
  14. ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
  15. ** FITNESS FOR A PARTICULAR PURPOSE.
  16. **
  17. ** You should have received a copy of the CPL and the LGPL along with this
  18. ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
  19. ** included with the source distribution for more information.
  20. ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
  21. **
  22. ** <http://libqxt.org> <foundation@libqxt.org>
  23. **
  24. ****************************************************************************/
  25. #ifndef QXTMETATYPE_H
  26. #define QXTMETATYPE_H
  27. #include <QMetaType>
  28. #include <QDataStream>
  29. #include <QGenericArgument>
  30. #include <QtDebug>
  31. #include <qxtglobal.h>
  32. template <typename T>
  33. class /*QXT_CORE_EXPORT*/ QxtMetaType
  34. {
  35. public:
  36. static inline T* construct(const T* copy = 0)
  37. {
  38. return QMetaType::construct(qMetaTypeId<T>(), reinterpret_cast<const void*>(copy));
  39. }
  40. static inline void destroy(T* data)
  41. {
  42. QMetaType::destroy(qMetaTypeId<T>(), data);
  43. }
  44. // no need to reimplement isRegistered since this class will fail at compile time if it isn't
  45. static inline bool load(QDataStream& stream, T* data)
  46. {
  47. return QMetaType::load(stream, qMetaTypeId<T>(), reinterpret_cast<void*>(data));
  48. }
  49. static inline bool save(QDataStream& stream, const T* data)
  50. {
  51. return QMetaType::save(stream, qMetaTypeId<T>(), reinterpret_cast<const void*>(data));
  52. }
  53. static inline int type()
  54. {
  55. return qMetaTypeId<T>();
  56. }
  57. static inline const char* name()
  58. {
  59. return QMetaType::typeName(qMetaTypeId<T>());
  60. }
  61. };
  62. template <>
  63. class /*QXT_CORE_EXPORT*/ QxtMetaType<void>
  64. {
  65. public:
  66. static inline void* construct(const void* copy = 0)
  67. {
  68. Q_UNUSED(copy);
  69. return 0;
  70. }
  71. static inline void destroy(void* data)
  72. {
  73. Q_UNUSED(data);
  74. }
  75. static inline bool load(QDataStream& stream, void* data)
  76. {
  77. Q_UNUSED(stream);
  78. Q_UNUSED(data);
  79. return false;
  80. }
  81. static inline bool save(QDataStream& stream, const void* data)
  82. {
  83. Q_UNUSED(stream);
  84. Q_UNUSED(data);
  85. return false;
  86. }
  87. static inline int type()
  88. {
  89. return 0;
  90. }
  91. static inline const char* name()
  92. {
  93. return 0;
  94. }
  95. };
  96. inline void* qxtConstructByName(const char* typeName, const void* copy = 0)
  97. {
  98. return QMetaType::construct(QMetaType::type(typeName), copy);
  99. }
  100. inline void qxtDestroyByName(const char* typeName, void* data)
  101. {
  102. QMetaType::destroy(QMetaType::type(typeName), data);
  103. }
  104. inline void* qxtConstructFromGenericArgument(QGenericArgument arg)
  105. {
  106. return qxtConstructByName(arg.name(), arg.data());
  107. }
  108. inline void qxtDestroyFromGenericArgument(QGenericArgument arg)
  109. {
  110. qxtDestroyByName(arg.name(), arg.data());
  111. }
  112. #endif