| QxOrm 1.1.6 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines