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