variant.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #ifndef VARIANT_H
  8. #define VARIANT_H
  9. // clazy:excludeall=const-signal-or-slot
  10. #include <QObject>
  11. #include <QVariant>
  12. #include "cppwebframework_global.h"
  13. CWF_BEGIN_NAMESPACE
  14. /**
  15. * @brief This class is designed to facilitate the passing of simple type parameters such as
  16. * qlonglong, double, int, and QString to the CSTL (C++ Server Pages Standard Tags Library).
  17. */
  18. class CPPWEBFRAMEWORKSHARED_EXPORT Variant : public QObject
  19. {
  20. Q_OBJECT
  21. QVariant variant;
  22. public:
  23. /**
  24. * @brief Constructs an invalid variant.
  25. */
  26. Variant() = default;
  27. /**
  28. * @brief Constructs a new variant with an integer value.
  29. */
  30. explicit Variant(int value)
  31. : variant(value)
  32. {}
  33. /**
  34. * @brief Constructs a new variant with an double value.
  35. */
  36. explicit Variant(double value)
  37. : variant(value)
  38. {}
  39. /**
  40. * @brief Constructs a new variant with an qlonglong value.
  41. */
  42. explicit Variant(qlonglong value)
  43. : variant(value)
  44. {}
  45. /**
  46. * @brief Constructs a new variant with an QString value.
  47. */
  48. explicit Variant(const QString &value)
  49. : variant(value)
  50. {}
  51. /**
  52. * @brief Constructs a new variant and converts to QString.
  53. */
  54. explicit Variant(const QByteArray &value)
  55. : variant(QString(value))
  56. {}
  57. public slots:
  58. /**
  59. * @brief Returns the variant as an int if the variant has userType() QMetaType::Int, QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::Double, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.
  60. * @warning: If the value is convertible to a QMetaType::LongLong but is too large to be represented in an int, the resulting arithmetic overflow will not be reflected in ok. A simple workaround is to use QString::toInt()
  61. * @param If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.
  62. */
  63. inline int toInt(bool *ok = nullptr) const noexcept { return variant.toInt(ok); }
  64. /**
  65. * @brief Constructs a new variant with an int value.
  66. */
  67. inline void setInt(int value) noexcept { variant = value; }
  68. /**
  69. * @brief Returns the variant as a double if the variant has userType() QMetaType::Double, QMetaType::Float, QMetaType::Bool, QMetaType::QByteArray, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.0.
  70. * @param If ok is non-null: *ok is set to true if the value could be converted to a double; otherwise *ok is set to false.
  71. */
  72. inline double toDouble(bool *ok = nullptr) const noexcept { return variant.toDouble(ok); }
  73. /**
  74. * @brief Constructs a new variant with an double value.
  75. */
  76. inline void setDouble(double value) noexcept { variant = value; }
  77. /**
  78. * @brief Returns the variant as a long long int if the variant has userType() QMetaType::LongLong, QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::Double, QMetaType::Int, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.
  79. * @param If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.
  80. */
  81. inline qlonglong toLongLong(bool *ok = nullptr) const noexcept
  82. {
  83. return variant.toLongLong(ok);
  84. }
  85. /**
  86. * @brief Constructs a new variant with an qlonglong value.
  87. */
  88. inline void setLongLong(qlonglong value) noexcept { variant = value; }
  89. /**
  90. * @brief Returns the variant as a QString if the variant has userType() QMetaType::QString, QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::QDate, QMetaType::QDateTime, QMetaType::Double, QMetaType::Int, QMetaType::LongLong, QMetaType::QStringList, QMetaType::QTime, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns an empty string.
  91. */
  92. inline QString toString() const noexcept { return variant.toString(); }
  93. /**
  94. * @brief Constructs a new variant with an QString value.
  95. */
  96. inline void setString(const QString &value) noexcept { variant = value; }
  97. };
  98. CWF_END_NAMESPACE
  99. #endif // VARIANT_H