STAR++ v0.2.2
C++ implementation of STAR voting
Loading...
Searching...
No Matches
categoryconfig_p.h
1#ifndef CATEGORYCONFIG_P_H
2#define CATEGORYCONFIG_P_H
3
4// Qt Includes
5#include <QString>
6#include <QList>
7
8// Qx Includes
9#include <qx/io/qx-textstreamreader.h>
10
11namespace Star
12{
15struct RefCategoryHeader
16{
17 QString name;
18 uint candidateCount;
19};
20
21class QX_ERROR_TYPE(RefCategoryConfigError, "Star::RefCategoryConfigError", 1152)
22{
23 friend class RefCategoryConfig;
24//-Class Enums-------------------------------------------------------------
25public:
26 enum Type
27 {
28 NoError,
29 DoesNotExist,
30 Empty,
31 IoError,
32 InvalidIni,
33 InvalidLayout,
34 InvalidValueType,
35 InvalidCategoryCount,
36 DuplicateCategory,
37 InvalidGeneralKey,
38 InvalidSeatCount,
39 NoCategories,
40 NoSeats
41 };
42
43//-Class Variables-------------------------------------------------------------
44private:
45 static inline const QString MAIN_ERR_MSG = u"Error reading the category configuration."_s;
46 static inline const QHash<Type, QString> ERR_STRINGS{
47 {NoError, u""_s},
48 {DoesNotExist, u"The specified file does not exist."_s},
49 {Empty, u"The provided file is empty."_s},
50 {IoError, u"IO Error: %1"_s},
51 {InvalidIni, u"The provided file is not a valid INI format file (Line: %1)."_s},
52 {InvalidLayout, u"The provided file does not follow the expected layout (Line: %1)."_s},
53 {InvalidValueType, u"Category configuration contains a value that is not an unsigned integer (Line: %1)."_s},
54 {InvalidCategoryCount, u"A category specified a candidate count less than 2 (Line: %1)."_s},
55 {DuplicateCategory, u"The provided file contains duplicate categories (Line: %1)."_s},
56 {InvalidGeneralKey, u"Unrecognized General key (Line: %1)."_s},
57 {InvalidSeatCount, u"The provided file specified a seat count less than 1 (Line: %1)."_s},
58 {NoCategories, u"The provided file contains no categories."_s},
59 {NoSeats, u"The provided file didn't specify a seat count."_s}
60 };
61
62//-Instance Variables-------------------------------------------------------------
63private:
64 Type mType;
65 QString mString;
66
67//-Constructor-------------------------------------------------------------
68private:
69 RefCategoryConfigError(Type t = NoError);
70
71 template<typename... Strings>
72 requires (std::same_as<Strings, QString> && ...)
73 RefCategoryConfigError(Type t, Strings... args) :
74 RefCategoryConfigError(t)
75 {
76 mString = mString.arg(args...);
77 }
78
79 template<typename... Ints>
80 requires (std::integral<Ints> && ...)
81 RefCategoryConfigError(Type t, Ints... args) :
82 RefCategoryConfigError(t)
83 {
84 auto sub = [this](auto integer) { mString = mString.arg(integer); };
85 (sub(args), ...);
86 }
87
88//-Instance Functions-------------------------------------------------------------
89public:
90 bool isValid() const;
91 Type type() const;
92 QString string() const;
93
94private:
95 Qx::Severity deriveSeverity() const override;
96 quint32 deriveValue() const override;
97 QString derivePrimary() const override;
98 QString deriveSecondary() const override;
99};
100
101class RefCategoryConfig
102{
103//-Inner Classes----------------------------------------------------------------------------------------------------
104public:
105 class Reader;
106
107//-Class Variables--------------------------------------------------------------------------------------------------
108private:
109 static inline const QString SECTION_HEADING_CATEGORIES = QStringLiteral("[Categories]");
110 static inline const QString SECTION_HEADING_GENERAL = QStringLiteral("[General]");
111 static inline const QString KEY_GENERAL_SEATS = QStringLiteral("Seats");
112
113//-Instance Variables--------------------------------------------------------------------------------------------------
114private:
115 QList<RefCategoryHeader> mHeaders;
116 uint mSeats;
117 uint mTotalCandidates;
118
119//-Constructor--------------------------------------------------------------------------------------------------------
120public:
121 RefCategoryConfig();
122
123//-Instance Functions-------------------------------------------------------------------------------------------------
124public:
125 uint totalCandidates() const;
126 uint seats() const;
127 const QList<RefCategoryHeader>& headers() const;
128};
129
130class RefCategoryConfig::Reader
131{
132//-Class Enums------------------------------------------------------------------------------------------------------
133private:
134 enum Section { None, Categories, General };
135
136//-Instance Variables--------------------------------------------------------------------------------------------------
137private:
138 RefCategoryConfig* mTargetConfig;
139 Qx::TextStreamReader mIniReader;
140
141//-Constructor--------------------------------------------------------------------------------------------------------
142public:
143 Reader(RefCategoryConfig* targetConfig, const QString& filePath);
144
145//-Instance Functions-------------------------------------------------------------------------------------------------
146public:
147 RefCategoryConfigError readInto();
148};
150}
151#endif // CATEGORYCONFIG_P_H
The Star namespace is the main namespace through which all functionality of the STAR++ library is acc...
Definition calculator.cpp:21