QxOrm  1.2.3
C++ Object Relational Mapping library
IxDataMember.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 _IX_DATA_MEMBER_H_
00027 #define _IX_DATA_MEMBER_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #ifdef _MSC_VER
00041 #pragma warning(push)
00042 #pragma warning(disable:4996)
00043 #endif // _MSC_VER
00044 
00045 #include <boost/any.hpp>
00046 
00047 #include <QxCommon/QxBool.h>
00048 #include <QxCommon/QxPropertyBag.h>
00049 
00050 #include <QxSerialize/boost/QxSerializeInclude.h>
00051 
00052 #include <QxDao/IxSqlRelation.h>
00053 
00054 #ifdef _MSC_VER
00055 #pragma warning(pop)
00056 #endif // _MSC_VER
00057 
00058 #define QX_CONSTRUCT_IX_DATA_MEMBER() \
00059 m_iPrecision(6), m_iMinLength(-1), m_iMaxLength(-1), m_bRequired(false), \
00060 m_bReadOnly(false), m_bAutoIncrement(false), m_bNotNull(false), \
00061 m_bIsPrimaryKey(false), m_bAccessDataPointer(false), m_pName(NULL), m_pParent(NULL)
00062 
00063 #define QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \
00064 virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const = 0; \
00065 virtual void fromArchive(void * pOwner, ArchiveInput & ar) = 0;
00066 
00067 namespace qx {
00068 
00069 class IxDataMemberX;
00070 
00075 class QX_DLL_EXPORT IxDataMember : public qx::QxPropertyBag
00076 {
00077 
00078 protected:
00079 
00080    QString     m_sKey;                 
00081    QString     m_sName;                
00082    QString     m_sNameParent;          
00083    QString     m_sDescription;         
00084    QString     m_sFormat;              
00085    QString     m_sSqlType;             
00086    QString     m_sSqlAlias;            
00087    long        m_lVersion;             
00088    bool        m_bSerialize;           
00089    bool        m_bDao;                 
00090    QVariant    m_vDefaultValue;        
00091    QVariant    m_vMinValue;            
00092    QVariant    m_vMaxValue;            
00093    int         m_iPrecision;           
00094    int         m_iMinLength;           
00095    int         m_iMaxLength;           
00096    bool        m_bRequired;            
00097    bool        m_bReadOnly;            
00098    bool        m_bAutoIncrement;       
00099    bool        m_bNotNull;             
00100    bool        m_bIsPrimaryKey;        
00101    bool        m_bAccessDataPointer;   
00102 
00103    QByteArray m_byteName;              
00104    const char * m_pName;               
00105    QStringList m_lstNames;             
00106 
00107    boost::scoped_ptr<IxSqlRelation> m_pSqlRelation;   
00108    IxDataMemberX * m_pParent;                         
00109 
00110 public:
00111 
00112    IxDataMember(const QString & sKey) : qx::QxPropertyBag(), m_sKey(sKey), m_lVersion(-1), m_bSerialize(true), m_bDao(true), QX_CONSTRUCT_IX_DATA_MEMBER() { qAssert(! m_sKey.isEmpty()); updateNamePtr(); }
00113    IxDataMember(const QString & sKey, long lVersion, bool bSerialize, bool bDao) : qx::QxPropertyBag(), m_sKey(sKey), m_lVersion(lVersion), m_bSerialize(bSerialize), m_bDao(bDao), QX_CONSTRUCT_IX_DATA_MEMBER() { qAssert(! m_sKey.isEmpty()); updateNamePtr(); }
00114    virtual ~IxDataMember() = 0;
00115 
00116    inline QString getKey() const                   { return m_sKey; }
00117    inline QString getName() const                  { return (m_sName.isEmpty() ? m_sKey : m_sName); }
00118    inline int getNameCount() const                 { return m_lstNames.count(); }
00119    inline QString getNameParent() const            { return m_sNameParent; }
00120    inline const char * getNamePtr() const          { return m_pName; }
00121    inline QString getDescription() const           { return m_sDescription; }
00122    inline QString getFormat() const                { return m_sFormat; }
00123    inline long getVersion() const                  { return m_lVersion; }
00124    inline bool getSerialize() const                { return m_bSerialize; }
00125    inline bool getDao() const                      { return m_bDao; }
00126    inline QVariant getDefaultValue() const         { return m_vDefaultValue; }
00127    inline QVariant getMinValue() const             { return m_vMinValue; }
00128    inline QVariant getMaxValue() const             { return m_vMaxValue; }
00129    inline int getPrecision() const                 { return m_iPrecision; }
00130    inline int getMinLength() const                 { return m_iMinLength; }
00131    inline int getMaxLength() const                 { return m_iMaxLength; }
00132    inline bool getRequired() const                 { return m_bRequired; }
00133    inline bool getReadOnly() const                 { return m_bReadOnly; }
00134    inline bool getAutoIncrement() const            { return m_bAutoIncrement; }
00135    inline bool getNotNull() const                  { return m_bNotNull; }
00136    inline bool getIsPrimaryKey() const             { return m_bIsPrimaryKey; }
00137    inline IxDataMemberX * getParent() const        { return m_pParent; }
00138    inline IxSqlRelation * getSqlRelation() const   { return m_pSqlRelation.get(); }
00139    inline bool hasSqlRelation() const              { return (m_pSqlRelation.get() != NULL); }
00140 
00141    inline void setName(const QString & sName)                  { m_sName = sName; updateNamePtr(); }
00142    inline void setNameParent(const QString & sName)            { m_sNameParent = sName; }
00143    inline void setDescription(const QString & sDesc)           { m_sDescription = sDesc; }
00144    inline void setFormat(const QString & sFormat)              { m_sFormat = sFormat; }
00145    inline void setSqlType(const QString & sSqlType)            { m_sSqlType = sSqlType; }
00146    inline void setSqlAlias(const QString & sSqlAlias)          { m_sSqlAlias = sSqlAlias; }
00147    inline void setVersion(long lVersion)                       { m_lVersion = lVersion; }
00148    inline void setSerialize(bool bSerialize)                   { m_bSerialize = bSerialize; }
00149    inline void setDao(bool bDao)                               { m_bDao = bDao; }
00150    inline void setDefaultValue(const QVariant & var)           { m_vDefaultValue = var; }
00151    inline void setPrecision(int iPrecision)                    { m_iPrecision = iPrecision; }
00152    inline void setRequired(bool bRequired)                     { m_bRequired = bRequired; }
00153    inline void setReadOnly(bool bReadOnly)                     { m_bReadOnly = bReadOnly; }
00154    inline void setAutoIncrement(bool bAutoIncrement)           { m_bAutoIncrement = bAutoIncrement; }
00155    inline void setIsPrimaryKey(bool bIsPrimaryKey)             { m_bIsPrimaryKey = bIsPrimaryKey; }
00156    inline void setParent(IxDataMemberX * pParent)              { m_pParent = pParent; }
00157    inline void setSqlRelation(IxSqlRelation * pSqlRelation)    { m_pSqlRelation.reset(pSqlRelation); }
00158 
00159    void setMinValue(long lMinValue, const QString & sMessage = QString());
00160    void setMinValue(double dMinValue, const QString & sMessage = QString());
00161    void setMaxValue(long lMaxValue, const QString & sMessage = QString());
00162    void setMaxValue(double dMaxValue, const QString & sMessage = QString());
00163    void setMinLength(int iMinLength, const QString & sMessage = QString());
00164    void setMaxLength(int iMaxLength, const QString & sMessage = QString());
00165    void setNotNull(bool bNotNull, const QString & sMessage = QString());
00166 
00167    QString getName(int iIndex, const QString & sOtherName = QString()) const;
00168    QString getSqlAlias(const QString & sTable = QString(), bool bClauseWhere = false, int iIndexName = 0) const;
00169    QString getSqlType(int iIndexName = -1) const;
00170    QString getSqlTypeAndParams(int iIndexName = -1) const;
00171    QString getSqlPlaceHolder(const QString & sAppend = QString(), int iIndexName = 0, const QString & sSep = QString(", "), const QString & sOtherName = QString()) const;
00172    void setSqlPlaceHolder(QSqlQuery & query, void * pOwner, const QString & sAppend = QString(), const QString & sOtherName = QString()) const;
00173    QString getSqlAliasEqualToPlaceHolder(const QString & sTable = QString(), bool bClauseWhere = false, const QString & sAppend = QString(), const QString & sSep = QString(" AND ")) const;
00174    QString getSqlNameEqualToPlaceHolder(const QString & sAppend = QString(), const QString & sSep = QString(" AND ")) const;
00175    QString getSqlTablePointNameAsAlias(const QString & sTable, const QString & sSep = QString(", ")) const;
00176    QString getSqlName(const QString & sSep = QString(", "), const QString & sOtherName = QString()) const;
00177    QString getSqlNameAndTypeAndParams(const QString & sSep = QString(", "), const QString & sOtherName = QString()) const;
00178 
00179    virtual bool isEqual(const void * pOwner1, const void * pOwner2) const = 0;
00180    virtual QString toString(const void * pOwner, const QString & sFormat, int iIndexName = -1) const = 0;
00181    virtual qx_bool fromString(void * pOwner, const QString & s, const QString & sFormat, int iIndexName = -1) = 0;
00182    virtual QVariant toVariant(const void * pOwner, const QString & sFormat, int iIndexName = -1) const = 0;
00183    virtual qx_bool fromVariant(void * pOwner, const QVariant & v, const QString & sFormat, int iIndexName = -1) = 0;
00184 
00185    inline QString toString(const void * pOwner, int iIndexName = -1) const             { return this->toString(pOwner, m_sFormat, iIndexName); }
00186    inline qx_bool fromString(void * pOwner, const QString & s, int iIndexName = -1)    { return this->fromString(pOwner, s, m_sFormat, iIndexName); }
00187    inline QVariant toVariant(const void * pOwner, int iIndexName = -1) const           { return this->toVariant(pOwner, m_sFormat, iIndexName); }
00188    inline qx_bool fromVariant(void * pOwner, const QVariant & v, int iIndexName = -1)  { return this->fromVariant(pOwner, v, m_sFormat, iIndexName); }
00189 
00190 protected:
00191 
00192    virtual boost::any getDataPtr(const void * pOwner) const = 0;
00193    virtual boost::any getDataPtr(void * pOwner) = 0;
00194    virtual void * getDataVoidPtr(const void * pOwner) const = 0;
00195    virtual void * getDataVoidPtr(void * pOwner) = 0;
00196 
00197 public:
00198 
00199    inline boost::any getValueAnyPtr(const void * pOwner) const    { return this->getDataPtr(pOwner); }
00200    inline boost::any getValueAnyPtr(void * pOwner)                { return this->getDataPtr(pOwner); }
00201    inline void * getValueVoidPtr(const void * pOwner) const       { return this->getDataVoidPtr(pOwner); }
00202    inline void * getValueVoidPtr(void * pOwner)                   { return this->getDataVoidPtr(pOwner); }
00203 
00204    template <typename T>
00205    T * getValuePtr(void * pOwner, bool * bOk = NULL)
00206    {
00207       if (bOk) { (* bOk) = false; }
00208       if (! m_bAccessDataPointer) { qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "cannot access data-member pointer"); return NULL; }
00209       boost::any a = this->getDataPtr(pOwner);
00210       try { T * t = boost::any_cast<T *>(a); if (bOk) { (* bOk) = (t != NULL); }; return t; }
00211       catch (const boost::bad_any_cast & err) { Q_UNUSED(err); qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "bad any cast exception"); return NULL; }
00212    }
00213 
00214    template <typename T>
00215    T getValue(void * pOwner, bool * bOk = NULL)
00216    {
00217       if (! m_bAccessDataPointer) { return qxCannotAccessDataPointer<T, 0>::getValue(this, pOwner, bOk); }
00218       T * t = this->getValuePtr<T>(pOwner, bOk);
00219       return (t ? (* t) : T());
00220    }
00221 
00222    template <typename T>
00223    bool setValue(void * pOwner, const T & val)
00224    {
00225       if (! m_bAccessDataPointer) { return qxCannotAccessDataPointer<T, 0>::setValue(this, pOwner, val); }
00226       T * t = this->getValuePtr<T>(pOwner);
00227       if (t) { (* t) = val; }
00228       return (t != NULL);
00229    }
00230 
00231 private:
00232 
00233    template <typename T, int dummy>
00234    struct qxCannotAccessDataPointer
00235    {
00236       static inline T getValue(IxDataMember * pData, void * pOwner, bool * bOk)
00237       { Q_UNUSED(pData); Q_UNUSED(pOwner); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::getValue() : '%s'", "type T not supported"); if (bOk) { (* bOk) = false; }; return T(); }
00238       static inline bool setValue(IxDataMember * pData, void * pOwner, const T & val)
00239       { Q_UNUSED(pData); Q_UNUSED(pOwner); Q_UNUSED(val); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::setValue() : '%s'", "type T not supported"); return false; }
00240    };
00241 
00242    template <int dummy>
00243    struct qxCannotAccessDataPointer<QVariant, dummy>
00244    {
00245       static inline QVariant getValue(IxDataMember * pData, void * pOwner, bool * bOk)
00246       { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toVariant(pOwner, "") : QVariant()); }
00247       static inline bool setValue(IxDataMember * pData, void * pOwner, const QVariant & val)
00248       { return (pData ? pData->fromVariant(pOwner, val, "").getValue() : false); }
00249    };
00250 
00251    template <int dummy>
00252    struct qxCannotAccessDataPointer<QString, dummy>
00253    {
00254       static inline QString getValue(IxDataMember * pData, void * pOwner, bool * bOk)
00255       { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toString(pOwner, "") : QString()); }
00256       static inline bool setValue(IxDataMember * pData, void * pOwner, const QString & val)
00257       { return (pData ? pData->fromString(pOwner, val, "").getValue() : false); }
00258    };
00259 
00260 public:
00261 
00262 #if _QX_SERIALIZE_POLYMORPHIC
00263    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
00264 #endif // _QX_SERIALIZE_POLYMORPHIC
00265 
00266 #if _QX_SERIALIZE_BINARY
00267    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
00268 #endif // _QX_SERIALIZE_BINARY
00269 
00270 #if _QX_SERIALIZE_TEXT
00271    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
00272 #endif // _QX_SERIALIZE_TEXT
00273 
00274 #if _QX_SERIALIZE_XML
00275    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
00276 #endif // _QX_SERIALIZE_XML
00277 
00278 #if _QX_SERIALIZE_PORTABLE_BINARY
00279    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive)
00280 #endif // _QX_SERIALIZE_PORTABLE_BINARY
00281 
00282 #if _QX_SERIALIZE_WIDE_BINARY
00283    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
00284 #endif // _QX_SERIALIZE_WIDE_BINARY
00285 
00286 #if _QX_SERIALIZE_WIDE_TEXT
00287    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
00288 #endif // _QX_SERIALIZE_WIDE_TEXT
00289 
00290 #if _QX_SERIALIZE_WIDE_XML
00291    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
00292 #endif // _QX_SERIALIZE_WIDE_XML
00293 
00294 private:
00295 
00296    inline void updateNamePtr()
00297    {
00298       QString sNamePtr = (m_sName.isEmpty() ? m_sKey : m_sName);
00299       sNamePtr.replace("|", "-"); // valid xml tag
00300       m_byteName = sNamePtr.toAscii();
00301       m_pName = m_byteName.constData();
00302       m_lstNames = (m_sName.isEmpty() ? m_sKey.split("|") : m_sName.split("|"));
00303    }
00304 
00305    template <class Archive>
00306    void serialize(Archive & ar, const unsigned int version);
00307 
00308 };
00309 
00310 typedef boost::shared_ptr<IxDataMember> IxDataMember_ptr;
00311 
00312 } // namespace qx
00313 
00314 QX_DLL_EXPORT_INLINE_FCT inline bool operator< (const qx::IxDataMember & i1, const qx::IxDataMember & i2);
00315 QX_DLL_EXPORT_INLINE_FCT inline bool operator> (const qx::IxDataMember & i1, const qx::IxDataMember & i2);
00316 
00317 #endif // _IX_DATA_MEMBER_H_