QxOrm  1.4.5
C++ Object Relational Mapping library
QxDateNeutral.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_DATE_NEUTRAL_H_
00033 #define _QX_DATE_NEUTRAL_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00047 #include <boost/serialization/serialization.hpp>
00048 #include <boost/serialization/nvp.hpp>
00049 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00050 
00051 #include <QtCore/qdatetime.h>
00052 #include <QtCore/qdatastream.h>
00053 
00054 #include <QxSerialize/Qt/QxSerialize_QString.h>
00055 
00056 #include <QxTraits/get_class_name.h>
00057 
00058 namespace qx {
00059 class QxDateNeutral;
00060 } // namespace qx
00061 
00062 QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxDateNeutral & t) QX_USED;
00063 QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxDateNeutral & t) QX_USED;
00064 
00065 namespace qx {
00066 
00071 class QxDateNeutral
00072 {
00073 
00074 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00075    friend class boost::serialization::access;
00076 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00077 
00078    friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::QxDateNeutral & t);
00079    friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxDateNeutral & t);
00080 
00081 private:
00082 
00083    QDate m_date;        
00084    QString m_neutral;   
00085 
00086 public:
00087 
00088    QxDateNeutral() { ; }
00089    explicit QxDateNeutral(const QDate & date) : m_date(date) { update(); }
00090    explicit QxDateNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
00091    virtual ~QxDateNeutral() { ; }
00092 
00093    inline QDate toDate() const         { return m_date; }
00094    inline QString toNeutral() const    { return m_neutral; }
00095    inline bool isValid() const         { return m_date.isValid(); }
00096 
00097    inline void setDate(const QDate & date)            { m_neutral = ""; m_date = date; update(); }
00098    inline void setNeutral(const QString & neutral)    { m_date = QDate(); m_neutral = neutral; update(); }
00099 
00100    static QxDateNeutral fromDate(const QDate & date)           { return QxDateNeutral(date); }
00101    static QxDateNeutral fromNeutral(const QString & neutral)   { return QxDateNeutral(neutral); }
00102 
00103 private:
00104 
00105    static inline const char * format() { return "yyyyMMdd"; }
00106 
00107    void update()
00108    {
00109       if (m_neutral.isEmpty() && ! m_date.isValid()) { return; }
00110       else if (m_date.isValid()) { m_neutral = m_date.toString(format()); }
00111       else { qAssert(m_neutral.size() == QString(format()).size()); m_date = QDate::fromString(m_neutral, format()); qAssert(m_date.isValid()); }
00112    }
00113 
00114 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00115    template <class Archive>
00116    void serialize(Archive & ar, const unsigned int file_version)
00117    {
00118       Q_UNUSED(file_version);
00119       ar & boost::serialization::make_nvp("date_neutral", m_neutral);
00120       if (Archive::is_loading::value) { m_date = QDate(); update(); }
00121    }
00122 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00123 
00124 };
00125 
00126 } // namespace qx
00127 
00128 QX_REGISTER_CLASS_NAME(qx::QxDateNeutral)
00129 
00130 #endif // _QX_DATE_NEUTRAL_H_