QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxClone.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_CLONE_H_
33#define _QX_CLONE_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <string>
47#include <iostream>
48#include <sstream>
49#include <exception>
50
51#ifdef _QX_ENABLE_BOOST_SERIALIZATION
52
53#include <boost/archive/archive_exception.hpp>
54
58
59#else // _QX_ENABLE_BOOST_SERIALIZATION
60
63
64#endif // _QX_ENABLE_BOOST_SERIALIZATION
65
66#define QX_STR_CLONE_SERIALIZATION_ERROR "[QxOrm] qx::clone() serialization error : '%s'"
67#define QX_STR_CLONE_DESERIALIZATION_ERROR "[QxOrm] qx::clone() deserialization error : '%s'"
68
69namespace qx {
70
71#ifdef _QX_ENABLE_BOOST_SERIALIZATION
72
77template <class T>
78T * clone_to_nude_ptr(const T & obj)
79{
80 QX_CLONE_STRING_STREAM ioss(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
81 QX_CLONE_BINARY_OUTPUT_ARCHIVE oar(ioss, boost::archive::no_header);
83 bool bSerializeOk = false;
84
85 try { oar << obj; bSerializeOk = ioss.good(); }
86 catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
87 catch (const std::exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
88 catch (...) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, "unknown error"); }
89 if (! bSerializeOk) { qAssert(false); return NULL; }
90
91 T * pClone = new T();
92 QX_CLONE_BINARY_INPUT_ARCHIVE iar(ioss, boost::archive::no_header);
94 bool bDeserializeOk = false;
95
96 try { iar >> (* pClone); bDeserializeOk = ioss.good(); }
97 catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
98 catch (const std::exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
99 catch (...) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, "unknown error"); }
100 qAssert(bDeserializeOk);
101
102 return (bDeserializeOk ? pClone : NULL);
103}
104
105#else // _QX_ENABLE_BOOST_SERIALIZATION
106
111template <class T>
112T * clone_to_nude_ptr(const T & obj)
113{
114 QByteArray baClone = qx::serialization::qt::to_byte_array(obj);
115 if (baClone.isEmpty()) { qAssertMsg(false, "[QxOrm] qx::clone_to_nude_ptr", "an error occurred during QDataStream serialization process"); return NULL; }
116 T * pClone = new T();
117 qx_bool bOk = qx::serialization::qt::from_byte_array((* pClone), baClone);
118 return (bOk ? pClone : NULL);
119}
120
121#endif // _QX_ENABLE_BOOST_SERIALIZATION
122
127template <class T>
128std::shared_ptr<T> clone(const T & obj)
129{ T * ptr = qx::clone_to_nude_ptr<T>(obj); return std::shared_ptr<T>(ptr); }
130
131#ifdef _QX_ENABLE_BOOST
132
137template <class T>
138boost::shared_ptr<T> clone_to_boost_shared_ptr(const T & obj)
139{ T * ptr = qx::clone_to_nude_ptr<T>(obj); return boost::shared_ptr<T>(ptr); }
140
141#endif // _QX_ENABLE_BOOST
142
147template <class T>
148QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj)
149{ T * ptr = qx::clone_to_nude_ptr<T>(obj); return QSharedPointer<T>(ptr); }
150
155template <class T>
156std::shared_ptr<T> clone_to_std_shared_ptr(const T & obj)
157{ T * ptr = qx::clone_to_nude_ptr<T>(obj); return std::shared_ptr<T>(ptr); }
158
159} // namespace qx
160
161#endif // _QX_CLONE_H_
#define QX_STR_CLONE_DESERIALIZATION_ERROR
Definition QxClone.h:67
#define QX_STR_CLONE_SERIALIZATION_ERROR
Definition QxClone.h:66
#define qAssertMsg(test, where, what)
Definition QxMacro.h:60
#define qAssert(x)
Definition QxMacro.h:52
#define QX_CLONE_BINARY_OUTPUT_ARCHIVE
#define QX_CLONE_STRING_STREAM
#define QX_CLONE_BINARY_INPUT_ARCHIVE
Used when _QX_ENABLE_BOOST_SERIALIZATION compilation option is not defined to provide serialization e...
Include all Qt QDataStream serialization method (save/load) provided by QxOrm library.
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
QSharedPointer< T > clone_to_qt_shared_ptr(const T &obj)
qx::clone_to_qt_shared_ptr(const T & obj) : return a Qt smart-pointer (QSharedPointer<T>) of a new in...
Definition QxClone.h:148
T * clone_to_nude_ptr(const T &obj)
qx::clone_to_nude_ptr(const T & obj) : return a nude pointer (be careful with memory leak) of a new i...
Definition QxClone.h:78
std::shared_ptr< T > clone_to_std_shared_ptr(const T &obj)
qx::clone_to_std_shared_ptr(const T & obj) : return a C++11 std smart-pointer (std::shared_ptr<T>) of...
Definition QxClone.h:156
std::shared_ptr< T > clone(const T &obj)
qx::clone(const T & obj) : return a boost smart-pointer (std::shared_ptr<T>) of a new instance of typ...
Definition QxClone.h:128
boost::shared_ptr< T > clone_to_boost_shared_ptr(const T &obj)
qx::clone_to_boost_shared_ptr(const T & obj) : return a boost smart-pointer (boost::shared_ptr<T>) of...
Definition QxClone.h:138
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)
Root namespace for all QxOrm library features.