QxOrm  1.4.1
C++ Object Relational Mapping library
QxTimeNeutral.h
Go to the documentation of this file.
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_TIME_NEUTRAL_H_
00033 #define _QX_TIME_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 QxTimeNeutral;
00060 } // namespace qx
00061 
00062 QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxTimeNeutral & t) BOOST_USED;
00063 QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxTimeNeutral & t) BOOST_USED;
00064 
00065 namespace qx {
00066 
00071 class QxTimeNeutral
00072 {
00073 
00074 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00075    friend class boost::serialization::access;
00076 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00077 
00078    friend QDataStream & ::operator<< (QDataStream & stream, const qx::QxTimeNeutral & t);
00079    friend QDataStream & ::operator>> (QDataStream & stream, qx::QxTimeNeutral & t);
00080 
00081 private:
00082 
00083    QTime m_time;        
00084    QString m_neutral;   
00085 
00086 public:
00087 
00088    QxTimeNeutral() { ; }
00089    explicit QxTimeNeutral(const QTime & time) : m_time(time) { update(); }
00090    explicit QxTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
00091    virtual ~QxTimeNeutral() { ; }
00092 
00093    inline QTime toTime() const         { return m_time; }
00094    inline QString toNeutral() const    { return m_neutral; }
00095    inline bool isValid() const         { return m_time.isValid(); }
00096 
00097    inline void setTime(const QTime & time)            { m_neutral = ""; m_time = time; update(); }
00098    inline void setNeutral(const QString & neutral)    { m_time = QTime(); m_neutral = neutral; update(); }
00099 
00100    static QxTimeNeutral fromTime(const QTime & time)           { return QxTimeNeutral(time); }
00101    static QxTimeNeutral fromNeutral(const QString & neutral)   { return QxTimeNeutral(neutral); }
00102 
00103 private:
00104 
00105    static inline const char * format() { return "hhmmss"; }
00106 
00107    void update()
00108    {
00109       if (m_neutral.isEmpty() && ! m_time.isValid()) { return; }
00110       else if (m_time.isValid()) { m_neutral = m_time.toString(format()); }
00111       else { qAssert(m_neutral.size() == QString(format()).size()); m_time = QTime::fromString(m_neutral, format()); qAssert(m_time.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("time_neutral", m_neutral);
00120       if (Archive::is_loading::value) { m_time = QTime(); update(); }
00121    }
00122 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00123 
00124 };
00125 
00126 } // namespace qx
00127 
00128 QX_REGISTER_CLASS_NAME(qx::QxTimeNeutral)
00129 
00130 #endif // _QX_TIME_NEUTRAL_H_