QxOrm 1.1.9
C++ Object Relational Mapping library
QxStringCvt_Impl.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_STRING_CVT_IMPL_H_
00027 #define _QX_STRING_CVT_IMPL_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00033 #include <boost/lexical_cast.hpp>
00034 #include <boost/mpl/if.hpp>
00035 #include <boost/mpl/logical.hpp>
00036 #include <boost/type_traits/is_pointer.hpp>
00037 #include <boost/type_traits/is_enum.hpp>
00038 
00039 #include <QxCommon/QxStringCvt.h>
00040 #include <QxCommon/QxBool.h>
00041 
00042 #include <QxDao/QxDateNeutral.h>
00043 #include <QxDao/QxTimeNeutral.h>
00044 #include <QxDao/QxDateTimeNeutral.h>
00045 
00046 #include <QxCollection/QxCollection.h>
00047 
00048 #include <QxRegister/QxClass.h>
00049 
00050 #include <QxSerialize/QxArchive.h>
00051 
00052 #include <QxTraits/is_smart_ptr.h>
00053 #include <QxTraits/is_container.h>
00054 #include <QxTraits/is_qx_registered.h>
00055 #include <QxTraits/is_qt_variant_compatible.h>
00056 #include <QxTraits/get_class_name_primitive.h>
00057 #include <QxTraits/construct_ptr.h>
00058 #include <QxTraits/generic_container.h>
00059 
00060 #define QX_STR_CVT_QDATE_FORMAT        "yyyyMMdd"
00061 #define QX_STR_CVT_QTIME_FORMAT        "hhmmsszzz"
00062 #define QX_STR_CVT_QDATETIME_FORMAT    "yyyyMMddhhmmsszzz"
00063 
00064 #if _QX_SERIALIZE_POLYMORPHIC
00065 #define QX_STR_CVT_DEFAULT_ARCHIVE     qx::serialization::polymorphic_xml
00066 #elif _QX_SERIALIZE_XML
00067 #define QX_STR_CVT_DEFAULT_ARCHIVE     qx::serialization::xml
00068 #elif _QX_SERIALIZE_TEXT
00069 #define QX_STR_CVT_DEFAULT_ARCHIVE     qx::serialization::text
00070 #elif _QX_SERIALIZE_BINARY
00071 #define QX_STR_CVT_DEFAULT_ARCHIVE     qx::serialization::binary
00072 #endif // _QX_SERIALIZE_XML
00073 
00074 #define QX_STR_CVT_BY_USING_ARCHIVE_IMPL(className) \
00075 namespace qx { namespace cvt { namespace detail { \
00076 template <> struct QxStringCvt_ToString< className > { \
00077 static inline QString toString(const className & t, const QString & format, int index) \
00078 { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::to_string(t); } }; \
00079 template <> struct QxStringCvt_FromString< className > { \
00080 static inline qx_bool fromString(const QString & s, className & t, const QString & format, int index) \
00081 { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::from_string(t, s); } }; \
00082 } } } // namespace qx::cvt::detail
00083 
00084 namespace qx {
00085 namespace cvt {
00086 namespace detail {
00087 
00088 template <typename T>
00089 struct QxStringCvtGeneric
00090 {
00091 
00092    static inline QString toString(const T & t, const QString & format, int index)
00093    {
00094       Q_UNUSED(format); Q_UNUSED(index); std::string s;
00095       try { s = boost::lexical_cast<std::string>(t); }
00096       catch (...) { qDebug("[QxOrm] %s", "'QxStringCvtGeneric::toString()' unknown error calling 'boost::lexical_cast<std::string>()'"); s = ""; }
00097       return QString::fromStdString(s);
00098    }
00099 
00100    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
00101    {
00102       Q_UNUSED(format); Q_UNUSED(index);
00103       try { t = boost::lexical_cast<T>(s.toStdString()); }
00104       catch (...) { qDebug("[QxOrm] %s", "'QxStringCvtGeneric::fromString()' unknown error calling 'boost::lexical_cast<T>()'"); return qx_bool(false); }
00105       return qx_bool(true);
00106    }
00107 
00108    static inline QVariant toVariant(const T & t, const QString & format, int index)
00109    { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::toVariant(t, format, index); }
00110 
00111    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
00112    { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::fromVariant(v, t, format, index); }
00113 
00114 private:
00115 
00116    template <bool isQVariantCompatible /* = false */, int dummy>
00117    struct cvtQVariant
00118    {
00119       static inline QVariant toVariant(const T & t, const QString & format, int index)                { return qx::cvt::to_string(t, format, index); };
00120       static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index) { return qx::cvt::from_string(v.toString(), t, format, index); };
00121    };
00122 
00123    template <int dummy>
00124    struct cvtQVariant<true, dummy>
00125    {
00126       static inline QVariant toVariant(const T & t, const QString & format, int index)                { Q_UNUSED(format); Q_UNUSED(index); return QVariant(t); };
00127       static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index) { Q_UNUSED(format); Q_UNUSED(index); t = v.value<T>(); return qx_bool(true); };
00128    };
00129 
00130 };
00131 
00132 template <typename T>
00133 struct QxStringCvtPtr
00134 {
00135 
00136    static inline QString toString(const T & t, const QString & format, int index)
00137    { return (t ? qx::cvt::to_string((* t), format, index) : ""); }
00138 
00139    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
00140    { if (! t) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_string(s, (* t), format, index) : qx_bool(false)); }
00141 
00142    static inline QVariant toVariant(const T & t, const QString & format, int index)
00143    { return (t ? qx::cvt::to_variant((* t), format, index) : QVariant()); }
00144 
00145    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
00146    { if (! t && ! v.isNull()) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_variant(v, (* t), format, index) : qx_bool(false)); }
00147 
00148 };
00149 
00150 template <typename T>
00151 struct QxStringCvtRegistered
00152 {
00153 
00154    static inline QString toString(const T & t, const QString & format, int index)
00155    { return (getId() ? getId()->toString((& t), format, index) : ""); }
00156 
00157    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
00158    { return (getId() ? getId()->fromString((& t), s, format, index) : qx_bool(false)); }
00159 
00160    static inline QVariant toVariant(const T & t, const QString & format, int index)
00161    { return (getId() ? getId()->toVariant((& t), format, index) : QVariant()); }
00162 
00163    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
00164    { return (getId() ? getId()->fromVariant((& t), v, format, index) : qx_bool(false)); }
00165 
00166 private:
00167 
00168    static inline qx::IxDataMember * getId()
00169    { return qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); }
00170 
00171 };
00172 
00173 template <typename T>
00174 struct QxStringCvtContainer
00175 {
00176 
00177    static inline QString toString(const T & t, const QString & format, int index)
00178    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::to_string(t); }
00179 
00180    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
00181    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::from_string(t, s); }
00182 
00183    static inline QVariant toVariant(const T & t, const QString & format, int index)
00184    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::to_string(t); }
00185 
00186    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
00187    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::from_string(t, v.toString()); }
00188 
00189 };
00190 
00191 template <typename T>
00192 struct QxStringCvtEnum
00193 {
00194 
00195    static inline QString toString(const T & t, const QString & format, int index)
00196    { Q_UNUSED(format); Q_UNUSED(index); return QString::number(static_cast<long>(t)); }
00197 
00198    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
00199    { Q_UNUSED(format); Q_UNUSED(index); bool bOk = false; t = static_cast<T>(static_cast<long>(s.toLongLong(& bOk))); return bOk; }
00200 
00201    static inline QVariant toVariant(const T & t, const QString & format, int index)
00202    { Q_UNUSED(format); Q_UNUSED(index); return QVariant(static_cast<qlonglong>(t)); }
00203 
00204    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
00205    { Q_UNUSED(format); Q_UNUSED(index); bool bOk = false; t = static_cast<T>(static_cast<long>(v.toLongLong(& bOk))); return bOk; }
00206 
00207 };
00208 
00209 template <typename T>
00210 struct QxStringCvtHelper
00211 {
00212 
00213 private:
00214 
00215    typedef typename boost::mpl::if_c< boost::is_pointer<T>::value, qx::cvt::detail::QxStringCvtPtr<T>, qx::cvt::detail::QxStringCvtGeneric<T> >::type type_str_cvt_helper_1;
00216    typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<T>::value, qx::cvt::detail::QxStringCvtPtr<T>, type_str_cvt_helper_1 >::type type_str_cvt_helper_2;
00217    typedef typename boost::mpl::if_c< qx::trait::is_container<T>::value, qx::cvt::detail::QxStringCvtContainer<T>, type_str_cvt_helper_2 >::type type_str_cvt_helper_3;
00218    typedef typename boost::mpl::if_c< qx::trait::is_qx_registered<T>::value, qx::cvt::detail::QxStringCvtRegistered<T>, type_str_cvt_helper_3 >::type type_str_cvt_helper_4;
00219    typedef typename boost::mpl::if_c< boost::is_enum<T>::value, qx::cvt::detail::QxStringCvtEnum<T>, type_str_cvt_helper_4 >::type type_str_cvt_helper_5;
00220 
00221 public:
00222 
00223    typedef typename QxStringCvtHelper<T>::type_str_cvt_helper_5 type;
00224 
00225 };
00226 
00227 template <typename T>
00228 struct QxStringCvt_ToString {
00229 static inline QString toString(const T & t, const QString & format, int index)
00230 { return qx::cvt::detail::QxStringCvtHelper<T>::type::toString(t, format, index); } };
00231 
00232 template <typename T>
00233 struct QxStringCvt_FromString {
00234 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
00235 { return qx::cvt::detail::QxStringCvtHelper<T>::type::fromString(s, t, format, index); } };
00236 
00237 template <typename T>
00238 struct QxStringCvt_ToVariant {
00239 static inline QVariant toVariant(const T & t, const QString & format, int index)
00240 { return qx::cvt::detail::QxStringCvtHelper<T>::type::toVariant(t, format, index); } };
00241 
00242 template <typename T>
00243 struct QxStringCvt_FromVariant {
00244 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
00245 { return qx::cvt::detail::QxStringCvtHelper<T>::type::fromVariant(v, t, format, index); } };
00246 
00247 } // namespace detail
00248 } // namespace cvt
00249 } // namespace qx
00250 
00251 #include "../../inl/QxCommon/QxStringCvt_WithIndex.inl"
00252 #include "../../inl/QxCommon/QxStringCvt_ToString.inl"
00253 #include "../../inl/QxCommon/QxStringCvt_FromString.inl"
00254 #include "../../inl/QxCommon/QxStringCvt_ToVariant.inl"
00255 #include "../../inl/QxCommon/QxStringCvt_FromVariant.inl"
00256 #include "../../inl/QxCommon/QxStringCvt_Qt.inl"
00257 
00258 #endif // _QX_STRING_CVT_IMPL_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines