QxOrm  1.4.5
C++ Object Relational Mapping library
QxBoostOptionalOnly.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_ENABLE_BOOST
00033 #ifndef _QX_BOOST_OPTIONAL_ONLY_H_
00034 #define _QX_BOOST_OPTIONAL_ONLY_H_
00035 
00036 #ifdef _MSC_VER
00037 #pragma once
00038 #endif
00039 
00047 #include <boost/optional.hpp>
00048 #include <boost/none.hpp>
00049 
00050 #include <QxOrm.h>
00051 
00052 #define _QX_ENABLE_BOOST
00053 #include <QxSerialize/QDataStream/QxSerializeQDataStream_boost_optional.h>
00054 #undef _QX_ENABLE_BOOST
00055 
00056 #include <QxTraits/construct_null_qvariant.h>
00057 
00058 QX_REGISTER_CLASS_NAME_TEMPLATE_1(boost::optional)
00059 
00060 namespace qx {
00061 namespace trait {
00062 
00063 template <typename T>
00064 struct get_sql_type< boost::optional<T> >
00065 { static inline const char * get() { return qx::trait::get_sql_type<T>::get(); } };
00066 
00067 } // namespace trait
00068 } // namespace qx
00069 
00070 namespace qx {
00071 namespace cvt {
00072 namespace detail {
00073 
00074 #ifndef _QX_NO_JSON
00075 
00076 template <typename T> struct QxConvert_ToJson< boost::optional<T> > {
00077 static inline QJsonValue toJson(const boost::optional<T> & t, const QString & format)
00078 { if (t) { return qx::cvt::to_json((* t), format); }; return QJsonValue(); } };
00079 
00080 template <typename T> struct QxConvert_FromJson< boost::optional<T> > {
00081 static inline qx_bool fromJson(const QJsonValue & j, boost::optional<T> & t, const QString & format)
00082 {
00083    if (j.isNull()) { t = boost::none; return qx_bool(true); }
00084    else if (! t) { t = T(); }
00085    return qx::cvt::from_json(j, (* t), format);
00086 } };
00087 
00088 #endif // _QX_NO_JSON
00089 
00090 template <typename T> struct QxConvert_ToString< boost::optional<T> > {
00091 static inline QString toString(const boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
00092 { if (t) { return qx::cvt::to_string((* t), format, index, ctx); }; return QString(); } };
00093 
00094 template <typename T> struct QxConvert_FromString< boost::optional<T> > {
00095 static inline qx_bool fromString(const QString & s, boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
00096 { if (! t) { t = T(); }; return qx::cvt::from_string(s, (* t), format, index, ctx); } };
00097 
00098 template <typename T> struct QxConvert_ToVariant< boost::optional<T> > {
00099 static inline QVariant toVariant(const boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
00100 { if (t) { return qx::cvt::to_variant((* t), format, index, ctx); }; return qx::trait::construct_null_qvariant<T>::get(); } };
00101 
00102 template <typename T> struct QxConvert_FromVariant< boost::optional<T> > {
00103 static inline qx_bool fromVariant(const QVariant & v, boost::optional<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
00104 {
00105    if (v.isNull()) { t = boost::none; return qx_bool(true); }
00106    else if (! t) { t = T(); }
00107    return qx::cvt::from_variant(v, (* t), format, index, ctx);
00108 } };
00109 
00110 } // namespace detail
00111 } // namespace cvt
00112 } // namespace qx
00113 
00114 #endif // _QX_BOOST_OPTIONAL_ONLY_H_
00115 #endif // _QX_ENABLE_BOOST