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