EHM DAL 0.2.3
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// Qt headers
7#include <QDataStream>
8#include <QVariant>
9
10namespace ehm_dal::data_types {
11// TODO add sanity checks when getting data in case the id is -1
12// TODO consider merging Flag and Pointer into a single class. This should be possible if FlagTable derives from AbstractTable.
13// --- Abstract pointer base class --- //
17template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
19{
20public:
27 AbstractPointer(const T_TableIndex table_id,
28 const T_IdDataType record_id = PointerFlags::NULL_POINTER);
29
30 // File i/o
31 friend QDataStream &operator>>(QDataStream &in, AbstractPointer &data)
32 {
33 in.readRawData(reinterpret_cast<char *>(&data.id_), sizeof(T_IdDataType));
34 return in;
35 }
36
37 friend QDataStream &operator<<(QDataStream &out, const AbstractPointer &data)
38 {
39 auto id{data.id()};
40 out.writeRawData(reinterpret_cast<char *>(&id), sizeof(T_IdDataType));
41 return out;
42 }
43
44 // Get data - attributes
48 qint32 columnCount() const { return constTable()->columnCount(); }
52 inline bool isNone() const { return !isPointer(); }
56 inline bool isPointer() const { return id() > PointerFlags::NULL_POINTER; }
60 inline bool isValidPointer() const { return isValidTable() && isPointer(); }
64 inline bool isValidTable() const
65 {
66 return table_id_ != static_cast<T_TableIndex>(PointerFlags::NULL_POINTER);
67 }
68
69 // Get data - pointer
76 QVariant data(const qint32 role) const;
84 virtual QVariant data(const qint32 column, const qint32 role) const;
88 inline virtual T_IdDataType id() const { return id_; }
95 inline T_IdDataType idBuffer() const { return id_; }
102 inline T_IdDataType parentItemId() const { return idBuffer(); }
106 virtual QString text() const;
107
108 // Get data - table
112 QString description() const;
116 virtual std::shared_ptr<T_Table> constTable() const = 0;
120 virtual std::shared_ptr<T_Table> table() = 0;
124 T_TableIndex tableId() const { return table_id_; }
125
126 // Operator overloading: Comparators
127 inline auto operator<=>(const AbstractPointer &rhs) const { return id() <=> rhs.id(); }
128 inline bool operator==(const AbstractPointer &rhs) const { return id() == rhs.id(); }
129 inline auto operator<=>(const qint32 &rhs) const { return id() <=> rhs; }
130 inline bool operator==(const qint32 &rhs) const { return id() == rhs; }
131
132 // Set data - pointer
133 // TODO deprecate set() in favour of setData()
134 Q_DECL_DEPRECATED void set(const QVariant &value, const qint32 role)
135 {
136 return setData(value, role);
137 }
143 virtual void setData(const QVariant &value, const qint32 role);
151 bool setData(const qint32 column, const QVariant &value, const qint32 role);
152
153protected:
154 T_IdDataType id_{static_cast<T_IdDataType>(PointerFlags::NULL_POINTER)}; // Record id
155 T_TableIndex table_id_{static_cast<T_TableIndex>(PointerFlags::NULL_POINTER)}; // Table id
156
157 inline void setId(const qint32 id) { id_ = static_cast<T_IdDataType>(id); }
158};
159
160/* ============================ */
161/* Pointer Base Class */
162/* ============================ */
163
164template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
165AbstractPointer<T_IdDataType, T_TableIndex, T_Table>::AbstractPointer()
166{}
167
168template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
170 const T_IdDataType record_id)
171 : id_(record_id)
172 , table_id_(table_id)
173{}
174
175/* =========================== */
176/* Get Data - Pointer */
177/* =========================== */
178
179template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
181{
182 switch (role) {
183 case Qt::DisplayRole:
184 return text();
185 case Qt::EditRole:
186 return id();
187 default:
188 return QVariant();
189 }
190}
191
192template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
194 qint32 role) const
195{
196 return constTable()->index(id(), column).data(role);
197}
198
199template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
201{
202 return constTable()->index(id(), 2).data(Qt::DisplayRole).toString();
203}
204
205/* ========================= */
206/* Get Data - Table */
207/* ========================= */
208
209// --- Get table name --- //
210template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
212{
213 if (tableId() != static_cast<T_TableIndex>(PointerFlags::NULL_POINTER))
214 return constTable()->attributes()->name();
215
216 return QStringLiteral("[No table]");
217}
218
219/* =========================== */
220/* Set Data - Pointer */
221/* =========================== */
222
223// --- Sets the pointer id value --- //
224template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
226 const qint32 role)
227{
228 if (role == Qt::EditRole)
229 id_ = static_cast<T_IdDataType>(value.toInt());
230}
231
232// --- Edits the underlying item pointed to --- //
233template<typename T_IdDataType, typename T_TableIndex, typename T_Table>
235 const QVariant &value,
236 const qint32 role)
237{
238 const auto index{table()->index(id(), column)};
239 return table()->setData(index, value, role);
240}
241} // namespace ehm_dal::data_types
242
243
The AbstractPointer class represents a pointer to a table item.
Definition: abstract_pointer.h:19
virtual void setData(const QVariant &value, const qint32 role)
Sets the role data for the item at index to value.
Definition: abstract_pointer.h:225
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:180
bool isValidTable() const
Returns whether or not the AbstractPointer has been set to a table.
Definition: abstract_pointer.h:64
virtual QString text() const
Returns the display text of the item.
Definition: abstract_pointer.h:200
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:88
bool isPointer() const
Returns whether or not the AbstractPointer has been set to an item index.
Definition: abstract_pointer.h:56
qint32 columnCount() const
Returns the number of columns in the table pointed to.
Definition: abstract_pointer.h:48
T_TableIndex tableId() const
Returns the table id of the table pointed to.
Definition: abstract_pointer.h:124
bool isValidPointer() const
Returns whether or not the AbstractPointer has been set to an item index and to a table.
Definition: abstract_pointer.h:60
QString description() const
Returns the table name of the item.
Definition: abstract_pointer.h:211
T_IdDataType parentItemId() const
parentItemId returns the buffered id which is generally only useful when first initialising the links...
Definition: abstract_pointer.h:102
T_IdDataType idBuffer() const
idBuffer returns the buffered id which is generally only useful when first initialising the links bet...
Definition: abstract_pointer.h:95
bool isNone() const
Returns whether or not the AbstractPointer is set to ehm_dal::data_types::PointerFlags::NULL_POINTER.
Definition: abstract_pointer.h:52
namespace ehm_dal::data_types
Definition: attribute.h:6
@ NULL_POINTER
Definition: pointer_flags.h:9