QxOrm  1.2.3
C++ Object Relational Mapping library
QxClone.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_CLONE_H_
00027 #define _QX_CLONE_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <string>
00041 #include <iostream>
00042 #include <sstream>
00043 #include <exception>
00044 
00045 #include <boost/archive/archive_exception.hpp>
00046 
00047 #include <QxSerialize/boost/QxSerializeInclude.h>
00048 #include <QxSerialize/QxBoostSerializeHelper/QxBoostSerializeRegisterHelperX.h>
00049 #include <QxSerialize/QxSerializeInvoker.h>
00050 
00051 #define QX_STR_CLONE_SERIALIZATION_ERROR "[QxOrm] qx::clone() serialization error : '%s'"
00052 #define QX_STR_CLONE_DESERIALIZATION_ERROR "[QxOrm] qx::clone() deserialization error : '%s'"
00053 
00054 namespace qx {
00055 
00060 template <class T>
00061 T * clone_to_nude_ptr(const T & obj)
00062 {
00063    QX_CLONE_STRING_STREAM ioss(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
00064    QX_CLONE_BINARY_OUTPUT_ARCHIVE oar(ioss, boost::archive::no_header);
00065    QxBoostSerializeRegisterHelperX::helper(oar);
00066    bool bSerializeOk = false;
00067 
00068    try { oar << obj; bSerializeOk = ioss.good(); }
00069    catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
00070    catch (const std::exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); }
00071    catch (...) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, "unknown error"); }
00072    if (! bSerializeOk) { qAssert(false); return NULL; }
00073 
00074    T * pClone = new T();
00075    QX_CLONE_BINARY_INPUT_ARCHIVE iar(ioss, boost::archive::no_header);
00076    QxBoostSerializeRegisterHelperX::helper(iar);
00077    bool bDeserializeOk = false;
00078 
00079    try { iar >> (* pClone); bDeserializeOk = ioss.good(); }
00080    catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
00081    catch (const std::exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); }
00082    catch (...) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, "unknown error"); }
00083    qAssert(bDeserializeOk);
00084 
00085    return (bDeserializeOk ? pClone : NULL);
00086 }
00087 
00092 template <class T>
00093 boost::shared_ptr<T> clone(const T & obj)
00094 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return boost::shared_ptr<T>(ptr); }
00095 
00100 template <class T>
00101 QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj)
00102 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return QSharedPointer<T>(ptr); }
00103 
00104 } // namespace qx
00105 
00106 #endif // _QX_CLONE_H_