QxClone.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef _QX_CLONE_H_
  32. #define _QX_CLONE_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxClone.h
  38. * \author Lionel Marty
  39. * \ingroup QxSerialize
  40. * \brief Clone all classes registered into QxOrm context using QxOrm library serialization engine
  41. */
  42. #include <string>
  43. #include <iostream>
  44. #include <sstream>
  45. #include <exception>
  46. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  47. #include <boost/archive/archive_exception.hpp>
  48. #include <QxSerialize/boost/QxSerializeInclude.h>
  49. #include <QxSerialize/QxBoostSerializeHelper/QxBoostSerializeRegisterHelperX.h>
  50. #include <QxSerialize/QxSerializeInvoker.h>
  51. #else // _QX_ENABLE_BOOST_SERIALIZATION
  52. #include <QxSerialize/QxSerializeQDataStream.h>
  53. #include <QxSerialize/QDataStream/QxSerializeQDataStream_all_include.h>
  54. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  55. #define QX_STR_CLONE_SERIALIZATION_ERROR "[QxOrm] qx::clone() serialization error : '%s'"
  56. #define QX_STR_CLONE_DESERIALIZATION_ERROR "[QxOrm] qx::clone() deserialization error : '%s'"
  57. namespace qx {
  58. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  59. /*!
  60. * \ingroup QxSerialize
  61. * \brief qx::clone_to_nude_ptr(const T & obj) : return a nude pointer (be careful with memory leak) of a new instance of type T cloned from obj
  62. */
  63. template <class T>
  64. T * clone_to_nude_ptr(const T & obj)
  65. {
  66. QX_CLONE_STRING_STREAM ioss(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
  67. QX_CLONE_BINARY_OUTPUT_ARCHIVE oar(ioss, boost::archive::no_header);
  68. QxBoostSerializeRegisterHelperX::helper(oar);
  69. bool bSerializeOk = false;
  70. try { oar << obj; bSerializeOk = ioss.good(); }
  71. catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
  72. catch (const std::exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
  73. catch (...) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, "unknown error"); }
  74. if (! bSerializeOk) { qAssert(false); return NULL; }
  75. T * pClone = new T();
  76. QX_CLONE_BINARY_INPUT_ARCHIVE iar(ioss, boost::archive::no_header);
  77. QxBoostSerializeRegisterHelperX::helper(iar);
  78. bool bDeserializeOk = false;
  79. try { iar >> (* pClone); bDeserializeOk = ioss.good(); }
  80. catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
  81. catch (const std::exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
  82. catch (...) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, "unknown error"); }
  83. qAssert(bDeserializeOk);
  84. return (bDeserializeOk ? pClone : NULL);
  85. }
  86. #else // _QX_ENABLE_BOOST_SERIALIZATION
  87. /*!
  88. * \ingroup QxSerialize
  89. * \brief qx::clone_to_nude_ptr(const T & obj) : return a nude pointer (be careful with memory leak) of a new instance of type T cloned from obj (this is a limited clone version which uses Qt QDataStream engine compared to boost::serialization engine)
  90. */
  91. template <class T>
  92. T * clone_to_nude_ptr(const T & obj)
  93. {
  94. QByteArray baClone = qx::serialization::qt::to_byte_array(obj);
  95. if (baClone.isEmpty()) { qAssertMsg(false, "[QxOrm] qx::clone_to_nude_ptr", "an error occurred during QDataStream serialization process"); return NULL; }
  96. T * pClone = new T();
  97. qx_bool bOk = qx::serialization::qt::from_byte_array((* pClone), baClone);
  98. return (bOk ? pClone : NULL);
  99. }
  100. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  101. /*!
  102. * \ingroup QxSerialize
  103. * \brief qx::clone(const T & obj) : return a boost smart-pointer (std::shared_ptr<T>) of a new instance of type T cloned from obj
  104. */
  105. template <class T>
  106. std::shared_ptr<T> clone(const T & obj)
  107. { T * ptr = qx::clone_to_nude_ptr<T>(obj); return std::shared_ptr<T>(ptr); }
  108. #ifdef _QX_ENABLE_BOOST
  109. /*!
  110. * \ingroup QxSerialize
  111. * \brief qx::clone_to_boost_shared_ptr(const T & obj) : return a boost smart-pointer (boost::shared_ptr<T>) of a new instance of type T cloned from obj
  112. */
  113. template <class T>
  114. boost::shared_ptr<T> clone_to_boost_shared_ptr(const T & obj)
  115. { T * ptr = qx::clone_to_nude_ptr<T>(obj); return boost::shared_ptr<T>(ptr); }
  116. #endif // _QX_ENABLE_BOOST
  117. /*!
  118. * \ingroup QxSerialize
  119. * \brief qx::clone_to_qt_shared_ptr(const T & obj) : return a Qt smart-pointer (QSharedPointer<T>) of a new instance of type T cloned from obj
  120. */
  121. template <class T>
  122. QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj)
  123. { T * ptr = qx::clone_to_nude_ptr<T>(obj); return QSharedPointer<T>(ptr); }
  124. /*!
  125. * \ingroup QxSerialize
  126. * \brief qx::clone_to_std_shared_ptr(const T & obj) : return a C++11 std smart-pointer (std::shared_ptr<T>) of a new instance of type T cloned from obj
  127. */
  128. template <class T>
  129. std::shared_ptr<T> clone_to_std_shared_ptr(const T & obj)
  130. { T * ptr = qx::clone_to_nude_ptr<T>(obj); return std::shared_ptr<T>(ptr); }
  131. } // namespace qx
  132. #endif // _QX_CLONE_H_