QxOrm  1.2.4
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    static bool implementIxPersistable(const QString & sKey, bool bTraceIfFalse = true);
00107 
00108    static QxCollection<QString, IxClass *> * getAllClasses();
00109    static void registerAllClasses();
00110    static QString dumpAllClasses();
00111    static QString dumpSqlSchema();
00112 
00113    static QHash<QString, QString> * getAllValidatorMessage()               { return (& QxClassX::getSingleton()->m_lstValidatorMessage); }
00114    static QHash<QString, QString> * getAllSqlTypeByClassName()             { return (& QxClassX::getSingleton()->m_lstSqlTypeByClassName); }
00115    static QString getSqlTypeByClassName(const QString & sClassName)        { return QxClassX::getAllSqlTypeByClassName()->value(sClassName); }
00116    static const std::type_info & getTypeInfo(const QString & sClassName)   { return QxClassX::getSingleton()->typeInfo(sClassName); }
00117 
00118    static type_fct_save_qvariant_usertype getFctSaveQVariantUserType()           { return QxClassX::getSingleton()->m_fctSaveQVariantUserType; }
00119    static type_fct_load_qvariant_usertype getFctLoadQVariantUserType()           { return QxClassX::getSingleton()->m_fctLoadQVariantUserType; }
00120    static void setFctSaveQVariantUserType(type_fct_save_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctSaveQVariantUserType = fct; }
00121    static void setFctLoadQVariantUserType(type_fct_load_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctLoadQVariantUserType = fct; }
00122 
00123    template <class U>
00124    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00125    {
00126       typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00127       typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00128       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00129    }
00130 
00131    template <class U>
00132    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00133    {
00134       typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00135       typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00136       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00137    }
00138 
00139 private:
00140 
00141    template <class U>
00142    struct invoke_ptr
00143    {
00144       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00145       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00146       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00147       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00148    };
00149 
00150    template <class U>
00151    struct invoke_default
00152    {
00153       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00154       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00155       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00156       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00157    };
00158 
00159    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const QString & params = QString(), boost::any * ret = NULL);
00160    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const type_any_params & params, boost::any * ret = NULL);
00161 
00162    static bool isValid_DataMember(IxDataMember * p);
00163    static bool isValid_SqlRelation(IxDataMember * p);
00164 
00165 };
00166 
00167 } // namespace qx
00168 
00169 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxClassX)
00170 
00171 #endif // _QX_CLASS_X_H_