QxOrm  1.2.3
C++ Object Relational Mapping library
QxClass.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_H_
00027 #define _QX_CLASS_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/static_assert.hpp>
00041 #include <boost/type_traits/is_same.hpp>
00042 #include <boost/type_traits/is_base_of.hpp>
00043 #include <boost/serialization/version.hpp>
00044 
00045 #include <QxRegister/IxClass.h>
00046 
00047 #include <QxDataMember/QxDataMemberX.h>
00048 
00049 #include <QxFunction/QxFunctionInclude.h>
00050 
00051 #include <QxSingleton/QxSingleton.h>
00052 
00053 #include <QxTraits/get_class_name.h>
00054 #include <QxTraits/get_base_class.h>
00055 #include <QxTraits/get_primary_key.h>
00056 
00057 #include <QxValidator/QxValidatorX.h>
00058 
00059 namespace qx {
00060 
00065 template <class T>
00066 void register_class(T & t) { Q_UNUSED(t); qAssert(false); };
00067 
00072 template <class T>
00073 class QxClass : public IxClass, public QxSingleton< QxClass<T> >
00074 {
00075 
00076    friend class QxSingleton< QxClass<T> >;
00077 
00078 public:
00079 
00080    typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
00081    typedef typename qx::trait::get_base_class<T>::type type_base_class;
00082    typedef IxFunction::type_any_params type_any_params;
00083 
00084    enum { is_valid_base_class = ((! boost::is_same<type_base_class, T>::value) && (boost::is_base_of<type_base_class, T>::value || boost::is_same<type_base_class, qx::trait::no_base_class_defined>::value)) };
00085 
00086 protected:
00087 
00088    QMutex m_oMutexClass;   
00089 
00090 protected:
00091 
00092    QxClass() : IxClass(), QxSingleton< QxClass<T> >(QString("qx::QxClass_") + qx::trait::get_class_name<T>::get_xml_tag()) { init(); }
00093    virtual ~QxClass() { ; }
00094 
00095 public:
00096 
00097    inline QxDataMemberX<T> * dataMemberX() const   { return static_cast<QxDataMemberX<T> *>(m_pDataMemberX); }
00098    inline IxFunctionX * fctMemberX() const         { return m_pFctMemberX.get(); }
00099 
00100    IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey);
00101    IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey, long lVersion);
00102    IxDataMember * id(const QString & sKey, long lVersion);
00103    IxDataMember * data(const QString & sKey, long lVersion);
00104 
00105    template <typename V, typename U> IxDataMember * data(V U::* pData, const QString & sKey);
00106    template <typename V, typename U> IxDataMember * data(V U::* pData, const QString & sKey, long lVersion);
00107    template <typename V, typename U> IxDataMember * data(V U::* pData, const QString & sKey, long lVersion, bool bSerialize);
00108    template <typename V, typename U> IxDataMember * data(V U::* pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao);
00109 
00110    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey);
00111    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey, long lVersion);
00112    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey);
00113    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey, long lVersion);
00114    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey);
00115    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey, long lVersion);
00116    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType);
00117    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType, long lVersion);
00118 
00119    template <typename R> IxFunction * fct_0(const typename QxFunction_0<T, R>::type_fct & fct, const QString & sKey);
00120    template <typename R, typename P1> IxFunction * fct_1(const typename QxFunction_1<T, R, P1>::type_fct & fct, const QString & sKey);
00121    template <typename R, typename P1, typename P2> IxFunction * fct_2(const typename QxFunction_2<T, R, P1, P2>::type_fct & fct, const QString & sKey);
00122    template <typename R, typename P1, typename P2, typename P3> IxFunction * fct_3(const typename QxFunction_3<T, R, P1, P2, P3>::type_fct & fct, const QString & sKey);
00123    template <typename R, typename P1, typename P2, typename P3, typename P4> IxFunction * fct_4(const typename QxFunction_4<T, R, P1, P2, P3, P4>::type_fct & fct, const QString & sKey);
00124    template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5> IxFunction * fct_5(const typename QxFunction_5<T, R, P1, P2, P3, P4, P5>::type_fct & fct, const QString & sKey);
00125    template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6> IxFunction * fct_6(const typename QxFunction_6<T, R, P1, P2, P3, P4, P5, P6>::type_fct & fct, const QString & sKey);
00126    template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7> IxFunction * fct_7(const typename QxFunction_7<T, R, P1, P2, P3, P4, P5, P6, P7>::type_fct & fct, const QString & sKey);
00127    template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8> IxFunction * fct_8(const typename QxFunction_8<T, R, P1, P2, P3, P4, P5, P6, P7, P8>::type_fct & fct, const QString & sKey);
00128    template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9> IxFunction * fct_9(const typename QxFunction_9<T, R, P1, P2, P3, P4, P5, P6, P7, P8, P9>::type_fct & fct, const QString & sKey);
00129 
00130    static qx_bool invoke(const QString & sKey, T * pOwner, const QString & params = QString(), boost::any * ret = NULL) { return QxClass<T>::getSingleton()->invokeHelper(sKey, pOwner, params, ret); }
00131    static qx_bool invoke(const QString & sKey, T * pOwner, const type_any_params & params, boost::any * ret = NULL)     { return QxClass<T>::getSingleton()->invokeHelper(sKey, pOwner, params, ret); }
00132 
00133    virtual const std::type_info & typeInfo() const
00134    { return typeid(T); }
00135 
00136    virtual IxClass * getBaseClass() const
00137    { return (boost::is_same<type_base_class, qx::trait::no_base_class_defined>::value ? NULL : QxClass<type_base_class>::getSingleton()); }
00138 
00139 #if _QX_SUPPORT_COVARIANT_RETURN_TYPE
00140    virtual QxValidatorX<T> * getAllValidator()
00141    {
00142       if (! m_pAllValidator) { m_pAllValidator.reset(new QxValidatorX<T>()); IxClass::getAllValidator(); }
00143       return static_cast<QxValidatorX<T> *>(m_pAllValidator.get());
00144    }
00145 #else // _QX_SUPPORT_COVARIANT_RETURN_TYPE
00146    virtual IxValidatorX * getAllValidator()
00147    {
00148       if (! m_pAllValidator) { m_pAllValidator.reset(new QxValidatorX<T>()); IxClass::getAllValidator(); }
00149       return m_pAllValidator.get();
00150    }
00151 #endif // _QX_SUPPORT_COVARIANT_RETURN_TYPE
00152 
00153 private:
00154 
00155    void init();
00156    IxFunction * insertFct(IxFunction_ptr pFct, const QString & sKey);
00157 
00158    void registerClass() { qx::register_class(* this); }
00159 
00160    void beforeRegisterClass()
00161    {
00162       BOOST_STATIC_ASSERT(is_valid_base_class);
00163       QMutexLocker locker(& m_oMutexClass);
00164       QxClass<type_base_class>::getSingleton();
00165       bool bNeedReg = (m_pDataMemberX && (m_pDataMemberX->count() <= 0));
00166       bNeedReg = (bNeedReg && m_pFctMemberX && (m_pFctMemberX->count() <= 0));
00167       if (bNeedReg) { registerClass(); }
00168    }
00169 
00170    qx_bool invokeHelper(const QString & sKey, T * pOwner, const QString & params, boost::any * ret)
00171    { return ((pOwner && m_pFctMemberX && m_pFctMemberX->exist(sKey)) ? m_pFctMemberX->getByKey(sKey)->invoke(pOwner, params, ret) : QxClass<type_base_class>::invoke(sKey, dynamic_cast<type_base_class *>(pOwner), params, ret)); }
00172 
00173    qx_bool invokeHelper(const QString & sKey, T * pOwner, const type_any_params & params, boost::any * ret)
00174    { return ((pOwner && m_pFctMemberX && m_pFctMemberX->exist(sKey)) ? m_pFctMemberX->getByKey(sKey)->invoke(pOwner, params, ret) : QxClass<type_base_class>::invoke(sKey, dynamic_cast<type_base_class *>(pOwner), params, ret)); }
00175 
00176 };
00177 
00178 } // namespace qx
00179 
00180 #include "../../inl/QxRegister/QxClass.inl"
00181 
00182 #endif // _QX_CLASS_H_