![]() |
QxOrm
1.2.7
C++ Object Relational Mapping library
|
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 const std::type_info & typeInfo(const QString & sKey) const; 00103 00104 public: 00105 00106 static boost::any create(const QString & sKey); 00107 static IxClass * getClass(const QString & sKey); 00108 static IxDataMemberX * getDataMemberX(const QString & sKey); 00109 static IxFunctionX * getFctMemberX(const QString & sKey); 00110 static IxFunctionX * getFctStaticX(const QString & sKey); 00111 static IxDataMember * getDataMember(const QString & sClassKey, const QString & sDataKey, bool bRecursive = true); 00112 static IxFunction * getFctMember(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true); 00113 static IxFunction * getFctStatic(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true); 00114 static bool implementIxPersistable(const QString & sKey, bool bTraceIfFalse = true); 00115 00116 static QxCollection<QString, IxClass *> * getAllClasses(); 00117 static void registerAllClasses(); 00118 static QString dumpAllClasses(); 00119 static QString dumpSqlSchema(); 00120 00121 static QHash<QString, QString> * getAllValidatorMessage() { return (& QxClassX::getSingleton()->m_lstValidatorMessage); } 00122 static QHash<QString, QString> * getAllSqlTypeByClassName() { return (& QxClassX::getSingleton()->m_lstSqlTypeByClassName); } 00123 static QString getSqlTypeByClassName(const QString & sClassName) { return QxClassX::getAllSqlTypeByClassName()->value(sClassName); } 00124 static const std::type_info & getTypeInfo(const QString & sClassName) { return QxClassX::getSingleton()->typeInfo(sClassName); } 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(), 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 template <class U> 00140 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL) 00141 { 00142 typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1; 00143 typedef typename boost::mpl::if_c< 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(), boost::any * ret = NULL); 00148 static qx_bool invokeStatic(const QString & sClassKey, const QString & sFctKey, const type_any_params & params, boost::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(), boost::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, boost::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(), boost::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, boost::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(), boost::any * ret = NULL); 00171 static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const type_any_params & params, boost::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_