QxValidator.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_VALIDATOR_H_
  32. #define _QX_VALIDATOR_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxValidator.h
  38. * \author Lionel Marty
  39. * \ingroup QxValidator
  40. * \brief Concrete class for a custom or recursive validator
  41. */
  42. #include <QxValidator/IxValidator.h>
  43. #include <QxValidator/QxInvalidValueX.h>
  44. #include <QxDataMember/IxDataMember.h>
  45. namespace qx {
  46. template <class T>
  47. QxInvalidValueX validate(T & t, const QString & group);
  48. /*!
  49. * \ingroup QxValidator
  50. * \brief qx::QxValidator<Owner> : concrete class for a custom validator
  51. *
  52. * For more informations about <b>QxValidator module</b>, <a href="https://www.qxorm.com/qxorm_en/faq.html#faq_250" target="_blank">goto the FAQ of QxOrm website</a> :
  53. * <a href="https://www.qxorm.com/qxorm_en/faq.html#faq_250" target="_blank">https://www.qxorm.com/qxorm_en/faq.html#faq_250</a>
  54. */
  55. template <class Owner>
  56. class QxValidator : public IxValidator
  57. {
  58. public:
  59. typedef std::function<void (Owner *, QxInvalidValueX &)> type_fct_custom_validator_member;
  60. typedef std::function<void (const QVariant &, QxInvalidValueX &)> type_fct_custom_validator_variant;
  61. typedef std::function<void (const QVariant &, const IxValidator *, QxInvalidValueX &)> type_fct_custom_validator_variant_validator;
  62. protected:
  63. type_fct_custom_validator_member m_fctCustomValidator_Member; //!< Custom validator function : class method
  64. type_fct_custom_validator_variant m_fctCustomValidator_Variant; //!< Custom validator function : global function with value converted to QVariant type
  65. type_fct_custom_validator_variant_validator m_fctCustomValidator_VariantValidator; //!< Custom validator function : global function with value converted to QVariant type and a IxValidator pointer containing all parameters
  66. public:
  67. QxValidator() : IxValidator(IxValidator::custom_validator) { ; }
  68. virtual ~QxValidator() { ; }
  69. void setFunction(type_fct_custom_validator_member fct) { m_fctCustomValidator_Member = fct; }
  70. void setFunction(type_fct_custom_validator_variant fct) { m_fctCustomValidator_Variant = fct; }
  71. void setFunction(type_fct_custom_validator_variant_validator fct) { m_fctCustomValidator_VariantValidator = fct; }
  72. virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
  73. {
  74. if (m_fctCustomValidator_Member)
  75. { m_fctCustomValidator_Member(static_cast<Owner *>(pOwner), lstInvalidValues); }
  76. else if (m_fctCustomValidator_Variant && m_pDataMember)
  77. { m_fctCustomValidator_Variant(m_pDataMember->toVariant(pOwner), lstInvalidValues); }
  78. else if (m_fctCustomValidator_VariantValidator && m_pDataMember)
  79. { m_fctCustomValidator_VariantValidator(m_pDataMember->toVariant(pOwner), this, lstInvalidValues); }
  80. }
  81. };
  82. /*!
  83. * \ingroup QxValidator
  84. * \brief qx::QxValidator_WithDataType<DataType, Owner> : concrete class for a custom validator with data type
  85. *
  86. * For more informations about <b>QxValidator module</b>, <a href="https://www.qxorm.com/qxorm_en/faq.html#faq_250" target="_blank">goto the FAQ of QxOrm website</a> :
  87. * <a href="https://www.qxorm.com/qxorm_en/faq.html#faq_250" target="_blank">https://www.qxorm.com/qxorm_en/faq.html#faq_250</a>
  88. */
  89. template <typename DataType, class Owner>
  90. class QxValidator_WithDataType : public IxValidator
  91. {
  92. public:
  93. typedef std::function<void (const DataType &, QxInvalidValueX &)> type_fct_custom_validator_data_type;
  94. typedef std::function<void (const DataType &, const IxValidator *, QxInvalidValueX &)> type_fct_custom_validator_data_type_validator;
  95. protected:
  96. type_fct_custom_validator_data_type m_fctCustomValidator_DataType; //!< Custom validator function : global function with value
  97. type_fct_custom_validator_data_type_validator m_fctCustomValidator_DataTypeValidator; //!< Custom validator function : global function with value and a IxValidator pointer containing all parameters
  98. public:
  99. QxValidator_WithDataType() : IxValidator(IxValidator::custom_validator) { ; }
  100. virtual ~QxValidator_WithDataType() { ; }
  101. void setFunction(type_fct_custom_validator_data_type fct) { m_fctCustomValidator_DataType = fct; }
  102. void setFunction(type_fct_custom_validator_data_type_validator fct) { m_fctCustomValidator_DataTypeValidator = fct; }
  103. virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
  104. {
  105. if (! m_pDataMember) { return; }
  106. IxDataMember * pDataMember = const_cast<IxDataMember *>(m_pDataMember);
  107. DataType * val = pDataMember->getValuePtr<DataType>(pOwner);
  108. if (m_fctCustomValidator_DataType && val)
  109. { m_fctCustomValidator_DataType((* val), lstInvalidValues); }
  110. else if (m_fctCustomValidator_DataTypeValidator && val)
  111. { m_fctCustomValidator_DataTypeValidator((* val), this, lstInvalidValues); }
  112. }
  113. };
  114. /*!
  115. * \ingroup QxValidator
  116. * \brief qx::QxValidator_Recursive<DataType, Owner> : concrete class for a recursive validator
  117. *
  118. * For more informations about <b>QxValidator module</b>, <a href="https://www.qxorm.com/qxorm_en/faq.html#faq_250" target="_blank">goto the FAQ of QxOrm website</a> :
  119. * <a href="https://www.qxorm.com/qxorm_en/faq.html#faq_250" target="_blank">https://www.qxorm.com/qxorm_en/faq.html#faq_250</a>
  120. */
  121. template <typename DataType, class Owner>
  122. class QxValidator_Recursive : public IxValidator
  123. {
  124. public:
  125. QxValidator_Recursive() : IxValidator(IxValidator::recursive_validator) { ; }
  126. virtual ~QxValidator_Recursive() { ; }
  127. virtual void validate(void * pOwner, QxInvalidValueX & lstInvalidValues) const
  128. {
  129. if (! m_pDataMember) { qAssert(false); return; }
  130. IxDataMember * pDataMember = const_cast<IxDataMember *>(m_pDataMember);
  131. DataType * val = pDataMember->getValuePtr<DataType>(pOwner);
  132. if (! val) { qAssert(false); return; }
  133. QxInvalidValueX invalidValues;
  134. invalidValues.setCurrentPath(m_pDataMember->getName());
  135. invalidValues.insert(qx::validate((* val), m_group));
  136. lstInvalidValues.insert(invalidValues);
  137. }
  138. };
  139. } // namespace qx
  140. #endif // _QX_VALIDATOR_H_