QxOrm  1.5.0
C++ Object Relational Mapping library
QxClassX.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 #ifndef _QX_CLASS_X_H_
00033 #define _QX_CLASS_X_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
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 std::function<QString (const QVariant &, int, const unsigned int)> type_fct_save_qvariant_usertype;
00073    typedef std::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 
00097 #ifndef _QX_NO_RTTI
00098    const std::type_info & typeInfo(const QString & sKey) const;
00099 #endif // _QX_NO_RTTI
00100 
00101 public:
00102 
00103    static qx::any create(const QString & sKey);
00104    static IxClass * getClass(const QString & sKey);
00105    static IxDataMemberX * getDataMemberX(const QString & sKey);
00106    static IxFunctionX * getFctMemberX(const QString & sKey);
00107    static IxFunctionX * getFctStaticX(const QString & sKey);
00108    static IxDataMember * getDataMember(const QString & sClassKey, const QString & sDataKey, bool bRecursive = true);
00109    static IxFunction * getFctMember(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true);
00110    static IxFunction * getFctStatic(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true);
00111    static bool implementIxPersistable(const QString & sKey, bool bTraceIfFalse = true);
00112 
00113    static QxCollection<QString, IxClass *> * getAllClasses();
00114    static void registerAllClasses(bool bInitAllRelations = true, bool bInitValidator = true);
00115    static QString dumpAllClasses();
00116    static QString dumpSqlSchema();
00117 
00118    static QHash<QString, QString> * getAllValidatorMessage()               { return (& QxClassX::getSingleton()->m_lstValidatorMessage); }
00119    static QHash<QString, QString> * getAllSqlTypeByClassName()             { return (& QxClassX::getSingleton()->m_lstSqlTypeByClassName); }
00120    static QString getSqlTypeByClassName(const QString & sClassName)        { return QxClassX::getAllSqlTypeByClassName()->value(sClassName); }
00121 
00122 #ifndef _QX_NO_RTTI
00123    static const std::type_info & getTypeInfo(const QString & sClassName)   { return QxClassX::getSingleton()->typeInfo(sClassName); }
00124 #endif // _QX_NO_RTTI
00125 
00126    static type_fct_save_qvariant_usertype getFctSaveQVariantUserType()           { return QxClassX::getSingleton()->m_fctSaveQVariantUserType; }
00127    static type_fct_load_qvariant_usertype getFctLoadQVariantUserType()           { return QxClassX::getSingleton()->m_fctLoadQVariantUserType; }
00128    static void setFctSaveQVariantUserType(type_fct_save_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctSaveQVariantUserType = fct; }
00129    static void setFctLoadQVariantUserType(type_fct_load_qvariant_usertype fct)   { QxClassX::getSingleton()->m_fctLoadQVariantUserType = fct; }
00130 
00131    template <class U>
00132    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), qx::any * ret = NULL)
00133    {
00134       typedef typename std::conditional< std::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00135       typedef typename std::conditional< 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    template <class U>
00140    static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, qx::any * ret = NULL)
00141    {
00142       typedef typename std::conditional< std::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1;
00143       typedef typename std::conditional< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2;
00144       return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret);
00145    }
00146 
00147    static qx_bool invokeStatic(const QString & sClassKey, const QString & sFctKey, const QString & params = QString(), qx::any * ret = NULL);
00148    static qx_bool invokeStatic(const QString & sClassKey, const QString & sFctKey, const type_any_params & params, qx::any * ret = NULL);
00149 
00150 private:
00151 
00152    template <class U>
00153    struct invoke_ptr
00154    {
00155       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), qx::any * ret = NULL)
00156       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00157       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, qx::any * ret = NULL)
00158       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); }
00159    };
00160 
00161    template <class U>
00162    struct invoke_default
00163    {
00164       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), qx::any * ret = NULL)
00165       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00166       static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, qx::any * ret = NULL)
00167       { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); }
00168    };
00169 
00170    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const QString & params = QString(), qx::any * ret = NULL);
00171    static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const type_any_params & params, qx::any * ret = NULL);
00172 
00173    static bool isValid_DataMember(IxDataMember * p);
00174    static bool isValid_SqlRelation(IxDataMember * p);
00175 
00176 };
00177 
00178 } // namespace qx
00179 
00180 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxClassX)
00181 
00182 #endif // _QX_CLASS_X_H_