QxOrm  1.2.3
C++ Object Relational Mapping library
QxTimeNeutral.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_TIME_NEUTRAL_H_
00027 #define _QX_TIME_NEUTRAL_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/serialization/serialization.hpp>
00041 #include <boost/serialization/nvp.hpp>
00042 
00043 #include <QtCore/qdatetime.h>
00044 
00045 #include <QxSerialize/Qt/QxSerialize_QString.h>
00046 
00047 #include <QxTraits/get_class_name.h>
00048 
00049 namespace qx {
00050 
00055 class QxTimeNeutral
00056 {
00057 
00058    friend class boost::serialization::access;
00059 
00060 private:
00061 
00062    QTime m_time;        
00063    QString m_neutral;   
00064 
00065 public:
00066 
00067    QxTimeNeutral() { ; }
00068    explicit QxTimeNeutral(const QTime & time) : m_time(time) { update(); }
00069    explicit QxTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
00070    virtual ~QxTimeNeutral() { ; }
00071 
00072    inline QTime toTime() const         { return m_time; }
00073    inline QString toNeutral() const    { return m_neutral; }
00074    inline bool isValid() const         { return m_time.isValid(); }
00075 
00076    inline void setTime(const QTime & time)            { m_neutral = ""; m_time = time; update(); }
00077    inline void setNeutral(const QString & neutral)    { m_time = QTime(); m_neutral = neutral; update(); }
00078 
00079    static QxTimeNeutral fromTime(const QTime & time)           { return QxTimeNeutral(time); }
00080    static QxTimeNeutral fromNeutral(const QString & neutral)   { return QxTimeNeutral(neutral); }
00081 
00082 private:
00083 
00084    static inline const char * format() { return "hhmmss"; }
00085 
00086    void update()
00087    {
00088       if (m_neutral.isEmpty() && ! m_time.isValid()) { return; }
00089       else if (m_time.isValid()) { m_neutral = m_time.toString(format()); }
00090       else { qAssert(m_neutral.size() == QString(format()).size()); m_time = QTime::fromString(m_neutral, format()); qAssert(m_time.isValid()); }
00091    }
00092 
00093    template <class Archive>
00094    void serialize(Archive & ar, const unsigned int file_version)
00095    {
00096       Q_UNUSED(file_version);
00097       ar & boost::serialization::make_nvp("time_neutral", m_neutral);
00098       if (Archive::is_loading::value) { m_time = QTime(); update(); }
00099    }
00100 
00101 };
00102 
00103 } // namespace qx
00104 
00105 QX_REGISTER_CLASS_NAME(qx::QxTimeNeutral)
00106 
00107 #endif // _QX_TIME_NEUTRAL_H_