![]() |
QxOrm
1.4.4
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_STD_OPTIONAL_H_ 00033 #define _QX_STD_OPTIONAL_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <optional> 00047 00048 #include <QtCore/qdatastream.h> 00049 00050 #include <QxOrm.h> 00051 00052 template <typename T> 00053 QDataStream & operator<< (QDataStream & stream, const std::optional<T> & t) 00054 { 00055 qint8 iHasData = (t ? 1 : 0); 00056 stream << iHasData; 00057 if (t) { stream << (* t); } 00058 return stream; 00059 } 00060 00061 template <typename T> 00062 QDataStream & operator>> (QDataStream & stream, std::optional<T> & t) 00063 { 00064 qint8 iHasData = 0; 00065 stream >> iHasData; 00066 if (iHasData) { t = T(); stream >> (* t); } 00067 else { t = std::optional<T>(); } 00068 return stream; 00069 } 00070 00071 QX_REGISTER_CLASS_NAME_TEMPLATE_1(std::optional) 00072 00073 namespace qx { 00074 namespace trait { 00075 00076 template <typename T> 00077 struct get_sql_type< std::optional<T> > 00078 { static inline const char * get() { return qx::trait::get_sql_type<T>::get(); } }; 00079 00080 } // namespace trait 00081 } // namespace qx 00082 00083 namespace qx { 00084 namespace cvt { 00085 namespace detail { 00086 00087 #ifndef _QX_NO_JSON 00088 00089 template <typename T> struct QxConvert_ToJson< std::optional<T> > { 00090 static inline QJsonValue toJson(const std::optional<T> & t, const QString & format) 00091 { if (t) { return qx::cvt::to_json((* t), format); }; return QJsonValue(); } }; 00092 00093 template <typename T> struct QxConvert_FromJson< std::optional<T> > { 00094 static inline qx_bool fromJson(const QJsonValue & j, std::optional<T> & t, const QString & format) 00095 { 00096 if (j.isNull()) { t = std::optional<T>(); return qx_bool(true); } 00097 else if (! t) { t = T(); } 00098 return qx::cvt::from_json(j, (* t), format); 00099 } }; 00100 00101 #endif // _QX_NO_JSON 00102 00103 template <typename T> struct QxConvert_ToString< std::optional<T> > { 00104 static inline QString toString(const std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00105 { if (t) { return qx::cvt::to_string((* t), format, index, ctx); }; return QString(); } }; 00106 00107 template <typename T> struct QxConvert_FromString< std::optional<T> > { 00108 static inline qx_bool fromString(const QString & s, std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00109 { if (! t) { t = T(); }; return qx::cvt::from_string(s, (* t), format, index, ctx); } }; 00110 00111 template <typename T> struct QxConvert_ToVariant< std::optional<T> > { 00112 static inline QVariant toVariant(const std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00113 { if (t) { return qx::cvt::to_variant((* t), format, index, ctx); }; return QVariant(); } }; 00114 00115 template <typename T> struct QxConvert_FromVariant< std::optional<T> > { 00116 static inline qx_bool fromVariant(const QVariant & v, std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx) 00117 { 00118 if (v.isNull()) { t = std::optional<T>(); return qx_bool(true); } 00119 else if (! t) { t = T(); } 00120 return qx::cvt::from_variant(v, (* t), format, index, ctx); 00121 } }; 00122 00123 } // namespace detail 00124 } // namespace cvt 00125 } // namespace qx 00126 00127 #endif // _QX_STD_OPTIONAL_H_