QxOrm  1.2.3
C++ Object Relational Mapping library
QxValidatorFct.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_VALIDATOR_FUNCTION_H_
00027 #define _QX_VALIDATOR_FUNCTION_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/static_assert.hpp>
00041 #include <boost/mpl/if.hpp>
00042 #include <boost/mpl/logical.hpp>
00043 #include <boost/type_traits/is_pointer.hpp>
00044 
00045 #include <QxValidator/IxValidatorX.h>
00046 #include <QxValidator/QxInvalidValueX.h>
00047 
00048 #include <QxRegister/QxClass.h>
00049 
00050 #include <QxTraits/is_container.h>
00051 #include <QxTraits/is_smart_ptr.h>
00052 #include <QxTraits/is_qx_registered.h>
00053 
00054 namespace qx {
00055 template <class T>
00056 QxInvalidValueX validate(T & t, const QString & group);
00057 } // namespace qx
00058 
00059 namespace qx {
00060 namespace validator {
00061 namespace detail {
00062 
00063 template <class T>
00064 struct QxValidator_Helper_Generic
00065 {
00066 
00067    static inline qx::QxInvalidValueX validate(T & t, const QString & group)
00068    {
00069       BOOST_STATIC_ASSERT(qx::trait::is_qx_registered<T>::value);
00070 
00071       qx::QxInvalidValueX invalidValues;
00072       qx::IxClass * pClass = qx::QxClass<T>::getSingleton();
00073       if (! pClass) { qAssert(false); return invalidValues; }
00074       qx::IxValidatorX * pAllValidator = pClass->getAllValidator();
00075       if (! pAllValidator) { return invalidValues; }
00076       invalidValues.setCurrentPath(pClass->getName());
00077       invalidValues.insert(pAllValidator->validate((& t), group));
00078       return invalidValues;
00079    }
00080 
00081 };
00082 
00083 template <class T>
00084 struct QxValidator_Helper_Container
00085 {
00086 
00087    static inline qx::QxInvalidValueX validate(T & t, const QString & group)
00088    {
00089       qx::QxInvalidValueX invalidValues; long lIndex = 0;
00090       for (typename T::iterator it = t.begin(); it != t.end(); ++it)
00091       {
00092          invalidValues.setCurrentPath("[" + QString::number(lIndex) + "]");
00093          invalidValues.insert(validateItem((* it), group));
00094          lIndex++;
00095       }
00096       return invalidValues;
00097    }
00098 
00099 private:
00100 
00101    template <typename U>
00102    static inline qx::QxInvalidValueX validateItem(U & item, const QString & group)
00103    { return validateItem_Helper<U, boost::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::validate(item, group); }
00104 
00105    template <typename U, bool bIsPointer /* = true */>
00106    struct validateItem_Helper
00107    {
00108       static inline qx::QxInvalidValueX validate(U & item, const QString & group)
00109       { return (item ? qx::validator::detail::QxValidator_Helper_Container<T>::validateItem((* item), group) : qx::QxInvalidValueX()); }
00110    };
00111 
00112    template <typename U1, typename U2>
00113    struct validateItem_Helper<std::pair<U1, U2>, false>
00114    {
00115       static inline qx::QxInvalidValueX validate(std::pair<U1, U2> & item, const QString & group)
00116       { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
00117    };
00118 
00119    template <typename U1, typename U2>
00120    struct validateItem_Helper<const std::pair<U1, U2>, false>
00121    {
00122       static inline qx::QxInvalidValueX validate(const std::pair<U1, U2> & item, const QString & group)
00123       { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
00124    };
00125 
00126    template <typename U1, typename U2>
00127    struct validateItem_Helper<QPair<U1, U2>, false>
00128    {
00129       static inline qx::QxInvalidValueX validate(QPair<U1, U2> & item, const QString & group)
00130       { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
00131    };
00132 
00133    template <typename U1, typename U2>
00134    struct validateItem_Helper<const QPair<U1, U2>, false>
00135    {
00136       static inline qx::QxInvalidValueX validate(const QPair<U1, U2> & item, const QString & group)
00137       { return qx::validator::detail::QxValidator_Helper_Container<T>::validateItem(item.second, group); }
00138    };
00139 
00140    template <typename U>
00141    struct validateItem_Helper<U, false>
00142    { static qx::QxInvalidValueX validate(U & item, const QString & group) { return qx::validate(item, group); } };
00143 
00144 };
00145 
00146 template <class T>
00147 struct QxValidator_Helper_Ptr
00148 {
00149 
00150    static inline qx::QxInvalidValueX validate(T & t, const QString & group)
00151    { return (t ? qx::validate((* t), group) : qx::QxInvalidValueX()); }
00152 
00153 };
00154 
00155 template <class T>
00156 struct QxValidator_Helper
00157 {
00158 
00159    static inline qx::QxInvalidValueX validate(T & t, const QString & group)
00160    {
00161       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;
00162       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;
00163       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;
00164 
00165       return type_validator_3::validate(t, group);
00166    }
00167 
00168 };
00169 
00170 } // namespace detail
00171 } // namespace validator
00172 } // namespace qx
00173 
00174 namespace qx {
00175 
00176 template <class T>
00177 QxInvalidValueX validate(T & t, const QString & group)
00178 { return qx::validator::detail::QxValidator_Helper<T>::validate(t, group); }
00179 
00180 template <class T>
00181 QxInvalidValueX validate(T & t)
00182 { return qx::validator::detail::QxValidator_Helper<T>::validate(t, ""); }
00183 
00184 template <class T>
00185 QxInvalidValueX validate(T & t, const QStringList & groups)
00186 {
00187    QxInvalidValueX invalidValues;
00188    for (long l = 0; l < groups.count(); l++)
00189    { invalidValues.insert(qx::validate(t, groups.at(l))); }
00190    return invalidValues;
00191 }
00192 
00193 } // namespace qx
00194 
00195 #endif // _QX_VALIDATOR_FUNCTION_H_