QxSerializeInvoker.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  32. #ifndef _QX_SERIALIZE_INVOKER_H_
  33. #define _QX_SERIALIZE_INVOKER_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. #ifdef _MSC_VER
  38. #pragma warning(push)
  39. #pragma warning(disable:4996)
  40. #pragma warning(disable:4094)
  41. #endif // _MSC_VER
  42. #include <boost/serialization/serialization.hpp>
  43. #include <boost/serialization/base_object.hpp>
  44. #include <boost/serialization/nvp.hpp>
  45. #include <QxTraits/get_base_class.h>
  46. #include <QxTraits/is_qx_registered.h>
  47. #include <QxRegister/QxClass.h>
  48. namespace qx {
  49. namespace serialization {
  50. namespace detail {
  51. template <class Base>
  52. struct base_class
  53. {
  54. template <class Archive, class T>
  55. static inline void save(Archive & ar, const T & t, const unsigned int file_version)
  56. {
  57. Q_UNUSED(file_version);
  58. static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
  59. const char * sTag = QxClass<Base>::getSingleton()->getNamePtr();
  60. ar << boost::serialization::make_nvp(sTag, boost::serialization::base_object<const Base>(t));
  61. }
  62. template <class Archive, class T>
  63. static inline void load(Archive & ar, T & t, const unsigned int file_version)
  64. {
  65. Q_UNUSED(file_version);
  66. static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
  67. const char * sTag = QxClass<Base>::getSingleton()->getNamePtr();
  68. ar >> boost::serialization::make_nvp(sTag, boost::serialization::base_object<Base>(t));
  69. }
  70. };
  71. template <>
  72. struct base_class<qx::trait::no_base_class_defined>
  73. {
  74. template <class Archive, class T>
  75. static inline void save(Archive & ar, const T & t, const unsigned int file_version)
  76. { Q_UNUSED(ar); Q_UNUSED(t); Q_UNUSED(file_version); }
  77. template <class Archive, class T>
  78. static inline void load(Archive & ar, T & t, const unsigned int file_version)
  79. { Q_UNUSED(ar); Q_UNUSED(t); Q_UNUSED(file_version); }
  80. };
  81. template <class Archive, class T>
  82. void save(Archive & ar, const T & t, const unsigned int file_version)
  83. {
  84. typedef typename qx::trait::get_base_class<T>::type qx_type_base_class_tmp;
  85. qx::serialization::detail::base_class<qx_type_base_class_tmp>::save(ar, t, file_version);
  86. QxClass<T>::getSingleton()->dataMemberX()->toArchive(& t, ar, file_version);
  87. }
  88. template <class Archive, class T>
  89. void load(Archive & ar, T & t, const unsigned int file_version)
  90. {
  91. typedef typename qx::trait::get_base_class<T>::type qx_type_base_class_tmp;
  92. qx::serialization::detail::base_class<qx_type_base_class_tmp>::load(ar, t, file_version);
  93. QxClass<T>::getSingleton()->dataMemberX()->fromArchive(& t, ar, file_version);
  94. }
  95. template <class Archive, class T>
  96. struct saver
  97. {
  98. static inline void invoke(Archive & ar, const T & t, const unsigned int file_version)
  99. { qx::serialization::detail::save(ar, t, file_version); }
  100. };
  101. template <class Archive, class T>
  102. struct loader
  103. {
  104. static inline void invoke(Archive & ar, T & t, const unsigned int file_version)
  105. { qx::serialization::detail::load(ar, t, file_version); }
  106. };
  107. } // namespace detail
  108. } // namespace serialization
  109. } // namespace qx
  110. #include "../../inl/QxSerialize/QxSerializeInvoker.inl"
  111. #ifdef _MSC_VER
  112. #pragma warning(pop)
  113. #endif // _MSC_VER
  114. #endif // _QX_SERIALIZE_INVOKER_H_
  115. #endif // _QX_ENABLE_BOOST_SERIALIZATION