| QxOrm 1.2.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 ** 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       return QString::fromStdString(s);
    00108    }
    00109
    00110    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
    00111    {
    00112       Q_UNUSED(format); Q_UNUSED(index);
    00113       try { t = boost::lexical_cast<T>(s.toStdString()); }
    00114       catch (...) { qDebug("[QxOrm] %s", "'QxStringCvtGeneric::fromString()' unknown error calling 'boost::lexical_cast<T>()'"); return qx_bool(false); }
    00115       return qx_bool(true);
    00116    }
    00117
    00118    static inline QVariant toVariant(const T & t, const QString & format, int index)
    00119    { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::toVariant(t, format, index); }
    00120
    00121    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
    00122    { return cvtQVariant<qx::trait::is_qt_variant_compatible<T>::value, 0>::fromVariant(v, t, format, index); }
    00123
    00124 private:
    00125
    00126    template <bool isQVariantCompatible /* = false */, int dummy>
    00127    struct cvtQVariant
    00128    {
    00129       static inline QVariant toVariant(const T & t, const QString & format, int index)                { return qx::cvt::to_string(t, format, index); };
    00130       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); };
    00131    };
    00132
    00133    template <int dummy>
    00134    struct cvtQVariant<true, dummy>
    00135    {
    00136       static inline QVariant toVariant(const T & t, const QString & format, int index)                { Q_UNUSED(format); Q_UNUSED(index); return QVariant(t); };
    00137       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); };
    00138    };
    00139
    00140 };
    00141
    00142 template <typename T>
    00143 struct QxStringCvtPtr
    00144 {
    00145
    00146    static inline QString toString(const T & t, const QString & format, int index)
    00147    { return (t ? qx::cvt::to_string((* t), format, index) : ""); }
    00148
    00149    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
    00150    { if (! t) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_string(s, (* t), format, index) : qx_bool(false)); }
    00151
    00152    static inline QVariant toVariant(const T & t, const QString & format, int index)
    00153    { return (t ? qx::cvt::to_variant((* t), format, index) : QVariant()); }
    00154
    00155    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
    00156    { if (! t && ! v.isNull()) { qx::trait::construct_ptr<T>::get(t); }; return (t ? qx::cvt::from_variant(v, (* t), format, index) : qx_bool(false)); }
    00157
    00158 };
    00159
    00160 template <typename T>
    00161 struct QxStringCvtRegistered
    00162 {
    00163
    00164    static inline QString toString(const T & t, const QString & format, int index)
    00165    { return (getId() ? getId()->toString((& t), format, index) : ""); }
    00166
    00167    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
    00168    { return (getId() ? getId()->fromString((& t), s, format, index) : qx_bool(false)); }
    00169
    00170    static inline QVariant toVariant(const T & t, const QString & format, int index)
    00171    { return (getId() ? getId()->toVariant((& t), format, index) : QVariant()); }
    00172
    00173    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
    00174    { return (getId() ? getId()->fromVariant((& t), v, format, index) : qx_bool(false)); }
    00175
    00176 private:
    00177
    00178    static inline qx::IxDataMember * getId()
    00179    { return qx::QxClass<T>::getSingleton()->getDataMemberX()->getId_WithDaoStrategy(); }
    00180
    00181 };
    00182
    00183 template <typename T>
    00184 struct QxStringCvtContainer
    00185 {
    00186
    00187    static inline QString toString(const T & t, const QString & format, int index)
    00188    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::to_string(t); }
    00189
    00190    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
    00191    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::from_string(t, s); }
    00192
    00193    static inline QVariant toVariant(const T & t, const QString & format, int index)
    00194    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::to_string(t); }
    00195
    00196    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
    00197    { Q_UNUSED(format); Q_UNUSED(index); return QX_STR_CVT_DEFAULT_ARCHIVE::from_string(t, v.toString()); }
    00198
    00199 };
    00200
    00201 template <typename T>
    00202 struct QxStringCvtEnum
    00203 {
    00204
    00205    static inline QString toString(const T & t, const QString & format, int index)
    00206    { Q_UNUSED(format); Q_UNUSED(index); return QString::number(static_cast<long>(t)); }
    00207
    00208    static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
    00209    { Q_UNUSED(format); Q_UNUSED(index); bool bOk = false; t = static_cast<T>(static_cast<long>(s.toLongLong(& bOk))); return bOk; }
    00210
    00211    static inline QVariant toVariant(const T & t, const QString & format, int index)
    00212    { Q_UNUSED(format); Q_UNUSED(index); return QVariant(static_cast<qlonglong>(t)); }
    00213
    00214    static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
    00215    { Q_UNUSED(format); Q_UNUSED(index); bool bOk = false; t = static_cast<T>(static_cast<long>(v.toLongLong(& bOk))); return bOk; }
    00216
    00217 };
    00218
    00219 template <typename T>
    00220 struct QxStringCvtHelper
    00221 {
    00222
    00223 private:
    00224
    00225    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;
    00226    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;
    00227    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;
    00228    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;
    00229    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;
    00230
    00231 public:
    00232
    00233    typedef typename QxStringCvtHelper<T>::type_str_cvt_helper_5 type;
    00234
    00235 };
    00236
    00237 template <typename T>
    00238 struct QxStringCvt_ToString {
    00239 static inline QString toString(const T & t, const QString & format, int index)
    00240 { return qx::cvt::detail::QxStringCvtHelper<T>::type::toString(t, format, index); } };
    00241
    00242 template <typename T>
    00243 struct QxStringCvt_FromString {
    00244 static inline qx_bool fromString(const QString & s, T & t, const QString & format, int index)
    00245 { return qx::cvt::detail::QxStringCvtHelper<T>::type::fromString(s, t, format, index); } };
    00246
    00247 template <typename T>
    00248 struct QxStringCvt_ToVariant {
    00249 static inline QVariant toVariant(const T & t, const QString & format, int index)
    00250 { return qx::cvt::detail::QxStringCvtHelper<T>::type::toVariant(t, format, index); } };
    00251
    00252 template <typename T>
    00253 struct QxStringCvt_FromVariant {
    00254 static inline qx_bool fromVariant(const QVariant & v, T & t, const QString & format, int index)
    00255 { return qx::cvt::detail::QxStringCvtHelper<T>::type::fromVariant(v, t, format, index); } };
    00256
    00257 } // namespace detail
    00258 } // namespace cvt
    00259 } // namespace qx
    00260
    00261 #include "../../inl/QxCommon/QxStringCvt_WithIndex.inl"
    00262 #include "../../inl/QxCommon/QxStringCvt_ToString.inl"
    00263 #include "../../inl/QxCommon/QxStringCvt_FromString.inl"
    00264 #include "../../inl/QxCommon/QxStringCvt_ToVariant.inl"
    00265 #include "../../inl/QxCommon/QxStringCvt_FromVariant.inl"
    00266 #include "../../inl/QxCommon/QxStringCvt_Qt.inl"
    00267
    00268 #endif // _QX_STRING_CVT_IMPL_H_