QxOrm  1.2.3
C++ Object Relational Mapping library
QxClassX.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_CLASS_X_H_
00027 #define _QX_CLASS_X_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/function.hpp>
00041 #include <boost/mpl/if.hpp>
00042 #include <boost/mpl/or.hpp>
00043 #include <boost/mpl/logical.hpp>
00044 #include <boost/type_traits/is_pointer.hpp>
00045 
00046 #include <QxCommon/QxBool.h>
00047 
00048 #include <QxRegister/IxClass.h>
00049 
00050 #include <QxSingleton/QxSingleton.h>
00051 
00052 #include <QxCollection/QxCollection.h>
00053 
00054 #include <QxTraits/is_smart_ptr.h>
00055 #include <QxTraits/get_sql_type.h>
00056 
00057 namespace qx {
00058 
00063 class QX_DLL_EXPORT QxClassX : public QxSingleton<QxClassX>
00064 {
00065 
00066    friend class QxSingleton<QxClassX>;
00067    friend class IxClass;
00068 
00069 public:
00070 
00071    typedef IxFunction::type_any_params type_any_params;
00072    typedef boost::function<QString (const QVariant &, int, const unsigned int)> type_fct_save_qvariant_usertype;
00073    typedef boost::function<QVariant (const QString &, int, const unsigned int)> type_fct_load_qvariant_usertype;
00074 
00075 protected:
00076 
00077    QxCollection<QString, IxClass *> m_lstClass;                   
00078    QHash<QString, QString> m_lstSqlTypeByClassName;               
00079    QHash<QString, QString> m_lstValidatorMessage;                 
00080    type_fct_save_qvariant_usertype m_fctSaveQVariantUserType;     
00081    type_fct_load_qvariant_usertype m_fctLoadQVariantUserType;     
00082 
00083 private:
00084 
00085    QxClassX();
00086    virtual ~QxClassX();
00087 
00088    QxCollection<QString, IxClass *> * getAll();
00089    IxClass * get(const QString & sKey) const;
00090    bool exist(const QString & sKey) const;
00091    bool insert(const QString & sKey, IxClass * pClass);
00092    bool remove(const QString & sKey);
00093    void clear();
00094    void initSqlTypeByClassName();
00095    void initValidatorMessage();
00096    const std::type_info & typeInfo(const QString & sKey) const;
00097 
00098 public:
00099 
00100    static boost::any create(const QString & sKey);
00101    static IxClass * getClass(const QString & sKey);
00102    static IxDataMemberX * getDataMemberX(const QString & sKey);
00103    static IxFunctionX * getFctMemberX(const QString & sKey);
00104    static IxDataMember * getDataMember(const QString & sClassKey, const QString & sDataKey, bool bRecursive = true);
00105    static IxFunction * getFctMember(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true);
00106 
00107    static QxCollection<QString, IxClass *> * getAllClasses();
00108    static void registerAllClasses();
00109    static QString dumpAllClasses();
00110    static QString dumpSqlSchema();
00111 
00112    static QHash<QString, QString> * getAllValidatorMessage()               { return (& QxClassX::getSingleton()->m_lstValidatorMessage); }
00113    static QHash<QString, QString> * getAllSqlTypeByClassName()             { return (& QxClassX::getSingleton()->m_lstSqlTypeByClassName); }
00114    static QString getSqlTypeByClassName(const QString & sClassName)        { return QxClassX::getAllSqlTypeByClassName()->value(sClassName); }
00115    static const std::type_info & getTypeInfo(const QString & sClassName)   { return QxClassX::getSingleton()->typeInfo(sClassName); }
00116 
00117    static type_fct_save_qvariant_usertype getFctSaveQVariantUserType()           { return QxClassX::getSingleton()->m_fctSaveQVariantUserType; }
00118    static type_fct_load_qvariant_usertype getFctLoadQVariantUserType()           { return QxClassX::getSingleton()->m_fctLoadQVariantUserType; }
00119    static void setFctSaveQVariantUserType(type_fct_save_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctSaveQVariantUserType = fct; }
00120    static void setFctLoadQVariantUserType(type_fct_load_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctLoadQVariantUserType = fct; }
00121 
00122    template <class U>
00123    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00124    {
00125       typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00126       typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00127       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00128    }
00129 
00130    template <class U>
00131    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00132    {
00133       typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00134       typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00135       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00136    }
00137 
00138 private:
00139 
00140    template <class U>
00141    struct invoke_ptr
00142    {
00143       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00144       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00145       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00146       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00147    };
00148 
00149    template <class U>
00150    struct invoke_default
00151    {
00152       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00153       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00154       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00155       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00156    };
00157 
00158    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const QString & params = QString(), boost::any * ret = NULL);
00159    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const type_any_params & params, boost::any * ret = NULL);
00160 
00161    static bool isValid_DataMember(IxDataMember * p);
00162    static bool isValid_SqlRelation(IxDataMember * p);
00163 
00164 };
00165 
00166 } // namespace qx
00167 
00168 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxClassX)
00169 
00170 #endif // _QX_CLASS_X_H_