Browse Source

refactor(strongtype): Add property types to strong types

reviewable/pr5692/r2
Mick Sayson 6 years ago
parent
commit
15b9f9ff54
  1. 2
      src/core/receiptnum.h
  2. 2
      src/persistence/db/rawdatabase.h
  3. 45
      src/util/strongtype.h

2
src/core/receiptnum.h

@ -25,7 +25,7 @@ @@ -25,7 +25,7 @@
#include <QMetaType>
#include <cstdint>
using ReceiptNum = NamedType<uint32_t, struct ReceiptNumTag>;
using ReceiptNum = NamedType<uint32_t, struct ReceiptNumTag, Orderable>;
Q_DECLARE_METATYPE(ReceiptNum);
#endif /* RECEIPT_NUM_H */

2
src/persistence/db/rawdatabase.h

@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
#include <sqlite3.h>
using RowId = NamedType<int64_t, struct RowIdTag>;
using RowId = NamedType<int64_t, struct RowIdTag, Orderable>;
Q_DECLARE_METATYPE(RowId);
class RawDatabase : QObject

45
src/util/strongtype.h

@ -22,6 +22,38 @@ @@ -22,6 +22,38 @@
#include <QHash>
template <typename T>
struct Addable
{
T operator+(T const& other) const { return static_cast<T const&>(*this).get() + other.get(); };
};
template <typename T>
struct EqualityComparible
{
bool operator==(const T& other) const { return static_cast<T const&>(*this).get() == other.get(); };
};
template <typename T>
struct Hashable
{
friend uint qHash(const Hashable<T>& key, uint seed = 0)
{
return qHash(static_cast<T const&>(*key).get(), seed);
}
};
template <typename T>
struct Orderable : EqualityComparible<T>
{
bool operator<(const T& rhs) const { return static_cast<T const&>(*this).get() < rhs.get(); }
bool operator>(const T& rhs) const { return static_cast<T const&>(*this).get() > rhs.get(); }
bool operator>=(const T& rhs) const { return static_cast<T const&>(*this).get() >= rhs.get(); }
bool operator<=(const T& rhs) const { return static_cast<T const&>(*this).get() <= rhs.get(); }
};
/* This class facilitates creating a named class which wraps underlying POD,
* avoiding implict casts and arithmetic of the underlying data.
* Usage: Declare named type with arbitrary tag, then hook up Qt metatype for use
@ -32,24 +64,21 @@ @@ -32,24 +64,21 @@
* qRegisterMetaType<ReceiptNum>();
*/
template <typename T, typename Parameter>
class NamedType
template <typename T, typename Tag, template <typename> class... Properties>
class NamedType : public Properties<NamedType<T, Tag, Properties...>>...
{
public:
NamedType() {}
explicit NamedType(T const& value) : value_(value) {}
T& get() { return value_; }
T const& get() const {return value_; }
bool operator==(const NamedType& rhs) const { return value_ == rhs.value_; }
bool operator<(const NamedType& rhs) const { return value_ < rhs.value_; }
bool operator>(const NamedType& rhs) const { return value_ > rhs.value_; }
private:
T value_;
};
template <typename T, typename Parameter>
inline uint qHash(const NamedType<T,Parameter> &key, uint seed = 0) {
template <typename T, typename Tag, template <typename> class... Properties>
uint qHash(const NamedType<T, Tag, Properties...>& key, uint seed = 0)
{
return qHash(key.get(), seed);
}
#endif // STORNGTYPE_H

Loading…
Cancel
Save