STAR++ v0.2.2
C++ implementation of STAR voting
Loading...
Searching...
No Matches
ballotbox_p.h
1#ifndef BALLOTBOX_P_H
2#define BALLOTBOX_P_H
3
4// Qt Includes
5#include <QString>
6#include <QList>
7#include <QDate>
8#include <QFile>
9
10// Qx Includes
11#include <qx/core/qx-abstracterror.h>
12
13namespace Star
14{
17// Project Forward Declarations
18class RefCategoryConfig;
19
20struct RefCategory
21{
22 QString name;
23 QStringList candidates;
24};
25
26struct RefBallot
27{
28 QString voter;
29 QDate submissionDate;
30 QList<QList<int>> votes;
31};
32
33class QX_ERROR_TYPE(RefBallotBoxError, "Star::RefBallotBoxError", 1150)
34{
35 friend class RefBallotBox;
36//-Class Enums-------------------------------------------------------------
37public:
38 enum Type
39 {
40 NoError,
41 InvalidRowCount,
42 InvalidColumnCount,
43 InvalidDate,
44 Empty,
45 BlankValue,
46 InvalidVote,
47 DuplicateCandidate,
48 IoError
49 };
50
51//-Class Variables-------------------------------------------------------------
52private:
53 static inline const QString MAIN_ERR_MSG = u"Error reading the ballot box."_s;
54 static inline const QHash<Type, QString> ERR_STRINGS{
55 {NoError, u""_s},
56 {InvalidRowCount, u"The ballot box has less than the minimum required row count (headings + 2 voters)."_s},
57 {InvalidColumnCount, u"The ballot box has a different number of columns than specified by the category configuration (%1 vs %2)."_s},
58 {InvalidDate, u"The format of a submission date was invalid."_s},
59 {Empty, u"The provided file contains no ballots."_s},
60 {BlankValue, u"A field expected to have a value was blank (r: %1, c: %2)."_s},
61 {InvalidVote, u"A vote value was not a valid unsigned integer between 0 and 5 (r: %1, c: %2)."_s},
62 {DuplicateCandidate, u"The ballot box contained duplicate candidates within the same category."_s},
63 {IoError, u"IO Error: %1"_s}
64 };
65
66//-Instance Variables-------------------------------------------------------------
67private:
68 Type mType;
69 QString mString;
70
71//-Constructor-------------------------------------------------------------
72private:
73 RefBallotBoxError(Type t = NoError);
74
75 template<typename... Strings>
76 requires (std::same_as<Strings, QString> && ...)
77 RefBallotBoxError(Type t, Strings... args) :
78 RefBallotBoxError(t)
79 {
80 mString = mString.arg(args...);
81 }
82
83 template<typename... Ints>
84 requires (std::integral<Ints> && ...)
85 RefBallotBoxError(Type t, Ints... args) :
86 RefBallotBoxError(t)
87 {
88 auto sub = [this](auto integer) { mString = mString.arg(integer); };
89 (sub(args), ...);
90 }
91
92//-Instance Functions-------------------------------------------------------------
93public:
94 bool isValid() const;
95 Type type() const;
96 QString string() const;
97
98private:
99 Qx::Severity deriveSeverity() const override;
100 quint32 deriveValue() const override;
101 QString derivePrimary() const override;
102 QString deriveSecondary() const override;
103};
104
105class RefBallotBox
106{
107//-Inner Classes----------------------------------------------------------------------------------------------------
108public:
109 class Reader;
110
111//-Instance Variables--------------------------------------------------------------------------------------------------
112private:
113 QList<RefCategory> mCategories;
114 QList<RefBallot> mBallots;
115
116//-Constructor--------------------------------------------------------------------------------------------------------
117public:
118 RefBallotBox();
119
120//-Instance Functions-------------------------------------------------------------------------------------------------
121public:
122 const QList<RefCategory>& categories() const;
123 const QList<RefBallot>& ballots() const;
124};
125
126class RefBallotBox::Reader
127{
128//-Class Variables--------------------------------------------------------------------------------------------------
129private:
130 // Fields
131 static const int STATIC_FIELD_COUNT = 2;
132 static const int SUBMISSION_DATE_INDEX = 0;
133 static const int MEMBER_NAME_INDEX = 1;
134
135//-Instance Variables--------------------------------------------------------------------------------------------------
136private:
137 RefBallotBox* mTargetBox;
138 QFile mCsvFile;
139 const RefCategoryConfig* mCategoryConfig;
140 qsizetype mExpectedFieldCount;
141
142//-Constructor--------------------------------------------------------------------------------------------------------
143public:
144 Reader(RefBallotBox* targetBox, const QString& filePath, const RefCategoryConfig* categoryConfig);
145
146//-Instance Functions-------------------------------------------------------------------------------------------------
147private:
148 RefBallotBoxError parseCategories(const QList<QVariant>& headingsRow);
149 RefBallotBoxError parseBallot(const QList<QVariant>& ballotRow, qsizetype ballotNum);
150
151public:
152 RefBallotBoxError readInto();
153};
155}
156
157#endif //BALLOTBOX_P_H
The Star namespace is the main namespace through which all functionality of the STAR++ library is acc...
Definition calculator.cpp:21