QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxDateNeutral.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_DATE_NEUTRAL_H_
33#define _QX_DATE_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 QxDateNeutral;
60} // namespace qx
61
62QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxDateNeutral & t) QX_USED;
63QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxDateNeutral & 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::QxDateNeutral & t);
79 friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxDateNeutral & t);
80
81private:
82
83 QDate m_date;
84 QString m_neutral;
85
86public:
87
89 explicit QxDateNeutral(const QDate & date) : m_date(date) { update(); }
90 explicit QxDateNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
91 virtual ~QxDateNeutral() { ; }
92
93 inline QDate toDate() const { return m_date; }
94 inline QString toNeutral() const { return m_neutral; }
95 inline bool isValid() const { return m_date.isValid(); }
96
97 inline void setDate(const QDate & date) { m_neutral = ""; m_date = date; update(); }
98 inline void setNeutral(const QString & neutral) { m_date = QDate(); m_neutral = neutral; update(); }
99
100 static QxDateNeutral fromDate(const QDate & date) { return QxDateNeutral(date); }
101 static QxDateNeutral fromNeutral(const QString & neutral) { return QxDateNeutral(neutral); }
102
103private:
104
105 static inline const char * format() { return "yyyyMMdd"; }
106
107 void update()
108 {
109 if (m_neutral.isEmpty() && ! m_date.isValid()) { return; }
110 else if (m_date.isValid()) { m_neutral = m_date.toString(format()); }
111 else { qAssert(m_neutral.size() == QString(format()).size()); m_date = QDate::fromString(m_neutral, format()); qAssert(m_date.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("date_neutral", m_neutral);
120 if (Archive::is_loading::value) { m_date = QDate(); update(); }
121 }
122#endif // _QX_ENABLE_BOOST_SERIALIZATION
123
124};
125
126} // namespace qx
127
129
130#endif // _QX_DATE_NEUTRAL_H_
QX_DLL_EXPORT QDataStream & operator>>(QDataStream &stream, qx::QxDateNeutral &t) QX_USED
QX_DLL_EXPORT QDataStream & operator<<(QDataStream &stream, const qx::QxDateNeutral &t) QX_USED
#define QX_USED
Definition QxMacro.h:244
#define qAssert(x)
Definition QxMacro.h:52
#define QX_DLL_EXPORT
Definition QxMacro.h:182
qx::QxDateNeutral : helper class to store a date value into database under neutral format (YYYYMMDD) ...
void setNeutral(const QString &neutral)
static QxDateNeutral fromDate(const QDate &date)
QDate toDate() const
QxDateNeutral(const QDate &date)
static QxDateNeutral fromNeutral(const QString &neutral)
void setDate(const QDate &date)
void serialize(Archive &ar, const unsigned int file_version)
bool isValid() const
virtual ~QxDateNeutral()
QString m_neutral
Data value under neutral format 'yyyyMMdd'.
QxDateNeutral(const QString &neutral)
friend class boost::serialization::access
QDate m_date
Data value under QDate format from Qt library.
static const char * format()
QString toNeutral() const
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.