QxValidatorFct.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_FUNCTION_H_
  32. #define _QX_VALIDATOR_FUNCTION_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxValidatorFct.h
  38. * \author Lionel Marty
  39. * \ingroup QxValidator
  40. * \brief Implementation of qx::validate<T>() function (validator engine)
  41. */
  42. #include <QxValidator/IxValidatorX.h>
  43. #include <QxValidator/QxInvalidValueX.h>
  44. #include <QxRegister/QxClass.h>
  45. #include <QxTraits/is_container.h>
  46. #include <QxTraits/is_smart_ptr.h>
  47. #include <QxTraits/is_qx_registered.h>
  48. namespace qx {
  49. template <class T>
  50. QxInvalidValueX validate(T & t, const QString & group);
  51. } // namespace qx
  52. namespace qx {
  53. namespace validator {
  54. namespace detail {
  55. template <class T>
  56. struct QxValidator_Helper_Generic
  57. {
  58. static inline qx::QxInvalidValueX validate(T & t, const QString & group)
  59. {
  60. static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
  61. qx::QxInvalidValueX invalidValues;
  62. qx::IxClass * pClass = qx::QxClass<T>::getSingleton();
  63. if (! pClass) { qAssert(false); return invalidValues; }
  64. qx::IxValidatorX * pAllValidator = pClass->getAllValidator();
  65. if (! pAllValidator) { return invalidValues; }
  66. invalidValues.setCurrentPath(pClass->getName());
  67. invalidValues.insert(pAllValidator->validate((& t), group));
  68. return invalidValues;
  69. }
  70. };
  71. template <class T>
  72. struct QxValidator_Helper_Container
  73. {
  74. static inline qx::QxInvalidValueX validate(T & t, const QString & group)
  75. {
  76. qx::QxInvalidValueX invalidValues; long lIndex = 0;
  77. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  78. {
  79. invalidValues.setCurrentPath("[" + QString::number(lIndex) + "]");
  80. invalidValues.insert(validateItem((* it), group));
  81. lIndex++;
  82. }
  83. return invalidValues;
  84. }
  85. private:
  86. template <typename U>
  87. static inline qx::QxInvalidValueX validateItem(U & item, const QString & group)
  88. { return validateItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::validate(item, group); }
  89. template <typename U, bool bIsPointer /* = true */>
  90. struct validateItem_Helper
  91. {
  92. static inline qx::QxInvalidValueX validate(U & item, const QString & group)
  93. { return (item ? qx::validator::detail::QxValidator_Helper_Container<T>::validateItem((* item), group) : qx::QxInvalidValueX()); }
  94. };
  95. template <typename U1, typename U2>
  96. struct validateItem_Helper<std::pair<U1, U2>, false>
  97. {
  98. static inline qx::QxInvalidValueX validate(std::pair<U1, U2> & item, const QString & group)
  99. { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
  100. };
  101. template <typename U1, typename U2>
  102. struct validateItem_Helper<const std::pair<U1, U2>, false>
  103. {
  104. static inline qx::QxInvalidValueX validate(const std::pair<U1, U2> & item, const QString & group)
  105. { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
  106. };
  107. template <typename U1, typename U2>
  108. struct validateItem_Helper<QPair<U1, U2>, false>
  109. {
  110. static inline qx::QxInvalidValueX validate(QPair<U1, U2> & item, const QString & group)
  111. { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
  112. };
  113. template <typename U1, typename U2>
  114. struct validateItem_Helper<const QPair<U1, U2>, false>
  115. {
  116. static inline qx::QxInvalidValueX validate(const QPair<U1, U2> & item, const QString & group)
  117. { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
  118. };
  119. template <typename U>
  120. struct validateItem_Helper<U, false>
  121. { static qx::QxInvalidValueX validate(U & item, const QString & group) { return qx::validate(item, group); } };
  122. };
  123. template <class T>
  124. struct QxValidator_Helper_Ptr
  125. {
  126. static inline qx::QxInvalidValueX validate(T & t, const QString & group)
  127. { return (t ? qx::validate((* t), group) : qx::QxInvalidValueX()); }
  128. };
  129. template <class T>
  130. struct QxValidator_Helper
  131. {
  132. static inline qx::QxInvalidValueX validate(T & t, const QString & group)
  133. {
  134. typedef typename std::conditional< std::is_pointer<T>::value, qx::validator::detail::QxValidator_Helper_Ptr<T>, qx::validator::detail::QxValidator_Helper_Generic<T> >::type type_validator_1;
  135. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::validator::detail::QxValidator_Helper_Ptr<T>, type_validator_1 >::type type_validator_2;
  136. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::validator::detail::QxValidator_Helper_Container<T>, type_validator_2 >::type type_validator_3;
  137. return type_validator_3::validate(t, group);
  138. }
  139. };
  140. } // namespace detail
  141. } // namespace validator
  142. } // namespace qx
  143. namespace qx {
  144. template <class T>
  145. QxInvalidValueX validate(T & t, const QString & group)
  146. { return qx::validator::detail::QxValidator_Helper<T>::validate(t, group); }
  147. template <class T>
  148. QxInvalidValueX validate(T & t)
  149. { return qx::validator::detail::QxValidator_Helper<T>::validate(t, ""); }
  150. template <class T>
  151. QxInvalidValueX validate(T & t, const QStringList & groups)
  152. {
  153. QxInvalidValueX invalidValues;
  154. if (groups.count() <= 0) { return qx::validate(t); }
  155. for (long l = 0; l < groups.count(); l++) { invalidValues.insert(qx::validate(t, groups.at(l))); }
  156. return invalidValues;
  157. }
  158. } // namespace qx
  159. #endif // _QX_VALIDATOR_FUNCTION_H_