QxOrm  1.4.3
C++ Object Relational Mapping library
QxSerialize_QSharedPointer.h
Go to the documentation of this file.
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 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00033 #ifndef _QX_SERIALIZE_QSHAREDPOINTER_H_
00034 #define _QX_SERIALIZE_QSHAREDPOINTER_H_
00035 
00036 #ifdef _MSC_VER
00037 #pragma once
00038 #endif
00039 
00040 #include <boost/serialization/serialization.hpp>
00041 #include <boost/serialization/split_free.hpp>
00042 #include <boost/serialization/version.hpp>
00043 #include <boost/serialization/nvp.hpp>
00044 
00045 #include <QtCore/qsharedpointer.h>
00046 #include <QtCore/QWeakPointer>
00047 #include <QtCore/qhash.h>
00048 
00049 namespace boost {
00050 namespace serialization {
00051 
00052 template <class Archive, typename T>
00053 inline void save(Archive & ar, const QSharedPointer<T> & t, const unsigned int file_version)
00054 {
00055    Q_UNUSED(file_version);
00056    const T * ptr = t.data();
00057    ar << boost::serialization::make_nvp("qt_shared_ptr", ptr);
00058 }
00059 
00060 template <class Archive, typename T>
00061 inline void load(Archive & ar, QSharedPointer<T> & t, const unsigned int file_version)
00062 {
00063    Q_UNUSED(file_version);
00064    static QHash<void *, QWeakPointer<T> > listOfWeakPointers;
00065    T * ptr = NULL; ar >> boost::serialization::make_nvp("qt_shared_ptr", ptr);
00066    if (! ptr) { t = QSharedPointer<T>(); return; }
00067 
00068    void * pVoid = static_cast<void *>(ptr);
00069    QSharedPointer<T> pShared = (listOfWeakPointers.contains(pVoid) ? listOfWeakPointers.value(pVoid).toStrongRef() : QSharedPointer<T>());
00070    if (pShared) { t = pShared; return; }
00071    t = QSharedPointer<T>(ptr);
00072    QWeakPointer<T> pWeak = t;
00073    listOfWeakPointers.insert(pVoid, pWeak);
00074 
00075    static int iLoadCount = 0;
00076    iLoadCount++; if (iLoadCount <= 1000) { return; }
00077    QMutableHashIterator<void *, QWeakPointer<T> > itr(listOfWeakPointers);
00078    while (itr.hasNext()) { itr.next(); if (itr.value().isNull()) { itr.remove(); } }
00079    iLoadCount = 0;
00080 }
00081 
00082 template <class Archive, typename T>
00083 inline void serialize(Archive & ar, QSharedPointer<T> & t, const unsigned int file_version)
00084 {
00085    boost::serialization::split_free(ar, t, file_version);
00086 }
00087 
00088 } // namespace boost
00089 } // namespace serialization
00090 
00091 #endif // _QX_SERIALIZE_QSHAREDPOINTER_H_
00092 #endif // _QX_ENABLE_BOOST_SERIALIZATION