QxOrm  1.4.9
C++ Object Relational Mapping library
QxClone.h
Go to the documentation of this file.
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_CLONE_H_
00033 #define _QX_CLONE_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <string>
00047 #include <iostream>
00048 #include <sstream>
00049 #include <exception>
00050 
00051 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00052 
00053 #include <boost/archive/archive_exception.hpp>
00054 
00055 #include <QxSerialize/boost/QxSerializeInclude.h>
00056 #include <QxSerialize/QxBoostSerializeHelper/QxBoostSerializeRegisterHelperX.h>
00057 #include <QxSerialize/QxSerializeInvoker.h>
00058 
00059 #else // _QX_ENABLE_BOOST_SERIALIZATION
00060 
00061 #include <QxSerialize/QxSerializeQDataStream.h>
00062 #include <QxSerialize/QDataStream/QxSerializeQDataStream_all_include.h>
00063 
00064 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00065 
00066 #define QX_STR_CLONE_SERIALIZATION_ERROR "[QxOrm] qx::clone() serialization error : '%s'"
00067 #define QX_STR_CLONE_DESERIALIZATION_ERROR "[QxOrm] qx::clone() deserialization error : '%s'"
00068 
00069 namespace qx {
00070 
00071 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00072 
00077 template <class T>
00078 T * clone_to_nude_ptr(const T & obj)
00079 {
00080    QX_CLONE_STRING_STREAM ioss(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
00081    QX_CLONE_BINARY_OUTPUT_ARCHIVE oar(ioss, boost::archive::no_header);
00082    QxBoostSerializeRegisterHelperX::helper(oar);
00083    bool bSerializeOk = false;
00084 
00085    try { oar << obj; bSerializeOk = ioss.good(); }
00086    catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
00087    catch (const std::exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
00088    catch (...) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, "unknown error"); }
00089    if (! bSerializeOk) { qAssert(false); return NULL; }
00090 
00091    T * pClone = new T();
00092    QX_CLONE_BINARY_INPUT_ARCHIVE iar(ioss, boost::archive::no_header);
00093    QxBoostSerializeRegisterHelperX::helper(iar);
00094    bool bDeserializeOk = false;
00095 
00096    try { iar >> (* pClone); bDeserializeOk = ioss.good(); }
00097    catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
00098    catch (const std::exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
00099    catch (...) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, "unknown error"); }
00100    qAssert(bDeserializeOk);
00101 
00102    return (bDeserializeOk ? pClone : NULL);
00103 }
00104 
00105 #else // _QX_ENABLE_BOOST_SERIALIZATION
00106 
00111 template <class T>
00112 T * clone_to_nude_ptr(const T & obj)
00113 {
00114    QByteArray baClone = qx::serialization::qt::to_byte_array(obj);
00115    if (baClone.isEmpty()) { qAssertMsg(false, "[QxOrm] qx::clone_to_nude_ptr", "an error occurred during QDataStream serialization process"); return NULL; }
00116    T * pClone = new T();
00117    qx_bool bOk = qx::serialization::qt::from_byte_array((* pClone), baClone);
00118    return (bOk ? pClone : NULL);
00119 }
00120 
00121 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00122 
00127 template <class T>
00128 std::shared_ptr<T> clone(const T & obj)
00129 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return std::shared_ptr<T>(ptr); }
00130 
00131 #ifdef _QX_ENABLE_BOOST
00132 
00137 template <class T>
00138 boost::shared_ptr<T> clone_to_boost_shared_ptr(const T & obj)
00139 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return boost::shared_ptr<T>(ptr); }
00140 
00141 #endif // _QX_ENABLE_BOOST
00142 
00147 template <class T>
00148 QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj)
00149 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return QSharedPointer<T>(ptr); }
00150 
00155 template <class T>
00156 std::shared_ptr<T> clone_to_std_shared_ptr(const T & obj)
00157 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return std::shared_ptr<T>(ptr); }
00158 
00159 } // namespace qx
00160 
00161 #endif // _QX_CLONE_H_