QxOrm  1.5.0
C++ Object Relational Mapping library
QxBool.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_BOOL_H_
00033 #define _QX_BOOL_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/qdatastream.h>
00052 
00053 #include <QxSerialize/Qt/QxSerialize_QString.h>
00054 
00055 #include <QxTraits/get_class_name.h>
00056 
00057 namespace qx {
00058 class QxBool;
00059 } // namespace qx
00060 
00061 QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxBool & t) QX_USED;
00062 QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxBool & t) QX_USED;
00063 
00064 namespace qx {
00065 
00070 class QxBool
00071 {
00072 
00073 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00074    friend class boost::serialization::access;
00075 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00076 
00077    friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::QxBool & t);
00078    friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxBool & t);
00079 
00080 private:
00081 
00082    bool m_bValue;    
00083    long m_lCode;     
00084    QString m_sDesc;  
00085 
00086 public:
00087 
00088    QxBool() : m_bValue(false), m_lCode(0) { ; }
00089    QxBool(bool b) : m_bValue(b), m_lCode(0) { qAssert(checkInitialized(b)); }
00090    QxBool(long lCode, const QString & sDesc) : m_bValue(false), m_lCode(lCode), m_sDesc(sDesc) { ; }
00091    QxBool(bool bValue, long lCode, const QString & sDesc) : m_bValue(bValue), m_lCode(lCode), m_sDesc(sDesc) { qAssert(checkInitialized(bValue)); }
00092    QxBool(const QxBool & other) : m_bValue(other.getValue()), m_lCode(other.getCode()), m_sDesc(other.getDesc()) { ; }
00093    ~QxBool() { ; }
00094 
00095    inline bool getValue() const                          { return m_bValue; }
00096    inline long getCode() const                           { return m_lCode; }
00097    inline QString getDesc() const                        { return m_sDesc; }
00098 
00099    inline void setValue(bool bValue)                     { m_bValue = bValue; qAssert(checkInitialized(bValue)); }
00100    inline void setCode(long lCode)                       { m_lCode = lCode; }
00101    inline void setDesc(const QString & sDesc)            { m_sDesc = sDesc; }
00102 
00103    inline QxBool & operator=(const QxBool & other)       { m_bValue = other.getValue(); m_lCode = other.getCode(); m_sDesc = other.getDesc(); return (* this); }
00104    inline QxBool & operator=(const bool b)               { m_bValue = b; qAssert(checkInitialized(b)); return (* this); }
00105 
00106    inline operator bool() const                          { return (m_bValue != false); }
00107    inline bool operator!() const                         { return (m_bValue == false); }
00108 
00109    inline bool operator==(const QxBool & other) const    { return ((m_bValue == other.getValue()) && (m_lCode == other.getCode()) && (m_sDesc == other.getDesc())); }
00110    inline bool operator==(const bool b) const            { qAssert(checkInitialized(b)); return (m_bValue == b); }
00111    inline bool operator!=(const QxBool & other) const    { return ((m_bValue != other.getValue()) || (m_lCode != other.getCode()) || (m_sDesc != other.getDesc())); }
00112    inline bool operator!=(const bool b) const            { qAssert(checkInitialized(b)); return (m_bValue != b); }
00113    inline bool operator&&(const QxBool & other) const    { return (m_bValue && other.getValue()); }
00114    inline bool operator&&(const bool b) const            { qAssert(checkInitialized(b)); return (m_bValue && b); }
00115    inline bool operator||(const QxBool & other) const    { return (m_bValue || other.getValue()); }
00116    inline bool operator||(const bool b) const            { qAssert(checkInitialized(b)); return (m_bValue || b); }
00117 
00118    QString toString() const { return (QString(m_bValue ? "1" : "0") + "|" + QString::number(static_cast<qlonglong>(m_lCode)) + "|" + m_sDesc); }
00119 
00120    void fromString(const QString & s)
00121    {
00122       if (s.trimmed().isEmpty()) { (* this) = QxBool(); return; }
00123       bool bValue = s.startsWith("1");
00124       int iPos = s.indexOf("|", 2);
00125       if (iPos == -1) { (* this) = QxBool(bValue); return; }
00126       long lCode = s.mid(2, (iPos - 2)).toLong();
00127       QString sDesc = s.right(s.size() - (iPos + 1));
00128       (* this) = QxBool(bValue, lCode, sDesc);
00129    }
00130 
00131 private:
00132 
00133 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00134    template <class Archive>
00135    void serialize(Archive & ar, const unsigned int file_version)
00136    {
00137       Q_UNUSED(file_version);
00138       ar & boost::serialization::make_nvp("value", m_bValue);
00139       ar & boost::serialization::make_nvp("code", m_lCode);
00140       ar & boost::serialization::make_nvp("desc", m_sDesc);
00141    }
00142 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00143 
00144    inline bool checkInitialized(const bool b) const { return ((static_cast<int>(b) == 0) || (static_cast<int>(b) == 1)); }
00145 
00146 };
00147 
00148 } // namespace qx
00149 
00150 typedef qx::QxBool qx_bool;
00151 QX_REGISTER_CLASS_NAME(qx_bool)
00152 
00153 #endif // _QX_BOOL_H_