EHM DAL 0.2.5
Data abstraction layer for Eastside Hockey Manager
Loading...
Searching...
No Matches
container.h
1#pragma once
2
3// Application headers
4#include "include/database/database_version.h"
5
6// Qt headers
7#include <QDataStream>
8#include <execution>
9#include <type_traits>
10
11namespace ehm_dal::container {
12template<typename T_ItemCountType>
13concept Number = std::is_integral<T_ItemCountType>::value;
18template<typename T_DataType, Number T_ItemCountType = quint8>
20{
21public:
22 Container(const quint32 size = 0, const bool is_sortable = true);
23 Container(const Container &obj);
24 virtual ~Container() {}
25
26 // Add data
27 void addRows(const qint32 count);
28 inline void push_back(T_DataType &item) { data_.push_back(std::move(item)); }
29 inline void push_back(T_DataType *item) { data_.push_back(std::move(*item)); }
30
31 // Erase data
32 void clear() { data_.clear(); }
33 void erase(const qint32 first_index_to_delete, const qint32 last_index_to_delete);
34
35 // File i/o
36 virtual void read(QDataStream &in, ehm_dal::database::DatabaseVersion &version);
37 void read(QDataStream &in,
39 const T_ItemCountType count);
40 void readInto(QDataStream &in, ehm_dal::database::DatabaseVersion &version);
41 virtual void write(QDataStream &out, ehm_dal::database::DatabaseVersion &version);
42 void write(QDataStream &out,
44 const T_ItemCountType count);
45
46 // Get data - attributes
47 inline bool isSortable() const { return is_sortable_; }
48 inline qint32 rowCount() const { return static_cast<qint32>(data_.size()); }
49 inline T_ItemCountType size() const { return static_cast<T_ItemCountType>(data_.size()); }
50
51 // Get data - values
52 T_DataType *item(const T_ItemCountType pos);
53 const T_DataType *itemConst(const T_ItemCountType pos) const;
54
55 // Operator overloading
56 Container &operator=(Container rhs);
57
58protected:
59 inline std::vector<T_DataType> *data() { return &data_; }
60
61private:
62 // Database
63 std::vector<T_DataType> data_;
64
65 // Sorting
66 const bool is_sortable_;
67};
68
69/* ================== */
70/* Container */
71/* ================== */
72
73// --- Default constructor --- //
74template<typename T_DataType, Number T_ItemCountType>
75Container<T_DataType, T_ItemCountType>::Container(const quint32 size, const bool is_sortable)
76 : data_(size)
77 , is_sortable_(is_sortable)
78{}
79
80// --- Copy constructor --- //
81template<typename T_DataType, Number T_ItemCountType>
82Container<T_DataType, T_ItemCountType>::Container(const Container &obj)
83 : data_(std::move(obj.data_))
84 , is_sortable_(obj.isSortable())
85{
86 // NOTE check std::move does not cause a crash - this probably shouldn't be move
87 // (seeing as this is a copy constructor)
88}
89
90/* ================= */
91/* Add Data */
92/* ================= */
93
94// --- Add blank items --- //
95template<typename T_DataType, Number T_ItemCountType>
96void Container<T_DataType, T_ItemCountType>::addRows(const qint32 count)
97{
98 for (auto i = 0; i < count; ++i) {
99 T_DataType new_item;
100 data_.push_back(std::move(new_item));
101 }
102}
103
104/* =================== */
105/* Erase Data */
106/* =================== */
107
108// --- Remove/delete rows --- //
109template<typename T_DataType, Number T_ItemCountType>
110void Container<T_DataType, T_ItemCountType>::erase(const qint32 first_index_to_delete,
111 const qint32 last_index_to_delete)
112{
113 data_.erase(data_.begin() + first_index_to_delete, data_.begin() + last_index_to_delete);
114}
115
116/* ================= */
117/* File I/O */
118/* ================= */
119
120// --- Read all data --- //
121template<typename T_DataType, Number T_ItemCountType>
122void Container<T_DataType, T_ItemCountType>::read(QDataStream &in,
124{
125 T_ItemCountType count{0};
126 in.readRawData(reinterpret_cast<char *>(&count), sizeof(T_ItemCountType));
127
128 read(in, version, count);
129}
130
131// --- Read the specified number of data items --- //
132template<typename T_DataType, Number T_ItemCountType>
133void Container<T_DataType, T_ItemCountType>::read(QDataStream &in,
135 const T_ItemCountType count)
136{
137 for (T_ItemCountType i = 0; i < count; ++i) {
138 T_DataType item;
139 item.read(in, version);
140 data_.emplace_back(item);
141 }
142
143 if (isSortable())
144 std::sort(std::execution::par, this->data_.begin(), this->data_.end());
145}
146
147// --- Read the specified the data into the existing elements of the container --- //
148template<typename T_DataType, Number T_ItemCountType>
149void Container<T_DataType, T_ItemCountType>::readInto(QDataStream &in,
151{
152 for (auto &itr : data_)
153 itr.read(in, version);
154
155 if (isSortable())
156 std::sort(std::execution::par, this->data_.begin(), this->data_.end());
157}
158
159// --- Write all data --- //
160template<typename T_DataType, Number T_ItemCountType>
161void Container<T_DataType, T_ItemCountType>::write(QDataStream &out,
163{
164 auto count{size()};
165 out.writeRawData(reinterpret_cast<char *>(&count), sizeof(T_ItemCountType));
166
167 write(out, version, count);
168}
169
170// --- Write the specified number of data items --- //
171template<typename T_DataType, Number T_ItemCountType>
172void Container<T_DataType, T_ItemCountType>::write(QDataStream &out,
174 const T_ItemCountType count)
175{
176 if (isSortable())
177 std::sort(std::execution::par, this->data_.begin(), this->data_.end());
178
179 for (T_ItemCountType i = 0; i < count; ++i)
180 this->data_[i].write(out, version);
181}
182
183/* ========================== */
184/* Get Data - Values */
185/* ========================== */
186
187// --- Get item --- //
188template<typename T_DataType, Number T_ItemCountType>
189T_DataType *Container<T_DataType, T_ItemCountType>::item(const T_ItemCountType pos)
190{
191 if (pos < size() && pos >= 0)
192 return &data_[pos];
193
194 return nullptr;
195}
196
197// --- Get item --- //
198template<typename T_DataType, Number T_ItemCountType>
199const T_DataType *Container<T_DataType, T_ItemCountType>::itemConst(const T_ItemCountType pos) const
200{
201 if (pos < rowCount() && pos >= 0)
202 return &data_[pos];
203
204 return nullptr;
205}
206
207/* ============================= */
208/* Operator Overloading */
209/* ============================= */
210
211// --- Assignment operator --- //
212template<typename T_DataType, Number T_ItemCountType>
213Container<T_DataType, T_ItemCountType> &Container<T_DataType, T_ItemCountType>::operator=(
214 Container rhs)
215{
216 data_ = rhs.data_;
217 return *this;
218}
219} // namespace ehm_dal::container
The Container class is a representation of vector in the EHM database which is written to the databas...
Definition: container.h:20
The DatabaseVersion class represents the database version number.
Definition: database_version.h:12
Definition: container.h:13