QxBool.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef _QX_BOOL_H_
  32. #define _QX_BOOL_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxBool.h
  38. * \author Lionel Marty
  39. * \ingroup QxCommon
  40. * \brief qx_bool : QxOrm library boolean type with code and description message when an error occured
  41. */
  42. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  43. #include <boost/serialization/serialization.hpp>
  44. #include <boost/serialization/nvp.hpp>
  45. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  46. #include <QtCore/qdatastream.h>
  47. #include <QxSerialize/Qt/QxSerialize_QString.h>
  48. #include <QxTraits/get_class_name.h>
  49. namespace qx {
  50. class QxBool;
  51. } // namespace qx
  52. QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxBool & t) QX_USED;
  53. QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxBool & t) QX_USED;
  54. namespace qx {
  55. /*!
  56. * \ingroup QxCommon
  57. * \brief qx_bool : boolean type with code and description message when an error occured
  58. */
  59. class QxBool
  60. {
  61. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  62. friend class boost::serialization::access;
  63. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  64. friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::QxBool & t);
  65. friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxBool & t);
  66. private:
  67. bool m_bValue; //!< Data boolean value
  68. long m_lCode; //!< Error code when value is false
  69. QString m_sDesc; //!< Error description when value is false
  70. public:
  71. QxBool() : m_bValue(false), m_lCode(0) { ; }
  72. QxBool(bool b) : m_bValue(b), m_lCode(0) { qAssert(checkInitialized(b)); }
  73. QxBool(long lCode, const QString & sDesc) : m_bValue(false), m_lCode(lCode), m_sDesc(sDesc) { ; }
  74. QxBool(bool bValue, long lCode, const QString & sDesc) : m_bValue(bValue), m_lCode(lCode), m_sDesc(sDesc) { qAssert(checkInitialized(bValue)); }
  75. QxBool(const QxBool & other) : m_bValue(other.getValue()), m_lCode(other.getCode()), m_sDesc(other.getDesc()) { ; }
  76. ~QxBool() { ; }
  77. inline bool getValue() const { return m_bValue; }
  78. inline long getCode() const { return m_lCode; }
  79. inline QString getDesc() const { return m_sDesc; }
  80. inline void setValue(bool bValue) { m_bValue = bValue; qAssert(checkInitialized(bValue)); }
  81. inline void setCode(long lCode) { m_lCode = lCode; }
  82. inline void setDesc(const QString & sDesc) { m_sDesc = sDesc; }
  83. inline QxBool & operator=(const QxBool & other) { m_bValue = other.getValue(); m_lCode = other.getCode(); m_sDesc = other.getDesc(); return (* this); }
  84. inline QxBool & operator=(const bool b) { m_bValue = b; qAssert(checkInitialized(b)); return (* this); }
  85. inline operator bool() const { return (m_bValue != false); }
  86. inline bool operator!() const { return (m_bValue == false); }
  87. inline bool operator==(const QxBool & other) const { return ((m_bValue == other.getValue()) && (m_lCode == other.getCode()) && (m_sDesc == other.getDesc())); }
  88. inline bool operator==(const bool b) const { qAssert(checkInitialized(b)); return (m_bValue == b); }
  89. inline bool operator!=(const QxBool & other) const { return ((m_bValue != other.getValue()) || (m_lCode != other.getCode()) || (m_sDesc != other.getDesc())); }
  90. inline bool operator!=(const bool b) const { qAssert(checkInitialized(b)); return (m_bValue != b); }
  91. inline bool operator&&(const QxBool & other) const { return (m_bValue && other.getValue()); }
  92. inline bool operator&&(const bool b) const { qAssert(checkInitialized(b)); return (m_bValue && b); }
  93. inline bool operator||(const QxBool & other) const { return (m_bValue || other.getValue()); }
  94. inline bool operator||(const bool b) const { qAssert(checkInitialized(b)); return (m_bValue || b); }
  95. QString toString() const { return (QString(m_bValue ? "1" : "0") + "|" + QString::number(static_cast<qlonglong>(m_lCode)) + "|" + m_sDesc); }
  96. void fromString(const QString & s)
  97. {
  98. if (s.trimmed().isEmpty()) { (* this) = QxBool(); return; }
  99. bool bValue = s.startsWith("1");
  100. int iPos = s.indexOf("|", 2);
  101. if (iPos == -1) { (* this) = QxBool(bValue); return; }
  102. long lCode = s.mid(2, (iPos - 2)).toLong();
  103. QString sDesc = s.right(s.size() - (iPos + 1));
  104. (* this) = QxBool(bValue, lCode, sDesc);
  105. }
  106. private:
  107. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  108. template <class Archive>
  109. void serialize(Archive & ar, const unsigned int file_version)
  110. {
  111. Q_UNUSED(file_version);
  112. ar & boost::serialization::make_nvp("value", m_bValue);
  113. ar & boost::serialization::make_nvp("code", m_lCode);
  114. ar & boost::serialization::make_nvp("desc", m_sDesc);
  115. }
  116. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  117. inline bool checkInitialized(const bool b) const { return ((static_cast<int>(b) == 0) || (static_cast<int>(b) == 1)); }
  118. };
  119. } // namespace qx
  120. typedef qx::QxBool qx_bool;
  121. QX_REGISTER_CLASS_NAME(qx_bool)
  122. #endif // _QX_BOOL_H_