![]() |
QxOrm
1.2.7
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** http://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 #include <boost/archive/archive_exception.hpp> 00052 00053 #include <QxSerialize/boost/QxSerializeInclude.h> 00054 #include <QxSerialize/QxBoostSerializeHelper/QxBoostSerializeRegisterHelperX.h> 00055 #include <QxSerialize/QxSerializeInvoker.h> 00056 00057 #define QX_STR_CLONE_SERIALIZATION_ERROR "[QxOrm] qx::clone() serialization error : '%s'" 00058 #define QX_STR_CLONE_DESERIALIZATION_ERROR "[QxOrm] qx::clone() deserialization error : '%s'" 00059 00060 namespace qx { 00061 00066 template <class T> 00067 T * clone_to_nude_ptr(const T & obj) 00068 { 00069 QX_CLONE_STRING_STREAM ioss(std::ios_base::binary | std::ios_base::in | std::ios_base::out); 00070 QX_CLONE_BINARY_OUTPUT_ARCHIVE oar(ioss, boost::archive::no_header); 00071 QxBoostSerializeRegisterHelperX::helper(oar); 00072 bool bSerializeOk = false; 00073 00074 try { oar << obj; bSerializeOk = ioss.good(); } 00075 catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); } 00076 catch (const std::exception & e) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, e.what()); } 00077 catch (...) { qDebug(QX_STR_CLONE_SERIALIZATION_ERROR, "unknown error"); } 00078 if (! bSerializeOk) { qAssert(false); return NULL; } 00079 00080 T * pClone = new T(); 00081 QX_CLONE_BINARY_INPUT_ARCHIVE iar(ioss, boost::archive::no_header); 00082 QxBoostSerializeRegisterHelperX::helper(iar); 00083 bool bDeserializeOk = false; 00084 00085 try { iar >> (* pClone); bDeserializeOk = ioss.good(); } 00086 catch (const boost::archive::archive_exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); } 00087 catch (const std::exception & e) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, e.what()); } 00088 catch (...) { qDebug(QX_STR_CLONE_DESERIALIZATION_ERROR, "unknown error"); } 00089 qAssert(bDeserializeOk); 00090 00091 return (bDeserializeOk ? pClone : NULL); 00092 } 00093 00098 template <class T> 00099 boost::shared_ptr<T> clone(const T & obj) 00100 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return boost::shared_ptr<T>(ptr); } 00101 00106 template <class T> 00107 QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj) 00108 { T * ptr = qx::clone_to_nude_ptr<T>(obj); return QSharedPointer<T>(ptr); } 00109 00110 } // namespace qx 00111 00112 #endif // _QX_CLONE_H_