C++ Web Framework  3.0
variant.h
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 
8 #ifndef VARIANT_H
9 #define VARIANT_H
10 
11 #include <QObject>
12 #include <QVariant>
13 #include "cppwebframework_global.h"
14 
15 CWF_BEGIN_NAMESPACE
20 class CPPWEBFRAMEWORKSHARED_EXPORT Variant : public QObject
21 {
22  Q_OBJECT
23  QVariant variant;
24 public:
28  Variant() = default;
32  explicit Variant(int value) : variant(value) {}
36  explicit Variant(double value) : variant(value) {}
40  explicit Variant(qlonglong value) : variant(value) {}
44  explicit Variant(const QString &value) : variant(value) {}
48  explicit Variant(const QByteArray &value) : variant(QString(value)) {}
49 public slots:
55  inline int toInt(bool *ok = nullptr) const noexcept { return variant.toInt(ok); }
59  inline void setInt(int value) noexcept { variant = value; }
64  inline double toDouble(bool *ok = nullptr) const noexcept { return variant.toDouble(ok); }
68  inline void setDouble(double value) noexcept { variant = value; }
73  inline qlonglong toLongLong(bool *ok = nullptr) const noexcept { return variant.toLongLong(ok); }
77  inline void setLongLong(qlonglong value) noexcept { variant = value; }
81  inline QString toString() const noexcept { return variant.toString(); }
85  inline void setString(const QString &value) noexcept { variant = value; }
86 };
87 
88 CWF_END_NAMESPACE
89 
90 #endif // VARIANT_H
Variant(int value)
Constructs a new variant with an integer value.
Definition: variant.h:32
Variant(double value)
Constructs a new variant with an double value.
Definition: variant.h:36
This class is designed to facilitate the passing of simple type parameters such as qlonglong...
Definition: variant.h:20
Variant(const QString &value)
Constructs a new variant with an QString value.
Definition: variant.h:44
void setDouble(double value) noexcept
Constructs a new variant with an double value.
Definition: variant.h:68
void setString(const QString &value) noexcept
Constructs a new variant with an QString value.
Definition: variant.h:85
double toDouble(bool *ok=nullptr) const noexcept
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.
Definition: variant.h:64
QString toString() const noexcept
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.
Definition: variant.h:81
void setInt(int value) noexcept
Constructs a new variant with an int value.
Definition: variant.h:59
int toInt(bool *ok=nullptr) const noexcept
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.
Definition: variant.h:55
Variant(const QByteArray &value)
Constructs a new variant and converts to QString.
Definition: variant.h:48
void setLongLong(qlonglong value) noexcept
Constructs a new variant with an qlonglong value.
Definition: variant.h:77
qlonglong toLongLong(bool *ok=nullptr) const noexcept
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.
Definition: variant.h:73
Variant(qlonglong value)
Constructs a new variant with an qlonglong value.
Definition: variant.h:40