EHM DAL 0.2.5
Data abstraction layer for Eastside Hockey Manager
Loading...
Searching...
No Matches
abstract_pointer.h
1#pragma once
2
3// Application headers
4#include "include/database/pointer/pointer_flags.h"
5
6// Application headers
7#include "include/database/database_version.h"
8
9// Qt headers
10#include <QDataStream>
11#include <QVariant>
12
13namespace ehm_dal::data_types {
14// TODO add sanity checks when getting data in case the id is -1
15// TODO consider merging Flag and Pointer into a single class. This should be possible if FlagTable derives from AbstractTable.
16// --- Abstract pointer base class --- //
20template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
22{
23public:
30 AbstractPointer(const T_TableIndex table_id,
31 const T_IdDataType record_id = PointerFlags::NULL_POINTER);
32
33 // File i/o
34 void read(QDataStream &in, ehm_dal::database::DatabaseVersion &version)
35 {
36 Q_UNUSED(version)
37 in >> *this;
38 }
39 void write(QDataStream &out, ehm_dal::database::DatabaseVersion &version)
40 {
41 Q_UNUSED(version)
42 out << *this;
43 }
44 friend QDataStream &operator>>(QDataStream &in, AbstractPointer &data)
45 {
46 in.readRawData(reinterpret_cast<char *>(&data.id_), sizeof(T_IdDataType));
47 return in;
48 }
49 friend QDataStream &operator<<(QDataStream &out, const AbstractPointer &data)
50 {
51 auto id{data.id()};
52 out.writeRawData(reinterpret_cast<char *>(&id), sizeof(T_IdDataType));
53 return out;
54 }
55
56 // Get data - attributes
60 qint32 columnCount() const { return constTable()->columnCount(); }
64 inline bool isNone() const { return !isPointer(); }
68 inline bool isPointer() const { return id() > PointerFlags::NULL_POINTER; }
72 inline bool isValidPointer() const { return isValidTable() && isPointer(); }
76 inline bool isValidTable() const
77 {
78 return table_id_ != static_cast<T_TableIndex>(PointerFlags::NULL_POINTER);
79 }
80
81 // Get data - pointer
88 QVariant data(const qint32 role) const;
96 virtual QVariant data(const qint32 column, const qint32 role) const;
100 inline virtual T_IdDataType id() const { return id_; }
107 inline T_IdDataType idBuffer() const { return id_; }
114 inline T_IdDataType parentItemId() const { return idBuffer(); }
118 virtual QString text() const;
119
120 // Get data - table
124 QString description() const;
128 virtual std::shared_ptr<T_Table> constTable() const = 0;
132 virtual std::shared_ptr<T_Table> table() = 0;
136 T_TableIndex tableId() const { return table_id_; }
137
138 // Operator overloading: Comparators
139 inline auto operator<=>(const AbstractPointer &rhs) const { return id() <=> rhs.id(); }
140 inline bool operator==(const AbstractPointer &rhs) const { return id() == rhs.id(); }
141 inline auto operator<=>(const qint32 &rhs) const { return id() <=> rhs; }
142 inline bool operator==(const qint32 &rhs) const { return id() == rhs; }
143
144 // Set data - pointer
145 // TODO deprecate set() in favour of setData()
146 Q_DECL_DEPRECATED void set(const QVariant &value, const qint32 role)
147 {
148 return setData(value, role);
149 }
155 virtual void setData(const QVariant &value, const qint32 role);
163 bool setData(const qint32 column, const QVariant &value, const qint32 role);
168 inline void setTable(const T_TableIndex table_id) { table_id_ = table_id; }
169
170protected:
171 T_IdDataType id_{static_cast<T_IdDataType>(PointerFlags::NULL_POINTER)}; // Record id
172 T_TableIndex table_id_{static_cast<T_TableIndex>(PointerFlags::NULL_POINTER)}; // Table id
173
174 inline void setId(const qint32 id) { id_ = static_cast<T_IdDataType>(id); }
175};
176
177/* ============================ */
178/* Pointer Base Class */
179/* ============================ */
180
181template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
182AbstractPointer<T_IdDataType, T_TableIndex, T_Table>::AbstractPointer()
183{}
184
185template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
187 const T_IdDataType record_id)
188 : id_(record_id)
189 , table_id_(table_id)
190{}
191
192/* =========================== */
193/* Get Data - Pointer */
194/* =========================== */
195
196template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
198{
199 switch (role) {
200 case Qt::DisplayRole:
201 return text();
202 case Qt::EditRole:
203 return id();
204 default:
205 return QVariant();
206 }
207}
208
209template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
211 qint32 role) const
212{
213 return constTable()->index(id(), column).data(role);
214}
215
216template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
218{
219 return constTable()->index(id(), 2).data(Qt::DisplayRole).toString();
220}
221
222/* ========================= */
223/* Get Data - Table */
224/* ========================= */
225
226// --- Get table name --- //
227template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
229{
230 if (tableId() != static_cast<T_TableIndex>(PointerFlags::NULL_POINTER))
231 return constTable()->attributes()->name();
232
233 return QStringLiteral("[No table]");
234}
235
236/* =========================== */
237/* Set Data - Pointer */
238/* =========================== */
239
240// --- Sets the pointer id value --- //
241template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
243 const qint32 role)
244{
245 if (role == Qt::EditRole)
246 id_ = static_cast<T_IdDataType>(value.toInt());
247}
248
249// --- Edits the underlying item pointed to --- //
250template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
252 const QVariant &value,
253 const qint32 role)
254{
255 const auto index{table()->index(id(), column)};
256 return table()->setData(index, value, role);
257}
258} // namespace ehm_dal::data_types
259
260
The AbstractPointer class represents a pointer to a table item.
Definition: abstract_pointer.h:22
virtual void setData(const QVariant &value, const qint32 role)
Sets the role data for the item at index to value.
Definition: abstract_pointer.h:242
void setTable(const T_TableIndex table_id)
Sets the table index to table_id.
Definition: abstract_pointer.h:168
virtual std::shared_ptr< T_Table > constTable() const =0
Returns a read-only pointer to the table pointed to.
QVariant data(const qint32 role) const
Returns the data stored under the given role. Qt::DisplayRole returns the item's display text and Qt:...
Definition: abstract_pointer.h:197
bool isValidTable() const
Returns whether or not the AbstractPointer has been set to a table.
Definition: abstract_pointer.h:76
virtual QString text() const
Returns the display text of the item.
Definition: abstract_pointer.h:217
virtual std::shared_ptr< T_Table > table()=0
Returns a pointer to the table pointed to.
virtual T_IdDataType id() const
Returns the item index pointed to.
Definition: abstract_pointer.h:100
bool isPointer() const
Returns whether or not the AbstractPointer has been set to an item index.
Definition: abstract_pointer.h:68
qint32 columnCount() const
Returns the number of columns in the table pointed to.
Definition: abstract_pointer.h:60
T_TableIndex tableId() const
Returns the table id of the table pointed to.
Definition: abstract_pointer.h:136
bool isValidPointer() const
Returns whether or not the AbstractPointer has been set to an item index and to a table.
Definition: abstract_pointer.h:72
QString description() const
Returns the table name of the item.
Definition: abstract_pointer.h:228
T_IdDataType parentItemId() const
parentItemId returns the buffered id which is generally only useful when first initialising the links...
Definition: abstract_pointer.h:114
T_IdDataType idBuffer() const
idBuffer returns the buffered id which is generally only useful when first initialising the links bet...
Definition: abstract_pointer.h:107
bool isNone() const
Returns whether or not the AbstractPointer is set to ehm_dal::data_types::PointerFlags::NULL_POINTER.
Definition: abstract_pointer.h:64
The DatabaseVersion class represents the database version number.
Definition: database_version.h:12
namespace ehm_dal::data_types
Definition: attribute.h:6
@ NULL_POINTER
Definition: pointer_flags.h:9