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