QxSerializeQDataStream.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_SERIALIZE_QDATASTREAM_H_
  32. #define _QX_SERIALIZE_QDATASTREAM_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxSerializeQDataStream.h
  38. * \author Lionel Marty
  39. * \ingroup QxSerialize
  40. * \brief Used when _QX_ENABLE_BOOST_SERIALIZATION compilation option is not defined to provide serialization engine with Qt QDataStream class
  41. */
  42. #include <exception>
  43. #include <QtCore/qdatastream.h>
  44. #include <QtCore/qfile.h>
  45. #include <QxCommon/QxBool.h>
  46. namespace qx {
  47. namespace serialization {
  48. /*!
  49. * \ingroup QxSerialize
  50. * \brief serialize a class registered into QxOrm context using Qt QDataStream serialization engine
  51. */
  52. namespace qt {
  53. template <class T>
  54. inline QByteArray to_byte_array(const T & obj, void * owner = NULL, unsigned int flags = 1 /* boost::archive::no_header */)
  55. {
  56. Q_UNUSED(flags); Q_UNUSED(owner);
  57. QByteArray ba; QString err;
  58. QDataStream stream((& ba), QIODevice::WriteOnly);
  59. stream << (quint32)(9438);
  60. try { stream << obj; }
  61. catch (const std::exception & e) { err = QString("serialization error '%ERR%'").replace("%ERR%", e.what()); }
  62. catch (...) { err = QString("serialization error '%ERR%'").replace("%ERR%", "unknown error"); }
  63. if (! err.isEmpty()) { qDebug("[QxOrm] qx::serialization::qt::to_byte_array() : %s", qPrintable(err)); ba.clear(); }
  64. return ba;
  65. }
  66. template <class T>
  67. inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */)
  68. {
  69. Q_UNUSED(flags);
  70. qx_bool result = false;
  71. if (data.isEmpty()) { return qx_bool(false, "input binary data is empty"); }
  72. QDataStream stream(data);
  73. quint32 magic = 0; stream >> magic;
  74. if (magic != 9438) { return qx_bool(false, "input binary data is not valid"); }
  75. try { stream >> obj; result = true; }
  76. catch (const std::exception & e) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", e.what())); }
  77. catch (...) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", "unknown error")); }
  78. if (! result.getDesc().isEmpty()) { QString msg = result.getDesc(); qDebug("[QxOrm] qx::serialization::qt::from_byte_array() : %s", qPrintable(msg)); }
  79. return result;
  80. }
  81. template <class T>
  82. inline QString to_string(const T & obj, unsigned int flags = 1 /* boost::archive::no_header */)
  83. { return qx::serialization::qt::to_byte_array(obj, NULL, flags).toBase64(); }
  84. template <class T>
  85. inline qx_bool from_string(T & obj, const QString & sString, unsigned int flags = 1 /* boost::archive::no_header */)
  86. { QByteArray data = QByteArray::fromBase64(sString.toLatin1()); return qx::serialization::qt::from_byte_array(obj, data, flags); }
  87. template <class T>
  88. inline qx_bool to_file(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */)
  89. {
  90. QByteArray data = qx::serialization::qt::to_byte_array(obj, NULL, flags);
  91. QFile file(sFileName);
  92. if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  93. { return qx_bool(false, "cannot open file : " + sFileName); }
  94. file.write(data);
  95. file.close();
  96. return qx_bool(true);
  97. }
  98. template <class T>
  99. inline qx_bool from_file(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */)
  100. {
  101. QFile file(sFileName);
  102. if (! file.open(QIODevice::ReadOnly))
  103. { return qx_bool(false, "cannot open file : " + sFileName); }
  104. QByteArray data = file.readAll(); file.close();
  105. return qx::serialization::qt::from_byte_array(obj, data, flags);
  106. }
  107. template <class T>
  108. inline qx_bool to_file_compressed(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, int iCompressionLevel = -1)
  109. {
  110. QByteArray data = qx::serialization::qt::to_byte_array(obj, NULL, flags);
  111. QByteArray compressed = qCompress(data, iCompressionLevel);
  112. QFile file(sFileName);
  113. if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  114. { return qx_bool(false, "cannot open file : " + sFileName); }
  115. file.write(compressed);
  116. file.close();
  117. return qx_bool(true);
  118. }
  119. template <class T>
  120. inline qx_bool from_file_compressed(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */)
  121. {
  122. QFile file(sFileName);
  123. if (! file.open(QIODevice::ReadOnly))
  124. { return qx_bool(false, "cannot open file : " + sFileName); }
  125. QByteArray data = file.readAll(); file.close();
  126. QByteArray uncompressed = qUncompress(data);
  127. return qx::serialization::qt::from_byte_array(obj, uncompressed, flags);
  128. }
  129. } // namespace qt
  130. } // namespace serialization
  131. } // namespace qx
  132. #endif // _QX_SERIALIZE_QDATASTREAM_H_