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