QxOrm  1.2.9
C++ Object Relational Mapping library
QxValidator.h
Go to the documentation of this file.
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_VALIDATOR_H_
00033 #define _QX_VALIDATOR_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <boost/function.hpp>
00047 
00048 #include <QxValidator/IxValidator.h>
00049 #include <QxValidator/QxInvalidValueX.h>
00050 
00051 #include <QxDataMember/IxDataMember.h>
00052 
00053 namespace qx {
00054 
00055 template <class T>
00056 QxInvalidValueX validate(T & t, const QString & group);
00057 
00065 template <class Owner>
00066 class QxValidator : public IxValidator
00067 {
00068 
00069 public:
00070 
00071    typedef boost::function<void (Owner *, QxInvalidValueX &)> type_fct_custom_validator_member;
00072    typedef boost::function<void (const QVariant &, QxInvalidValueX &)> type_fct_custom_validator_variant;
00073    typedef boost::function<void (const QVariant &, const IxValidator *, QxInvalidValueX &)> type_fct_custom_validator_variant_validator;
00074 
00075 protected:
00076 
00077    type_fct_custom_validator_member             m_fctCustomValidator_Member;              
00078    type_fct_custom_validator_variant            m_fctCustomValidator_Variant;             
00079    type_fct_custom_validator_variant_validator  m_fctCustomValidator_VariantValidator;    
00080 
00081 public:
00082 
00083    QxValidator() : IxValidator(IxValidator::custom_validator) { ; }
00084    virtual ~QxValidator() { ; }
00085 
00086    void setFunction(type_fct_custom_validator_member fct)               { m_fctCustomValidator_Member = fct; }
00087    void setFunction(type_fct_custom_validator_variant fct)              { m_fctCustomValidator_Variant = fct; }
00088    void setFunction(type_fct_custom_validator_variant_validator fct)    { m_fctCustomValidator_VariantValidator = fct; }
00089 
00090    virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
00091    {
00092       if (! m_fctCustomValidator_Member.empty())
00093       { m_fctCustomValidator_Member(static_cast<Owner *>(pOwner), lstInvalidValues); }
00094       else if (! m_fctCustomValidator_Variant.empty() && m_pDataMember)
00095       { m_fctCustomValidator_Variant(m_pDataMember->toVariant(pOwner), lstInvalidValues); }
00096       else if (! m_fctCustomValidator_VariantValidator.empty() && m_pDataMember)
00097       { m_fctCustomValidator_VariantValidator(m_pDataMember->toVariant(pOwner), this, lstInvalidValues); }
00098    }
00099 
00100 };
00101 
00109 template <typename DataType, class Owner>
00110 class QxValidator_WithDataType : public IxValidator
00111 {
00112 
00113 public:
00114 
00115    typedef boost::function<void (const DataType &, QxInvalidValueX &)> type_fct_custom_validator_data_type;
00116    typedef boost::function<void (const DataType &, const IxValidator *, QxInvalidValueX &)> type_fct_custom_validator_data_type_validator;
00117 
00118 protected:
00119 
00120    type_fct_custom_validator_data_type             m_fctCustomValidator_DataType;            
00121    type_fct_custom_validator_data_type_validator   m_fctCustomValidator_DataTypeValidator;   
00122 
00123 public:
00124 
00125    QxValidator_WithDataType() : IxValidator(IxValidator::custom_validator) { ; }
00126    virtual ~QxValidator_WithDataType() { ; }
00127 
00128    void setFunction(type_fct_custom_validator_data_type fct)            { m_fctCustomValidator_DataType = fct; }
00129    void setFunction(type_fct_custom_validator_data_type_validator fct)  { m_fctCustomValidator_DataTypeValidator = fct; }
00130 
00131    virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
00132    {
00133       if (! m_pDataMember) { return; }
00134       IxDataMember * pDataMember = const_cast<IxDataMember *>(m_pDataMember);
00135       DataType * val = pDataMember->getValuePtr<DataType>(pOwner);
00136       if (! m_fctCustomValidator_DataType.empty() && val)
00137       { m_fctCustomValidator_DataType((* val), lstInvalidValues); }
00138       else if (! m_fctCustomValidator_DataTypeValidator.empty() && val)
00139       { m_fctCustomValidator_DataTypeValidator((* val), this, lstInvalidValues); }
00140    }
00141 
00142 };
00143 
00151 template <typename DataType, class Owner>
00152 class QxValidator_Recursive : public IxValidator
00153 {
00154 
00155 public:
00156 
00157    QxValidator_Recursive() : IxValidator(IxValidator::recursive_validator) { ; }
00158    virtual ~QxValidator_Recursive() { ; }
00159 
00160    virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
00161    {
00162       if (! m_pDataMember) { qAssert(false); return; }
00163       IxDataMember * pDataMember = const_cast<IxDataMember *>(m_pDataMember);
00164       DataType * val = pDataMember->getValuePtr<DataType>(pOwner);
00165       if (! val) { qAssert(false); return; }
00166       QxInvalidValueX invalidValues;
00167       invalidValues.setCurrentPath(m_pDataMember->getName());
00168       invalidValues.insert(qx::validate((* val), m_group));
00169       lstInvalidValues.insert(invalidValues);
00170    }
00171 
00172 };
00173 
00174 } // namespace qx
00175 
00176 #endif // _QX_VALIDATOR_H_