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_FUNCTION_H_ 00033 #define _QX_VALIDATOR_FUNCTION_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QxValidator/IxValidatorX.h> 00047 #include <QxValidator/QxInvalidValueX.h> 00048 00049 #include <QxRegister/QxClass.h> 00050 00051 #include <QxTraits/is_container.h> 00052 #include <QxTraits/is_smart_ptr.h> 00053 #include <QxTraits/is_qx_registered.h> 00054 00055 namespace qx { 00056 template <class T> 00057 QxInvalidValueX validate(T & t, const QString & group); 00058 } // namespace qx 00059 00060 namespace qx { 00061 namespace validator { 00062 namespace detail { 00063 00064 template <class T> 00065 struct QxValidator_Helper_Generic 00066 { 00067 00068 static inline qx::QxInvalidValueX validate(T & t, const QString & group) 00069 { 00070 static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value"); 00071 00072 qx::QxInvalidValueX invalidValues; 00073 qx::IxClass * pClass = qx::QxClass<T>::getSingleton(); 00074 if (! pClass) { qAssert(false); return invalidValues; } 00075 qx::IxValidatorX * pAllValidator = pClass->getAllValidator(); 00076 if (! pAllValidator) { return invalidValues; } 00077 invalidValues.setCurrentPath(pClass->getName()); 00078 invalidValues.insert(pAllValidator->validate((& t), group)); 00079 return invalidValues; 00080 } 00081 00082 }; 00083 00084 template <class T> 00085 struct QxValidator_Helper_Container 00086 { 00087 00088 static inline qx::QxInvalidValueX validate(T & t, const QString & group) 00089 { 00090 qx::QxInvalidValueX invalidValues; long lIndex = 0; 00091 for (typename T::iterator it = t.begin(); it != t.end(); ++it) 00092 { 00093 invalidValues.setCurrentPath("[" + QString::number(lIndex) + "]"); 00094 invalidValues.insert(validateItem((* it), group)); 00095 lIndex++; 00096 } 00097 return invalidValues; 00098 } 00099 00100 private: 00101 00102 template <typename U> 00103 static inline qx::QxInvalidValueX validateItem(U & item, const QString & group) 00104 { return validateItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::validate(item, group); } 00105 00106 template <typename U, bool bIsPointer /* = true */> 00107 struct validateItem_Helper 00108 { 00109 static inline qx::QxInvalidValueX validate(U & item, const QString & group) 00110 { return (item ? qx::validator::detail::QxValidator_Helper_Container<T>::validateItem((* item), group) : qx::QxInvalidValueX()); } 00111 }; 00112 00113 template <typename U1, typename U2> 00114 struct validateItem_Helper<std::pair<U1, U2>, false> 00115 { 00116 static inline qx::QxInvalidValueX validate(std::pair<U1, U2> & item, const QString & group) 00117 { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); } 00118 }; 00119 00120 template <typename U1, typename U2> 00121 struct validateItem_Helper<const std::pair<U1, U2>, false> 00122 { 00123 static inline qx::QxInvalidValueX validate(const std::pair<U1, U2> & item, const QString & group) 00124 { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); } 00125 }; 00126 00127 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) 00128 template <typename U1, typename U2> 00129 struct validateItem_Helper<QPair<U1, U2>, false> 00130 { 00131 static inline qx::QxInvalidValueX validate(QPair<U1, U2> & item, const QString & group) 00132 { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); } 00133 }; 00134 00135 template <typename U1, typename U2> 00136 struct validateItem_Helper<const QPair<U1, U2>, false> 00137 { 00138 static inline qx::QxInvalidValueX validate(const QPair<U1, U2> & item, const QString & group) 00139 { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); } 00140 }; 00141 #endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) 00142 00143 template <typename U> 00144 struct validateItem_Helper<U, false> 00145 { static qx::QxInvalidValueX validate(U & item, const QString & group) { return qx::validate(item, group); } }; 00146 00147 }; 00148 00149 template <class T> 00150 struct QxValidator_Helper_Ptr 00151 { 00152 00153 static inline qx::QxInvalidValueX validate(T & t, const QString & group) 00154 { return (t ? qx::validate((* t), group) : qx::QxInvalidValueX()); } 00155 00156 }; 00157 00158 template <class T> 00159 struct QxValidator_Helper 00160 { 00161 00162 static inline qx::QxInvalidValueX validate(T & t, const QString & group) 00163 { 00164 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; 00165 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; 00166 typedef typename std::conditional< qx::trait::is_container<T>::value, qx::validator::detail::QxValidator_Helper_Container<T>, type_validator_2 >::type type_validator_3; 00167 00168 return type_validator_3::validate(t, group); 00169 } 00170 00171 }; 00172 00173 } // namespace detail 00174 } // namespace validator 00175 } // namespace qx 00176 00177 namespace qx { 00178 00179 template <class T> 00180 QxInvalidValueX validate(T & t, const QString & group) 00181 { return qx::validator::detail::QxValidator_Helper<T>::validate(t, group); } 00182 00183 template <class T> 00184 QxInvalidValueX validate(T & t) 00185 { return qx::validator::detail::QxValidator_Helper<T>::validate(t, ""); } 00186 00187 template <class T> 00188 QxInvalidValueX validate(T & t, const QStringList & groups) 00189 { 00190 QxInvalidValueX invalidValues; 00191 if (groups.count() <= 0) { return qx::validate(t); } 00192 for (long l = 0; l < groups.count(); l++) { invalidValues.insert(qx::validate(t, groups.at(l))); } 00193 return invalidValues; 00194 } 00195 00196 } // namespace qx 00197 00198 #endif // _QX_VALIDATOR_FUNCTION_H_