QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxSerializeQDataStream.h
Go to the documentation of this file.
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
32#ifndef _QX_SERIALIZE_QDATASTREAM_H_
33#define _QX_SERIALIZE_QDATASTREAM_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <exception>
47
48#include <QtCore/qdatastream.h>
49#include <QtCore/qfile.h>
50
51#include <QxCommon/QxBool.h>
52
53namespace qx {
54namespace serialization {
55
60namespace qt {
61
62template <class T>
63inline QByteArray to_byte_array(const T & obj, void * owner = NULL, unsigned int flags = 1 /* boost::archive::no_header */)
64{
65 Q_UNUSED(flags); Q_UNUSED(owner);
66 QByteArray ba; QString err;
67 QDataStream stream((& ba), QIODevice::WriteOnly);
68 stream << (quint32)(9438);
69 try { stream << obj; }
70 catch (const std::exception & e) { err = QString("serialization error '%ERR%'").replace("%ERR%", e.what()); }
71 catch (...) { err = QString("serialization error '%ERR%'").replace("%ERR%", "unknown error"); }
72 if (! err.isEmpty()) { qDebug("[QxOrm] qx::serialization::qt::to_byte_array() : %s", qPrintable(err)); ba.clear(); }
73 return ba;
74}
75
76template <class T>
77inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */)
78{
79 Q_UNUSED(flags);
80 qx_bool result = false;
81 if (data.isEmpty()) { return qx_bool(false, "input binary data is empty"); }
82 QDataStream stream(data);
83 quint32 magic = 0; stream >> magic;
84 if (magic != 9438) { return qx_bool(false, "input binary data is not valid"); }
85 try { stream >> obj; result = true; }
86 catch (const std::exception & e) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", e.what())); }
87 catch (...) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", "unknown error")); }
88 if (! result.getDesc().isEmpty()) { QString msg = result.getDesc(); qDebug("[QxOrm] qx::serialization::qt::from_byte_array() : %s", qPrintable(msg)); }
89 return result;
90}
91
92template <class T>
93inline QString to_string(const T & obj, unsigned int flags = 1 /* boost::archive::no_header */)
94{ return qx::serialization::qt::to_byte_array(obj, NULL, flags).toBase64(); }
95
96template <class T>
97inline qx_bool from_string(T & obj, const QString & sString, unsigned int flags = 1 /* boost::archive::no_header */)
98{ QByteArray data = QByteArray::fromBase64(sString.toLatin1()); return qx::serialization::qt::from_byte_array(obj, data, flags); }
99
100template <class T>
101inline qx_bool to_file(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */)
102{
103 QByteArray data = qx::serialization::qt::to_byte_array(obj, NULL, flags);
104 QFile file(sFileName);
105 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
106 { return qx_bool(false, "cannot open file : " + sFileName); }
107 file.write(data);
108 file.close();
109 return qx_bool(true);
110}
111
112template <class T>
113inline qx_bool from_file(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */)
114{
115 QFile file(sFileName);
116 if (! file.open(QIODevice::ReadOnly))
117 { return qx_bool(false, "cannot open file : " + sFileName); }
118 QByteArray data = file.readAll(); file.close();
119 return qx::serialization::qt::from_byte_array(obj, data, flags);
120}
121
122template <class T>
123inline qx_bool to_file_compressed(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, int iCompressionLevel = -1)
124{
125 QByteArray data = qx::serialization::qt::to_byte_array(obj, NULL, flags);
126 QByteArray compressed = qCompress(data, iCompressionLevel);
127 QFile file(sFileName);
128 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
129 { return qx_bool(false, "cannot open file : " + sFileName); }
130 file.write(compressed);
131 file.close();
132 return qx_bool(true);
133}
134
135template <class T>
136inline qx_bool from_file_compressed(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */)
137{
138 QFile file(sFileName);
139 if (! file.open(QIODevice::ReadOnly))
140 { return qx_bool(false, "cannot open file : " + sFileName); }
141 QByteArray data = file.readAll(); file.close();
142 QByteArray uncompressed = qUncompress(data);
143 return qx::serialization::qt::from_byte_array(obj, uncompressed, flags);
144}
145
146} // namespace qt
147} // namespace serialization
148} // namespace qx
149
150#endif // _QX_SERIALIZE_QDATASTREAM_H_
qx_bool : QxOrm library boolean type with code and description message when an error occured
qx::QxBool qx_bool
Definition QxBool.h:150
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
QString getDesc() const
Definition QxBool.h:97
void setDesc(const QString &sDesc)
Definition QxBool.h:101
qx_bool to_file(const T &obj, const QString &sFileName, unsigned int flags=1)
QString to_string(const T &obj, unsigned int flags=1)
qx_bool from_string(T &obj, const QString &sString, unsigned int flags=1)
qx_bool from_file_compressed(T &obj, const QString &sFileName, unsigned int flags=1)
qx_bool from_file(T &obj, const QString &sFileName, unsigned int flags=1)
qx_bool from_byte_array(T &obj, const QByteArray &data, unsigned int flags=1)
QByteArray to_byte_array(const T &obj, void *owner=NULL, unsigned int flags=1)
qx_bool to_file_compressed(const T &obj, const QString &sFileName, unsigned int flags=1, int iCompressionLevel=-1)
Root namespace for all QxOrm library features.