4#include "include/database/database_version.h"
14concept Number = std::is_integral<T>::value || std::is_floating_point<T>::value;
27 template<Number T_CustomType>
28 void readCustomType(QDataStream &in);
29 template<Number T_CustomType>
30 void writeCustomType(QDataStream &out);
41 QVariant
data(
const qint32 role = Qt::DisplayRole)
const;
45 inline T
value()
const {
return value_; }
71 inline auto operator<=>(
const qint32 &rhs)
const {
return value() <=> rhs; }
72 inline bool operator==(
const qint32 &rhs)
const {
return value() == rhs; }
73 inline bool operator==(
const bool &rhs)
const {
return static_cast<bool>(
value()) == rhs; }
76 Q_DECL_DEPRECATED
void set(
const QVariant &
value,
const qint32 role = Qt::EditRole);
83 void setData(
const QVariant &
value,
const qint32 role = Qt::EditRole);
96 in.readRawData(
reinterpret_cast<char *
>(&
data.value_),
sizeof(T));
99 friend QDataStream &operator<<(QDataStream &out,
const DataType<T> &
data)
101 out.writeRawData(
reinterpret_cast<const char *
>(&
data.value_),
sizeof(T));
111DataType<T>::DataType(
const T value)
121inline bool DataType<T>::read(QDataStream &in, database::DatabaseVersion &version)
130inline bool DataType<T>::write(QDataStream &out, database::DatabaseVersion &version)
139template<Number T_CustomType>
140inline void DataType<T>::readCustomType(QDataStream &in)
143 in.readRawData(
reinterpret_cast<char *
>(&buffer),
sizeof(T_CustomType));
144 value_ =
static_cast<T
>(buffer);
149template<Number T_CustomType>
150inline void DataType<T>::writeCustomType(QDataStream &out)
152 T_CustomType buffer{
static_cast<T_CustomType
>(value_)};
153 out.writeRawData(
reinterpret_cast<const char *
>(&buffer),
sizeof(T_CustomType));
164 case Qt::DisplayRole:
179 if (role == Qt::DisplayRole || role == Qt::EditRole)
180 return QLocale().toString(value_);
188 if (role != Qt::DisplayRole && role != Qt::EditRole)
191 if constexpr (std::is_floating_point_v<T>)
192 return QLocale().toString(value_,
'g', 8);
194 return QString::number(0.0);
200 return static_cast<double>(value_);
206 return static_cast<float>(value_);
217 setData(value, role);
223 if (role != Qt::EditRole)
226 if constexpr (std::is_floating_point_v<T>)
227 setValue(
static_cast<T
>(value.toDouble()));
228 else if constexpr (std::is_unsigned_v<T>)
229 setValue(
static_cast<T
>(value.toUInt()));
231 setValue(
static_cast<T
>(value.toInt()));
The DataType class is a template class for all C++ integral and floating point data types.
Definition: data_type.h:21
double toDouble() const
Returns the value as a double.
Definition: data_type.h:198
QVariant accurateDecimal(const qint32 role=Qt::DisplayRole) const
Returns the decimal value to as many decimal places as possible.
Definition: data_type.h:177
float toFloat() const
Returns the value as a float.
Definition: data_type.h:204
QVariant roundedDecimal(const qint32 role=Qt::DisplayRole) const
Returns the decimal value rounded to 8 decimal places. This is useful when converting a double to a f...
Definition: data_type.h:186
QVariant data(const qint32 role=Qt::DisplayRole) const
Returns the data stored under the given role for the item.
Definition: data_type.h:161
T value() const
Returns the raw value which can otherwise be accessed via QVariant data(const qint32 role).
Definition: data_type.h:45
void setValue(const T value)
Sets the value to value
Definition: data_type.h:88
void setData(const QVariant &value, const qint32 role=Qt::EditRole)
Sets the role data for the item at to value.
Definition: data_type.h:221
The DatabaseVersion class represents the database version number.
Definition: database_version.h:12
Definition: data_type.h:14
namespace ehm_dal::data_types
Definition: attribute.h:6