QxOrm
1.5.0
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** https://www.qxorm.com/ 00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) 00005 ** 00006 ** This file is part of the QxOrm library 00007 ** 00008 ** This software is provided 'as-is', without any express or implied 00009 ** warranty. In no event will the authors be held liable for any 00010 ** damages arising from the use of this software 00011 ** 00012 ** Commercial Usage 00013 ** Licensees holding valid commercial QxOrm licenses may use this file in 00014 ** accordance with the commercial license agreement provided with the 00015 ** Software or, alternatively, in accordance with the terms contained in 00016 ** a written agreement between you and Lionel Marty 00017 ** 00018 ** GNU General Public License Usage 00019 ** Alternatively, this file may be used under the terms of the GNU 00020 ** General Public License version 3.0 as published by the Free Software 00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the 00022 ** packaging of this file. Please review the following information to 00023 ** ensure the GNU General Public License version 3.0 requirements will be 00024 ** met : http://www.gnu.org/copyleft/gpl.html 00025 ** 00026 ** If you are unsure which license is appropriate for your use, or 00027 ** if you have questions regarding the use of this file, please contact : 00028 ** contact@qxorm.com 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _QX_SERIALIZE_QDATASTREAM_H_ 00033 #define _QX_SERIALIZE_QDATASTREAM_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <exception> 00047 00048 #include <QtCore/qdatastream.h> 00049 #include <QtCore/qfile.h> 00050 00051 #include <QxCommon/QxBool.h> 00052 00053 namespace qx { 00054 namespace serialization { 00055 00060 namespace qt { 00061 00062 template <class T> 00063 inline QByteArray to_byte_array(const T & obj, void * owner = NULL, unsigned int flags = 1 /* boost::archive::no_header */) 00064 { 00065 Q_UNUSED(flags); Q_UNUSED(owner); 00066 QByteArray ba; QString err; 00067 QDataStream stream((& ba), QIODevice::WriteOnly); 00068 stream << (quint32)(9438); 00069 try { stream << obj; } 00070 catch (const std::exception & e) { err = QString("serialization error '%ERR%'").replace("%ERR%", e.what()); } 00071 catch (...) { err = QString("serialization error '%ERR%'").replace("%ERR%", "unknown error"); } 00072 if (! err.isEmpty()) { qDebug("[QxOrm] qx::serialization::qt::to_byte_array() : %s", qPrintable(err)); ba.clear(); } 00073 return ba; 00074 } 00075 00076 template <class T> 00077 inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */) 00078 { 00079 Q_UNUSED(flags); 00080 qx_bool result = false; 00081 if (data.isEmpty()) { return qx_bool(false, "input binary data is empty"); } 00082 QDataStream stream(data); 00083 quint32 magic = 0; stream >> magic; 00084 if (magic != 9438) { return qx_bool(false, "input binary data is not valid"); } 00085 try { stream >> obj; result = true; } 00086 catch (const std::exception & e) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", e.what())); } 00087 catch (...) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", "unknown error")); } 00088 if (! result.getDesc().isEmpty()) { QString msg = result.getDesc(); qDebug("[QxOrm] qx::serialization::qt::from_byte_array() : %s", qPrintable(msg)); } 00089 return result; 00090 } 00091 00092 template <class T> 00093 inline QString to_string(const T & obj, unsigned int flags = 1 /* boost::archive::no_header */) 00094 { return qx::serialization::qt::to_byte_array(obj, NULL, flags).toBase64(); } 00095 00096 template <class T> 00097 inline qx_bool from_string(T & obj, const QString & sString, unsigned int flags = 1 /* boost::archive::no_header */) 00098 { QByteArray data = QByteArray::fromBase64(sString.toLatin1()); return qx::serialization::qt::from_byte_array(obj, data, flags); } 00099 00100 template <class T> 00101 inline qx_bool to_file(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */) 00102 { 00103 QByteArray data = qx::serialization::qt::to_byte_array(obj, NULL, flags); 00104 QFile file(sFileName); 00105 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate)) 00106 { return qx_bool(false, "cannot open file : " + sFileName); } 00107 file.write(data); 00108 file.close(); 00109 return qx_bool(true); 00110 } 00111 00112 template <class T> 00113 inline qx_bool from_file(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */) 00114 { 00115 QFile file(sFileName); 00116 if (! file.open(QIODevice::ReadOnly)) 00117 { return qx_bool(false, "cannot open file : " + sFileName); } 00118 QByteArray data = file.readAll(); file.close(); 00119 return qx::serialization::qt::from_byte_array(obj, data, flags); 00120 } 00121 00122 template <class T> 00123 inline qx_bool to_file_compressed(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, int iCompressionLevel = -1) 00124 { 00125 QByteArray data = qx::serialization::qt::to_byte_array(obj, NULL, flags); 00126 QByteArray compressed = qCompress(data, iCompressionLevel); 00127 QFile file(sFileName); 00128 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate)) 00129 { return qx_bool(false, "cannot open file : " + sFileName); } 00130 file.write(compressed); 00131 file.close(); 00132 return qx_bool(true); 00133 } 00134 00135 template <class T> 00136 inline qx_bool from_file_compressed(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */) 00137 { 00138 QFile file(sFileName); 00139 if (! file.open(QIODevice::ReadOnly)) 00140 { return qx_bool(false, "cannot open file : " + sFileName); } 00141 QByteArray data = file.readAll(); file.close(); 00142 QByteArray uncompressed = qUncompress(data); 00143 return qx::serialization::qt::from_byte_array(obj, uncompressed, flags); 00144 } 00145 00146 } // namespace qt 00147 } // namespace serialization 00148 } // namespace qx 00149 00150 #endif // _QX_SERIALIZE_QDATASTREAM_H_