QxOrm  1.4.3
C++ Object Relational Mapping library
QxClassX.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_CLASS_X_H_
00033 #define _QX_CLASS_X_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <boost/function.hpp>
00047 #include <boost/mpl/if.hpp>
00048 #include <boost/mpl/or.hpp>
00049 #include <boost/mpl/logical.hpp>
00050 #include <boost/type_traits/is_pointer.hpp>
00051 
00052 #include <QxCommon/QxBool.h>
00053 
00054 #include <QxRegister/IxClass.h>
00055 
00056 #include <QxSingleton/QxSingleton.h>
00057 
00058 #include <QxCollection/QxCollection.h>
00059 
00060 #include <QxTraits/is_smart_ptr.h>
00061 #include <QxTraits/get_sql_type.h>
00062 
00063 namespace qx {
00064 
00069 class QX_DLL_EXPORT QxClassX : public QxSingleton<QxClassX>
00070 {
00071 
00072    friend class QxSingleton<QxClassX>;
00073    friend class IxClass;
00074 
00075 public:
00076 
00077    typedef IxFunction::type_any_params type_any_params;
00078    typedef boost::function<QString (const QVariant &, int, const unsigned int)> type_fct_save_qvariant_usertype;
00079    typedef boost::function<QVariant (const QString &, int, const unsigned int)> type_fct_load_qvariant_usertype;
00080 
00081 protected:
00082 
00083    QxCollection<QString, IxClass *> m_lstClass;                   
00084    QHash<QString, QString> m_lstSqlTypeByClassName;               
00085    QHash<QString, QString> m_lstValidatorMessage;                 
00086    type_fct_save_qvariant_usertype m_fctSaveQVariantUserType;     
00087    type_fct_load_qvariant_usertype m_fctLoadQVariantUserType;     
00088 
00089 private:
00090 
00091    QxClassX();
00092    virtual ~QxClassX();
00093 
00094    QxCollection<QString, IxClass *> * getAll();
00095    IxClass * get(const QString & sKey) const;
00096    bool exist(const QString & sKey) const;
00097    bool insert(const QString & sKey, IxClass * pClass);
00098    bool remove(const QString & sKey);
00099    void clear();
00100    void initSqlTypeByClassName();
00101    void initValidatorMessage();
00102 
00103 #ifndef _QX_NO_RTTI
00104    const std::type_info & typeInfo(const QString & sKey) const;
00105 #endif // _QX_NO_RTTI
00106 
00107 public:
00108 
00109    static boost::any create(const QString & sKey);
00110    static IxClass * getClass(const QString & sKey);
00111    static IxDataMemberX * getDataMemberX(const QString & sKey);
00112    static IxFunctionX * getFctMemberX(const QString & sKey);
00113    static IxFunctionX * getFctStaticX(const QString & sKey);
00114    static IxDataMember * getDataMember(const QString & sClassKey, const QString & sDataKey, bool bRecursive = true);
00115    static IxFunction * getFctMember(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true);
00116    static IxFunction * getFctStatic(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true);
00117    static bool implementIxPersistable(const QString & sKey, bool bTraceIfFalse = true);
00118 
00119    static QxCollection<QString, IxClass *> * getAllClasses();
00120    static void registerAllClasses();
00121    static QString dumpAllClasses();
00122    static QString dumpSqlSchema();
00123 
00124    static QHash<QString, QString> * getAllValidatorMessage()               { return (& QxClassX::getSingleton()->m_lstValidatorMessage); }
00125    static QHash<QString, QString> * getAllSqlTypeByClassName()             { return (& QxClassX::getSingleton()->m_lstSqlTypeByClassName); }
00126    static QString getSqlTypeByClassName(const QString & sClassName)        { return QxClassX::getAllSqlTypeByClassName()->value(sClassName); }
00127 
00128 #ifndef _QX_NO_RTTI
00129    static const std::type_info & getTypeInfo(const QString & sClassName)   { return QxClassX::getSingleton()->typeInfo(sClassName); }
00130 #endif // _QX_NO_RTTI
00131 
00132    static type_fct_save_qvariant_usertype getFctSaveQVariantUserType()           { return QxClassX::getSingleton()->m_fctSaveQVariantUserType; }
00133    static type_fct_load_qvariant_usertype getFctLoadQVariantUserType()           { return QxClassX::getSingleton()->m_fctLoadQVariantUserType; }
00134    static void setFctSaveQVariantUserType(type_fct_save_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctSaveQVariantUserType = fct; }
00135    static void setFctLoadQVariantUserType(type_fct_load_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctLoadQVariantUserType = fct; }
00136 
00137    template <class U>
00138    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00139    {
00140       typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00141       typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00142       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00143    }
00144 
00145    template <class U>
00146    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00147    {
00148       typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00149       typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00150       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00151    }
00152 
00153    static qx_bool invokeStatic(const QString & sClassKey, const QString & sFctKey, const QString & params = QString(), boost::any * ret = NULL);
00154    static qx_bool invokeStatic(const QString & sClassKey, const QString & sFctKey, const type_any_params & params, boost::any * ret = NULL);
00155 
00156 private:
00157 
00158    template <class U>
00159    struct invoke_ptr
00160    {
00161       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00162       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00163       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00164       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00165    };
00166 
00167    template <class U>
00168    struct invoke_default
00169    {
00170       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL)
00171       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00172       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL)
00173       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00174    };
00175 
00176    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const QString & params = QString(), boost::any * ret = NULL);
00177    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const type_any_params & params, boost::any * ret = NULL);
00178 
00179    static bool isValid_DataMember(IxDataMember * p);
00180    static bool isValid_SqlRelation(IxDataMember * p);
00181 
00182 };
00183 
00184 } // namespace qx
00185 
00186 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxClassX)
00187 
00188 #endif // _QX_CLASS_X_H_