QxOrm  1.3.2
C++ Object Relational Mapping library
QxAnyCastDynamic.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_ANY_CAST_DYNAMIC_H_
00033 #define _QX_ANY_CAST_DYNAMIC_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <boost/any.hpp>
00047 #include <boost/shared_ptr.hpp>
00048 
00049 #include <QtCore/qsharedpointer.h>
00050 
00051 #include <QxDao/QxDaoPointer.h>
00052 
00053 #ifdef _QX_CPP_11_SMART_PTR
00054 #ifndef BOOST_NO_CXX11_SMART_PTR
00055 #include <memory>
00056 #endif // BOOST_NO_CXX11_SMART_PTR
00057 #endif // _QX_CPP_11_SMART_PTR
00058 
00059 namespace qx {
00060 
00061 template <typename T>
00062 struct any_cast_dynamic
00063 { static T get(const boost::any & a) { return boost::any_cast<T>(a); } };
00064 
00065 template <typename T>
00066 struct any_cast_dynamic<T *>
00067 {
00068    static T * get(const boost::any & a)
00069    {
00070       if (a.empty()) { return NULL; }
00071       boost::any * b = const_cast<boost::any *>(& a);
00072       T ** t = boost::unsafe_any_cast<T *>(b);
00073       if (! t) { return NULL; }
00074       return (* t);
00075    }
00076 };
00077 
00078 template <typename T>
00079 struct any_cast_dynamic< boost::shared_ptr<T> >
00080 {
00081    static boost::shared_ptr<T> get(const boost::any & a)
00082    {
00083       if (a.empty()) { return boost::shared_ptr<T>(); }
00084       boost::any * b = const_cast<boost::any *>(& a);
00085       boost::shared_ptr<T> * t = boost::unsafe_any_cast< boost::shared_ptr<T> >(b);
00086       if (! t) { return boost::shared_ptr<T>(); }
00087       return (* t);
00088    }
00089 };
00090 
00091 template <typename T>
00092 struct any_cast_dynamic< QSharedPointer<T> >
00093 {
00094    static QSharedPointer<T> get(const boost::any & a)
00095    {
00096       if (a.empty()) { return QSharedPointer<T>(); }
00097       boost::any * b = const_cast<boost::any *>(& a);
00098       QSharedPointer<T> * t = boost::unsafe_any_cast< QSharedPointer<T> >(b);
00099       if (! t) { return QSharedPointer<T>(); }
00100       return (* t);
00101    }
00102 };
00103 
00104 template <typename T>
00105 struct any_cast_dynamic< qx::dao::ptr<T> >
00106 {
00107    static qx::dao::ptr<T> get(const boost::any & a)
00108    {
00109       if (a.empty()) { return qx::dao::ptr<T>(); }
00110       boost::any * b = const_cast<boost::any *>(& a);
00111       qx::dao::ptr<T> * t = boost::unsafe_any_cast< qx::dao::ptr<T> >(b);
00112       if (! t) { return qx::dao::ptr<T>(); }
00113       return (* t);
00114    }
00115 };
00116 
00117 #ifdef _QX_CPP_11_SMART_PTR
00118 #ifndef BOOST_NO_CXX11_SMART_PTR
00119 
00120 template <typename T>
00121 struct any_cast_dynamic< std::shared_ptr<T> >
00122 {
00123    static std::shared_ptr<T> get(const boost::any & a)
00124    {
00125       if (a.empty()) { return std::shared_ptr<T>(); }
00126       boost::any * b = const_cast<boost::any *>(& a);
00127       std::shared_ptr<T> * t = boost::unsafe_any_cast< std::shared_ptr<T> >(b);
00128       if (! t) { return std::shared_ptr<T>(); }
00129       return (* t);
00130    }
00131 };
00132 
00133 #endif // BOOST_NO_CXX11_SMART_PTR
00134 #endif // _QX_CPP_11_SMART_PTR
00135 
00136 } // namespace qx
00137 
00138 #endif // _QX_ANY_CAST_DYNAMIC_H_