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