QxOrm  1.2.3
C++ Object Relational Mapping library
QxValidator.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_VALIDATOR_H_
00027 #define _QX_VALIDATOR_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/function.hpp>
00041 
00042 #include <QxValidator/IxValidator.h>
00043 #include <QxValidator/QxInvalidValueX.h>
00044 
00045 #include <QxDataMember/IxDataMember.h>
00046 
00047 namespace qx {
00048 
00049 template <class T>
00050 QxInvalidValueX validate(T & t, const QString & group);
00051 
00059 template <class Owner>
00060 class QxValidator : public IxValidator
00061 {
00062 
00063 public:
00064 
00065    typedef boost::function<void (Owner *, QxInvalidValueX &)> type_fct_custom_validator_member;
00066    typedef boost::function<void (const QVariant &, QxInvalidValueX &)> type_fct_custom_validator_variant;
00067    typedef boost::function<void (const QVariant &, const IxValidator *, QxInvalidValueX &)> type_fct_custom_validator_variant_validator;
00068 
00069 protected:
00070 
00071    type_fct_custom_validator_member             m_fctCustomValidator_Member;              
00072    type_fct_custom_validator_variant            m_fctCustomValidator_Variant;             
00073    type_fct_custom_validator_variant_validator  m_fctCustomValidator_VariantValidator;    
00074 
00075 public:
00076 
00077    QxValidator() : IxValidator(IxValidator::custom_validator) { ; }
00078    virtual ~QxValidator() { ; }
00079 
00080    void setFunction(type_fct_custom_validator_member fct)               { m_fctCustomValidator_Member = fct; }
00081    void setFunction(type_fct_custom_validator_variant fct)              { m_fctCustomValidator_Variant = fct; }
00082    void setFunction(type_fct_custom_validator_variant_validator fct)    { m_fctCustomValidator_VariantValidator = fct; }
00083 
00084    virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
00085    {
00086       if (! m_fctCustomValidator_Member.empty())
00087       { m_fctCustomValidator_Member(static_cast<Owner *>(pOwner), lstInvalidValues); }
00088       else if (! m_fctCustomValidator_Variant.empty() && m_pDataMember)
00089       { m_fctCustomValidator_Variant(m_pDataMember->toVariant(pOwner), lstInvalidValues); }
00090       else if (! m_fctCustomValidator_VariantValidator.empty() && m_pDataMember)
00091       { m_fctCustomValidator_VariantValidator(m_pDataMember->toVariant(pOwner), this, lstInvalidValues); }
00092    }
00093 
00094 };
00095 
00103 template <typename DataType, class Owner>
00104 class QxValidator_WithDataType : public IxValidator
00105 {
00106 
00107 public:
00108 
00109    typedef boost::function<void (const DataType &, QxInvalidValueX &)> type_fct_custom_validator_data_type;
00110    typedef boost::function<void (const DataType &, const IxValidator *, QxInvalidValueX &)> type_fct_custom_validator_data_type_validator;
00111 
00112 protected:
00113 
00114    type_fct_custom_validator_data_type             m_fctCustomValidator_DataType;            
00115    type_fct_custom_validator_data_type_validator   m_fctCustomValidator_DataTypeValidator;   
00116 
00117 public:
00118 
00119    QxValidator_WithDataType() : IxValidator(IxValidator::custom_validator) { ; }
00120    virtual ~QxValidator_WithDataType() { ; }
00121 
00122    void setFunction(type_fct_custom_validator_data_type fct)            { m_fctCustomValidator_DataType = fct; }
00123    void setFunction(type_fct_custom_validator_data_type_validator fct)  { m_fctCustomValidator_DataTypeValidator = fct; }
00124 
00125    virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
00126    {
00127       if (! m_pDataMember) { return; }
00128       IxDataMember * pDataMember = const_cast<IxDataMember *>(m_pDataMember);
00129       DataType * val = pDataMember->getValuePtr<DataType>(pOwner);
00130       if (! m_fctCustomValidator_DataType.empty() && val)
00131       { m_fctCustomValidator_DataType((* val), lstInvalidValues); }
00132       else if (! m_fctCustomValidator_DataTypeValidator.empty() && val)
00133       { m_fctCustomValidator_DataTypeValidator((* val), this, lstInvalidValues); }
00134    }
00135 
00136 };
00137 
00145 template <typename DataType, class Owner>
00146 class QxValidator_Recursive : public IxValidator
00147 {
00148 
00149 public:
00150 
00151    QxValidator_Recursive() : IxValidator(IxValidator::recursive_validator) { ; }
00152    virtual ~QxValidator_Recursive() { ; }
00153 
00154    virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
00155    {
00156       if (! m_pDataMember) { qAssert(false); return; }
00157       IxDataMember * pDataMember = const_cast<IxDataMember *>(m_pDataMember);
00158       DataType * val = pDataMember->getValuePtr<DataType>(pOwner);
00159       if (! val) { qAssert(false); return; }
00160       QxInvalidValueX invalidValues;
00161       invalidValues.setCurrentPath(m_pDataMember->getName());
00162       invalidValues.insert(qx::validate((* val), m_group));
00163       lstInvalidValues.insert(invalidValues);
00164    }
00165 
00166 };
00167 
00168 } // namespace qx
00169 
00170 #endif // _QX_VALIDATOR_H_