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