QxOrm  1.4.5
C++ Object Relational Mapping library
QxValidatorFct.h
Go to the documentation of this file.
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    template <typename U1, typename U2>
00128    struct validateItem_Helper<QPair<U1, U2>, false>
00129    {
00130       static inline qx::QxInvalidValueX validate(QPair<U1, U2> & item, const QString & group)
00131       { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
00132    };
00133 
00134    template <typename U1, typename U2>
00135    struct validateItem_Helper<const QPair<U1, U2>, false>
00136    {
00137       static inline qx::QxInvalidValueX validate(const QPair<U1, U2> & item, const QString & group)
00138       { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
00139    };
00140 
00141    template <typename U>
00142    struct validateItem_Helper<U, false>
00143    { static qx::QxInvalidValueX validate(U & item, const QString & group) { return qx::validate(item, group); } };
00144 
00145 };
00146 
00147 template <class T>
00148 struct QxValidator_Helper_Ptr
00149 {
00150 
00151    static inline qx::QxInvalidValueX validate(T & t, const QString & group)
00152    { return (t ? qx::validate((* t), group) : qx::QxInvalidValueX()); }
00153 
00154 };
00155 
00156 template <class T>
00157 struct QxValidator_Helper
00158 {
00159 
00160    static inline qx::QxInvalidValueX validate(T & t, const QString & group)
00161    {
00162       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;
00163       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;
00164       typedef typename std::conditional< qx::trait::is_container<T>::value, qx::validator::detail::QxValidator_Helper_Container<T>, type_validator_2 >::type type_validator_3;
00165 
00166       return type_validator_3::validate(t, group);
00167    }
00168 
00169 };
00170 
00171 } // namespace detail
00172 } // namespace validator
00173 } // namespace qx
00174 
00175 namespace qx {
00176 
00177 template <class T>
00178 QxInvalidValueX validate(T & t, const QString & group)
00179 { return qx::validator::detail::QxValidator_Helper<T>::validate(t, group); }
00180 
00181 template <class T>
00182 QxInvalidValueX validate(T & t)
00183 { return qx::validator::detail::QxValidator_Helper<T>::validate(t, ""); }
00184 
00185 template <class T>
00186 QxInvalidValueX validate(T & t, const QStringList & groups)
00187 {
00188    QxInvalidValueX invalidValues;
00189    for (long l = 0; l < groups.count(); l++)
00190    { invalidValues.insert(qx::validate(t, groups.at(l))); }
00191    return invalidValues;
00192 }
00193 
00194 } // namespace qx
00195 
00196 #endif // _QX_VALIDATOR_FUNCTION_H_