QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxStdOptional.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_STD_OPTIONAL_H_
33#define _QX_STD_OPTIONAL_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <optional>
47
48#include <QtCore/qdatastream.h>
49
50#include <QxOrm.h>
51
53
54template <typename T>
55QDataStream & operator<< (QDataStream & stream, const std::optional<T> & t)
56{
57 qint8 iHasData = (t ? 1 : 0);
58 stream << iHasData;
59 if (t) { stream << (* t); }
60 return stream;
61}
62
63template <typename T>
64QDataStream & operator>> (QDataStream & stream, std::optional<T> & t)
65{
66 qint8 iHasData = 0;
67 stream >> iHasData;
68 if (iHasData) { t = T(); stream >> (* t); }
69 else { t = std::optional<T>(); }
70 return stream;
71}
72
74
75namespace qx {
76namespace trait {
77
78template <typename T>
79struct get_sql_type< std::optional<T> >
80{ static inline const char * get() { return qx::trait::get_sql_type<T>::get(); } };
81
82} // namespace trait
83} // namespace qx
84
85namespace qx {
86namespace cvt {
87namespace detail {
88
89#ifndef _QX_NO_JSON
90
91template <typename T> struct QxConvert_ToJson< std::optional<T> > {
92static inline QJsonValue toJson(const std::optional<T> & t, const QString & format)
93{ if (t) { return qx::cvt::to_json((* t), format); }; return QJsonValue(); } };
94
95template <typename T> struct QxConvert_FromJson< std::optional<T> > {
96static inline qx_bool fromJson(const QJsonValue & j, std::optional<T> & t, const QString & format)
97{
98 if (j.isNull()) { t = std::optional<T>(); return qx_bool(true); }
99 else if (! t) { t = T(); }
100 return qx::cvt::from_json(j, (* t), format);
101} };
102
103#endif // _QX_NO_JSON
104
105template <typename T> struct QxConvert_ToString< std::optional<T> > {
106static inline QString toString(const std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
107{ if (t) { return qx::cvt::to_string((* t), format, index, ctx); }; return QString(); } };
108
109template <typename T> struct QxConvert_FromString< std::optional<T> > {
110static inline qx_bool fromString(const QString & s, std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
111{ if (! t) { t = T(); }; return qx::cvt::from_string(s, (* t), format, index, ctx); } };
112
113template <typename T> struct QxConvert_ToVariant< std::optional<T> > {
114static inline QVariant toVariant(const std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
115{ if (t) { return qx::cvt::to_variant((* t), format, index, ctx); }; return qx::trait::construct_null_qvariant<T>::get(); } };
116
117template <typename T> struct QxConvert_FromVariant< std::optional<T> > {
118static inline qx_bool fromVariant(const QVariant & v, std::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
119{
120 if (v.isNull()) { t = std::optional<T>(); return qx_bool(true); }
121 else if (! t) { t = T(); }
122 return qx::cvt::from_variant(v, (* t), format, index, ctx);
123} };
124
125} // namespace detail
126} // namespace cvt
127} // namespace qx
128
129#endif // _QX_STD_OPTIONAL_H_
qx::QxBool qx_bool
Definition QxBool.h:150
QDataStream & operator>>(QDataStream &stream, std::optional< T > &t)
QDataStream & operator<<(QDataStream &stream, const std::optional< T > &t)
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx::trait::construct_null_qvariant<T>::get() : create a NULL QVariant which matches QVariant::Type wi...
#define QX_REGISTER_CLASS_NAME_TEMPLATE_1(className)
qx_bool from_variant(const QVariant &v, T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:80
QVariant to_variant(const T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:79
QString to_string(const T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:77
qx_bool from_json(const QJsonValue &j, T &t, const QString &format=QString())
Definition QxConvert.h:84
qx_bool from_string(const QString &s, T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
Definition QxConvert.h:78
QJsonValue to_json(const T &t, const QString &format=QString())
Definition QxConvert.h:83
Root namespace for all QxOrm library features.
static qx_bool fromJson(const QJsonValue &j, std::optional< T > &t, const QString &format)
static qx_bool fromString(const QString &s, std::optional< T > &t, const QString &format, int index, qx::cvt::context::ctx_type ctx)
static qx_bool fromVariant(const QVariant &v, std::optional< T > &t, const QString &format, int index, qx::cvt::context::ctx_type ctx)
static QJsonValue toJson(const std::optional< T > &t, const QString &format)
static QString toString(const std::optional< T > &t, const QString &format, int index, qx::cvt::context::ctx_type ctx)
static QVariant toVariant(const std::optional< T > &t, const QString &format, int index, qx::cvt::context::ctx_type ctx)
qx::trait::get_sql_type<T>::get() : return type name under const char * format used by database engin...
static const char * get()