/**************************************************************************** ** ** https://www.qxorm.com/ ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) ** ** This file is part of the QxOrm library ** ** This software is provided 'as-is', without any express or implied ** warranty. In no event will the authors be held liable for any ** damages arising from the use of this software ** ** Commercial Usage ** Licensees holding valid commercial QxOrm licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Lionel Marty ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file 'license.gpl3.txt' included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met : http://www.gnu.org/copyleft/gpl.html ** ** If you are unsure which license is appropriate for your use, or ** if you have questions regarding the use of this file, please contact : ** contact@qxorm.com ** ****************************************************************************/ #ifndef _QX_VALIDATOR_FUNCTION_H_ #define _QX_VALIDATOR_FUNCTION_H_ #ifdef _MSC_VER #pragma once #endif /*! * \file QxValidatorFct.h * \author Lionel Marty * \ingroup QxValidator * \brief Implementation of qx::validate() function (validator engine) */ #include #include #include #include #include #include namespace qx { template QxInvalidValueX validate(T & t, const QString & group); } // namespace qx namespace qx { namespace validator { namespace detail { template struct QxValidator_Helper_Generic { static inline qx::QxInvalidValueX validate(T & t, const QString & group) { static_assert(qx::trait::is_qx_registered::value, "qx::trait::is_qx_registered::value"); qx::QxInvalidValueX invalidValues; qx::IxClass * pClass = qx::QxClass::getSingleton(); if (! pClass) { qAssert(false); return invalidValues; } qx::IxValidatorX * pAllValidator = pClass->getAllValidator(); if (! pAllValidator) { return invalidValues; } invalidValues.setCurrentPath(pClass->getName()); invalidValues.insert(pAllValidator->validate((& t), group)); return invalidValues; } }; template struct QxValidator_Helper_Container { static inline qx::QxInvalidValueX validate(T & t, const QString & group) { qx::QxInvalidValueX invalidValues; long lIndex = 0; for (typename T::iterator it = t.begin(); it != t.end(); ++it) { invalidValues.setCurrentPath("[" + QString::number(lIndex) + "]"); invalidValues.insert(validateItem((* it), group)); lIndex++; } return invalidValues; } private: template static inline qx::QxInvalidValueX validateItem(U & item, const QString & group) { return validateItem_Helper::value || qx::trait::is_smart_ptr::value>::validate(item, group); } template struct validateItem_Helper { static inline qx::QxInvalidValueX validate(U & item, const QString & group) { return (item ? qx::validator::detail::QxValidator_Helper_Container::validateItem((* item), group) : qx::QxInvalidValueX()); } }; template struct validateItem_Helper, false> { static inline qx::QxInvalidValueX validate(std::pair & item, const QString & group) { return qx::validator::detail::QxValidator_Helper_Container::validateItem(item.second, group); } }; template struct validateItem_Helper, false> { static inline qx::QxInvalidValueX validate(const std::pair & item, const QString & group) { return qx::validator::detail::QxValidator_Helper_Container::validateItem(item.second, group); } }; template struct validateItem_Helper, false> { static inline qx::QxInvalidValueX validate(QPair & item, const QString & group) { return qx::validator::detail::QxValidator_Helper_Container::validateItem(item.second, group); } }; template struct validateItem_Helper, false> { static inline qx::QxInvalidValueX validate(const QPair & item, const QString & group) { return qx::validator::detail::QxValidator_Helper_Container::validateItem(item.second, group); } }; template struct validateItem_Helper { static qx::QxInvalidValueX validate(U & item, const QString & group) { return qx::validate(item, group); } }; }; template struct QxValidator_Helper_Ptr { static inline qx::QxInvalidValueX validate(T & t, const QString & group) { return (t ? qx::validate((* t), group) : qx::QxInvalidValueX()); } }; template struct QxValidator_Helper { static inline qx::QxInvalidValueX validate(T & t, const QString & group) { typedef typename std::conditional< std::is_pointer::value, qx::validator::detail::QxValidator_Helper_Ptr, qx::validator::detail::QxValidator_Helper_Generic >::type type_validator_1; typedef typename std::conditional< qx::trait::is_smart_ptr::value, qx::validator::detail::QxValidator_Helper_Ptr, type_validator_1 >::type type_validator_2; typedef typename std::conditional< qx::trait::is_container::value, qx::validator::detail::QxValidator_Helper_Container, type_validator_2 >::type type_validator_3; return type_validator_3::validate(t, group); } }; } // namespace detail } // namespace validator } // namespace qx namespace qx { template QxInvalidValueX validate(T & t, const QString & group) { return qx::validator::detail::QxValidator_Helper::validate(t, group); } template QxInvalidValueX validate(T & t) { return qx::validator::detail::QxValidator_Helper::validate(t, ""); } template QxInvalidValueX validate(T & t, const QStringList & groups) { QxInvalidValueX invalidValues; if (groups.count() <= 0) { return qx::validate(t); } for (long l = 0; l < groups.count(); l++) { invalidValues.insert(qx::validate(t, groups.at(l))); } return invalidValues; } } // namespace qx #endif // _QX_VALIDATOR_FUNCTION_H_