QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxTimeNeutral.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_TIME_NEUTRAL_H_
33#define _QX_TIME_NEUTRAL_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#ifdef _QX_ENABLE_BOOST_SERIALIZATION
47#include <boost/serialization/serialization.hpp>
48#include <boost/serialization/nvp.hpp>
49#endif // _QX_ENABLE_BOOST_SERIALIZATION
50
51#include <QtCore/qdatetime.h>
52#include <QtCore/qdatastream.h>
53
55
57
58namespace qx {
59class QxTimeNeutral;
60} // namespace qx
61
62QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxTimeNeutral & t) QX_USED;
63QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxTimeNeutral & t) QX_USED;
64
65namespace qx {
66
72{
73
74#ifdef _QX_ENABLE_BOOST_SERIALIZATION
76#endif // _QX_ENABLE_BOOST_SERIALIZATION
77
78 friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::QxTimeNeutral & t);
79 friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxTimeNeutral & t);
80
81private:
82
83 QTime m_time;
84 QString m_neutral;
85
86public:
87
89 explicit QxTimeNeutral(const QTime & time) : m_time(time) { update(); }
90 explicit QxTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
91 virtual ~QxTimeNeutral() { ; }
92
93 inline QTime toTime() const { return m_time; }
94 inline QString toNeutral() const { return m_neutral; }
95 inline bool isValid() const { return m_time.isValid(); }
96
97 inline void setTime(const QTime & time) { m_neutral = ""; m_time = time; update(); }
98 inline void setNeutral(const QString & neutral) { m_time = QTime(); m_neutral = neutral; update(); }
99
100 static QxTimeNeutral fromTime(const QTime & time) { return QxTimeNeutral(time); }
101 static QxTimeNeutral fromNeutral(const QString & neutral) { return QxTimeNeutral(neutral); }
102
103private:
104
105 static inline const char * format() { return "hhmmss"; }
106
107 void update()
108 {
109 if (m_neutral.isEmpty() && ! m_time.isValid()) { return; }
110 else if (m_time.isValid()) { m_neutral = m_time.toString(format()); }
111 else { qAssert(m_neutral.size() == QString(format()).size()); m_time = QTime::fromString(m_neutral, format()); qAssert(m_time.isValid()); }
112 }
113
114#ifdef _QX_ENABLE_BOOST_SERIALIZATION
115 template <class Archive>
116 void serialize(Archive & ar, const unsigned int file_version)
117 {
118 Q_UNUSED(file_version);
119 ar & boost::serialization::make_nvp("time_neutral", m_neutral);
120 if (Archive::is_loading::value) { m_time = QTime(); update(); }
121 }
122#endif // _QX_ENABLE_BOOST_SERIALIZATION
123
124};
125
126} // namespace qx
127
129
130#endif // _QX_TIME_NEUTRAL_H_
#define QX_USED
Definition QxMacro.h:244
#define qAssert(x)
Definition QxMacro.h:52
#define QX_DLL_EXPORT
Definition QxMacro.h:182
QX_DLL_EXPORT QDataStream & operator<<(QDataStream &stream, const qx::QxTimeNeutral &t) QX_USED
QX_DLL_EXPORT QDataStream & operator>>(QDataStream &stream, qx::QxTimeNeutral &t) QX_USED
qx::QxTimeNeutral : helper class to store a time value into database under neutral format (HHMMSS) =>...
void setTime(const QTime &time)
QxTimeNeutral(const QString &neutral)
QTime toTime() const
QString toNeutral() const
static QxTimeNeutral fromNeutral(const QString &neutral)
void setNeutral(const QString &neutral)
static const char * format()
static QxTimeNeutral fromTime(const QTime &time)
QTime m_time
Data value under QTime format from Qt library.
QxTimeNeutral(const QTime &time)
bool isValid() const
void serialize(Archive &ar, const unsigned int file_version)
virtual ~QxTimeNeutral()
QString m_neutral
Data value under neutral format 'hhmmss'.
friend class boost::serialization::access
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...
#define QX_REGISTER_CLASS_NAME(className)
Root namespace for all QxOrm library features.