| QxOrm 1.4.8 C++ Object Relational Mapping library

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

- File List
- File Members

QxStdOptional.h
Go to the documentation of this file.

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