QxOrm  1.4.2
C++ Object Relational Mapping library
QxSerializeInvoker.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_INVOKER_H_
00034 #define _QX_SERIALIZE_INVOKER_H_
00035 
00036 #ifdef _MSC_VER
00037 #pragma once
00038 #endif
00039 
00040 #ifdef _MSC_VER
00041 #pragma warning(push)
00042 #pragma warning(disable:4996)
00043 #pragma warning(disable:4094)
00044 #endif // _MSC_VER
00045 
00046 #include <boost/mpl/if.hpp>
00047 #include <boost/mpl/logical.hpp>
00048 #include <boost/static_assert.hpp>
00049 
00050 #include <boost/serialization/serialization.hpp>
00051 #include <boost/serialization/base_object.hpp>
00052 #include <boost/serialization/nvp.hpp>
00053 
00054 #include <QxTraits/get_base_class.h>
00055 #include <QxTraits/is_qx_registered.h>
00056 
00057 #include <QxRegister/QxClass.h>
00058 
00059 namespace qx {
00060 namespace serialization {
00061 namespace detail {
00062 
00063 template <class Base>
00064 struct base_class
00065 {
00066    template <class Archive, class T>
00067    static inline void save(Archive & ar, const T & t, const unsigned int file_version)
00068    {
00069       Q_UNUSED(file_version);
00070       BOOST_STATIC_ASSERT(qx::trait::is_qx_registered<T>::value);
00071       const char * sTag = QxClass<Base>::getSingleton()->getNamePtr();
00072       ar << boost::serialization::make_nvp(sTag, boost::serialization::base_object<const Base>(t));
00073    }
00074    template <class Archive, class T>
00075    static inline void load(Archive & ar, T & t, const unsigned int file_version)
00076    {
00077       Q_UNUSED(file_version);
00078       BOOST_STATIC_ASSERT(qx::trait::is_qx_registered<T>::value);
00079       const char * sTag = QxClass<Base>::getSingleton()->getNamePtr();
00080       ar >> boost::serialization::make_nvp(sTag, boost::serialization::base_object<Base>(t));
00081    }
00082 };
00083 
00084 template <>
00085 struct base_class<qx::trait::no_base_class_defined>
00086 {
00087    template <class Archive, class T>
00088    static inline void save(Archive & ar, const T & t, const unsigned int file_version)
00089    { Q_UNUSED(ar); Q_UNUSED(t); Q_UNUSED(file_version); }
00090    template <class Archive, class T>
00091    static inline void load(Archive & ar, T & t, const unsigned int file_version)
00092    { Q_UNUSED(ar); Q_UNUSED(t); Q_UNUSED(file_version); }
00093 };
00094 
00095 template <class Archive, class T>
00096 void save(Archive & ar, const T & t, const unsigned int file_version)
00097 {
00098    typedef typename qx::trait::get_base_class<T>::type qx_type_base_class_tmp;
00099    qx::serialization::detail::base_class<qx_type_base_class_tmp>::save(ar, t, file_version);
00100    QxClass<T>::getSingleton()->dataMemberX()->toArchive(& t, ar, file_version);
00101 }
00102 
00103 template <class Archive, class T>
00104 void load(Archive & ar, T & t, const unsigned int file_version)
00105 {
00106    typedef typename qx::trait::get_base_class<T>::type qx_type_base_class_tmp;
00107    qx::serialization::detail::base_class<qx_type_base_class_tmp>::load(ar, t, file_version);
00108    QxClass<T>::getSingleton()->dataMemberX()->fromArchive(& t, ar, file_version);
00109 }
00110 
00111 template <class Archive, class T>
00112 struct saver
00113 {
00114    static inline void invoke(Archive & ar, const T & t, const unsigned int file_version)
00115    { qx::serialization::detail::save(ar, t, file_version); }
00116 };
00117 
00118 template <class Archive, class T>
00119 struct loader
00120 {
00121    static inline void invoke(Archive & ar, T & t, const unsigned int file_version)
00122    { qx::serialization::detail::load(ar, t, file_version); }
00123 };
00124 
00125 } // namespace detail
00126 } // namespace serialization
00127 } // namespace qx
00128 
00129 #include "../../inl/QxSerialize/QxSerializeInvoker.inl"
00130 
00131 #ifdef _MSC_VER
00132 #pragma warning(pop)
00133 #endif // _MSC_VER
00134 
00135 #endif // _QX_SERIALIZE_INVOKER_H_
00136 #endif // _QX_ENABLE_BOOST_SERIALIZATION