![]() |
QxOrm 1.2.1
C++ Object Relational Mapping library
|
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 type_fct_save_qvariant_usertype m_fctSaveQVariantUserType; 00080 type_fct_load_qvariant_usertype m_fctLoadQVariantUserType; 00081 00082 private: 00083 00084 QxClassX() : QxSingleton<QxClassX>("qx::QxClassX") { this->initSqlTypeByClassName(); } 00085 virtual ~QxClassX() { ; } 00086 00087 QxCollection<QString, IxClass *> * getAll(); 00088 IxClass * get(const QString & sKey) const; 00089 bool exist(const QString & sKey) const; 00090 bool insert(const QString & sKey, IxClass * pClass); 00091 bool remove(const QString & sKey); 00092 void clear(); 00093 void initSqlTypeByClassName(); 00094 00095 public: 00096 00097 static boost::any create(const QString & sKey); 00098 static IxClass * getClass(const QString & sKey); 00099 static IxDataMemberX * getDataMemberX(const QString & sKey); 00100 static IxFunctionX * getFctMemberX(const QString & sKey); 00101 static IxDataMember * getDataMember(const QString & sClassKey, const QString & sDataKey, bool bRecursive = true); 00102 static IxFunction * getFctMember(const QString & sClassKey, const QString & sFctKey, bool bRecursive = true); 00103 00104 static QxCollection<QString, IxClass *> * getAllClasses(); 00105 static void registerAllClasses(); 00106 static QString dumpAllClasses(); 00107 static QString dumpSqlSchema(); 00108 00109 static QHash<QString, QString> * getAllSqlTypeByClassName() { return (& QxClassX::getSingleton()->m_lstSqlTypeByClassName); } 00110 static QString getSqlTypeByClassName(const QString & sClassName) { return (QxClassX::getAllSqlTypeByClassName()->value(sClassName)); } 00111 00112 static type_fct_save_qvariant_usertype getFctSaveQVariantUserType() { return QxClassX::getSingleton()->m_fctSaveQVariantUserType; } 00113 static type_fct_load_qvariant_usertype getFctLoadQVariantUserType() { return QxClassX::getSingleton()->m_fctLoadQVariantUserType; } 00114 static void setFctSaveQVariantUserType(type_fct_save_qvariant_usertype fct) { QxClassX::getSingleton()->m_fctSaveQVariantUserType = fct; } 00115 static void setFctLoadQVariantUserType(type_fct_load_qvariant_usertype fct) { QxClassX::getSingleton()->m_fctLoadQVariantUserType = fct; } 00116 00117 template <class U> 00118 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL) 00119 { 00120 typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1; 00121 typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2; 00122 return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret); 00123 } 00124 00125 template <class U> 00126 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL) 00127 { 00128 typedef typename boost::mpl::if_c< boost::is_pointer<U>::value, QxClassX::invoke_ptr<U>, QxClassX::invoke_default<U> >::type type_invoke_1; 00129 typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<U>::value, QxClassX::invoke_ptr<U>, type_invoke_1 >::type type_invoke_2; 00130 return type_invoke_2::invoke(sClassKey, sFctKey, pOwner, params, ret); 00131 } 00132 00133 private: 00134 00135 template <class U> 00136 struct invoke_ptr 00137 { 00138 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL) 00139 { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); } 00140 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL) 00141 { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& (* pOwner)), params, ret); } 00142 }; 00143 00144 template <class U> 00145 struct invoke_default 00146 { 00147 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const QString & params = QString(), boost::any * ret = NULL) 00148 { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); } 00149 static inline qx_bool invoke(const QString & sClassKey, const QString & sFctKey, U & pOwner, const type_any_params & params, boost::any * ret = NULL) 00150 { return QxClassX::invokeVoidPtr(sClassKey, sFctKey, static_cast<void *>(& pOwner), params, ret); } 00151 }; 00152 00153 static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const QString & params = QString(), boost::any * ret = NULL); 00154 static qx_bool invokeVoidPtr(const QString & sClassKey, const QString & sFctKey, void * pOwner, const type_any_params & params, boost::any * ret = NULL); 00155 00156 static bool isValid_DataMember(IxDataMember * p); 00157 static bool isValid_SqlRelation(IxDataMember * p); 00158 00159 }; 00160 00161 } // namespace qx 00162 00163 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxClassX) 00164 00165 #endif // _QX_CLASS_X_H_