HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
pgnreader.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2022-2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__PGNREADER_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__PGNREADER_H_INCLUDED
19
20#include "chessboard.h"
21#include "pgnreader-error.h"
22#include "pgnreader-types.h"
23
24#include <cstddef>
25#include <cstdint>
26#include <format>
27#include <string_view>
28
30{
31
32class ChessBoard;
33
36
41{
43 Abort,
44
47
48};
49
54{
56 std::uint64_t lineNumber;
57};
58
62{
63public:
67 virtual void gameStart()
68 {
69 }
70
77 virtual void pgnTag(std::string_view key, std::string_view value)
78 {
79 static_cast<void>(key);
80 static_cast<void>(value);
81 }
82
90 virtual void moveTextSection() { }
91
99 virtual void comment(std::string_view comment)
100 {
101 static_cast<void>(comment);
102 }
103
109 virtual void gameTerminated(PgnResult result)
110 {
111 static_cast<void>(result);
112 }
113
120 virtual void setBoardReferences(const ChessBoard &curBoard, const ChessBoard &prevBoard)
121 {
122 static_cast<void>(curBoard);
123 static_cast<void>(prevBoard);
124 }
125
134 virtual void afterMove(Move move)
135 {
136 static_cast<void>(move);
137 }
138
145 virtual void nag(std::uint8_t nagNum)
146 {
147 static_cast<void>(nagNum);
148 }
149
156 virtual void variationStart()
157 {
158 }
159
167 virtual void variationEnd()
168 {
169 }
170
172 virtual void endOfPGN()
173 {
174 }
175
208 virtual PgnReaderOnErrorAction onError(const PgnError &error, const PgnErrorInfo &additionalInfo)
209 {
210 static_cast<void>(error);
211 static_cast<void>(additionalInfo);
212
214 }
215};
216
218enum class PgnReaderActionClass : unsigned int
219{
221 PgnTag = 0U,
222
224 Move,
225
229 NAG,
230
232 Variation,
233
235 Comment,
236};
237
240{
241private:
242 std::uint32_t m_filterBits { };
243
244 static constexpr std::uint32_t actionBit(PgnReaderActionClass action) noexcept
245 {
246 const unsigned int shift { static_cast<unsigned int>(action) };
247 if (shift < 32U) [[likely]]
248 {
249 return UINT32_C(1) << shift;
250 }
251 else [[unlikely]]
252 {
253 return 0U;
254 }
255 }
256
257 constexpr void enablePack() noexcept
258 {
259 }
260
261 template <typename... Args>
262 constexpr void enablePack(PgnReaderActionClass action, Args... rest) noexcept
263 {
264 set(action, true);
265 enablePack(rest...);
266 }
267
268public:
273 template <typename... Args>
274 constexpr PgnReaderActionFilter(Args... enabled) noexcept
275 {
276 enablePack(enabled...);
277 }
278
283 constexpr inline void set(PgnReaderActionClass action, bool enable) noexcept
284 {
285 const std::uint32_t mask { actionBit(action) };
286
287 if (enable)
288 m_filterBits |= mask;
289 else
290 m_filterBits &= ~mask;
291 }
292
297 constexpr inline bool isEnabled(PgnReaderActionClass action) noexcept
298 {
299 return (m_filterBits & actionBit(action)) != 0U;
300 }
301
306 constexpr inline std::uint32_t getBitMask() noexcept
307 {
308 return m_filterBits;
309 }
310};
311
314{
315public:
338 static void readFromMemory(std::string_view pgn, PgnReaderActions &actions, PgnReaderActionFilter filter);
339};
340
342
343}
344
345#endif
The chessboard.
Definition chessboard.h:589
A legal move. Important: see the note!
Definition chessboard.h:198
PGN error exception.
Definition pgnreader-error.h:70
PGN reader action filter.
Definition pgnreader.h:240
constexpr std::uint32_t getBitMask() noexcept
Returns a bit mask of enabled action classes. When bit N is set, action class with numeric value N is...
Definition pgnreader.h:306
constexpr bool isEnabled(PgnReaderActionClass action) noexcept
Returns whether an action class is enabled.
Definition pgnreader.h:297
static constexpr std::uint32_t actionBit(PgnReaderActionClass action) noexcept
Definition pgnreader.h:244
constexpr void enablePack(PgnReaderActionClass action, Args... rest) noexcept
Definition pgnreader.h:262
constexpr void enablePack() noexcept
Definition pgnreader.h:257
constexpr PgnReaderActionFilter(Args... enabled) noexcept
Constructor.
Definition pgnreader.h:274
std::uint32_t m_filterBits
Definition pgnreader.h:242
constexpr void set(PgnReaderActionClass action, bool enable) noexcept
Enables/disables an action class.
Definition pgnreader.h:283
Semantic actions for reading a PGN. The caller is expected to inherit this class and override all cal...
Definition pgnreader.h:62
virtual void endOfPGN()
End of PGN.
Definition pgnreader.h:172
virtual void variationStart()
Invoked after a variation start is processed.
Definition pgnreader.h:156
virtual void moveTextSection()
Invoked when all PGN tags of the game are read and before any moves are processed.
Definition pgnreader.h:90
virtual void comment(std::string_view comment)
PGN comment.
Definition pgnreader.h:99
virtual void gameStart()
Invoked when a new game starts.
Definition pgnreader.h:67
virtual void gameTerminated(PgnResult result)
Invoked after the game termination marker is processed.
Definition pgnreader.h:109
virtual PgnReaderOnErrorAction onError(const PgnError &error, const PgnErrorInfo &additionalInfo)
Error handler for recoverable errors.
Definition pgnreader.h:208
virtual void variationEnd()
Invoked after a variation end marker is encountered but before the board states have been restored.
Definition pgnreader.h:167
virtual void nag(std::uint8_t nagNum)
Invoked after a numeric annotation glyph (NAG) is processed.
Definition pgnreader.h:145
virtual void afterMove(Move move)
Invoked after a move is processed.
Definition pgnreader.h:134
virtual void pgnTag(std::string_view key, std::string_view value)
Invoked when a PGN tag has been read.
Definition pgnreader.h:77
virtual void setBoardReferences(const ChessBoard &curBoard, const ChessBoard &prevBoard)
Invoked when the PGN reader is instantiated.
Definition pgnreader.h:120
The PGN reader interface.
Definition pgnreader.h:314
static void readFromMemory(std::string_view pgn, PgnReaderActions &actions, PgnReaderActionFilter filter)
Reads and processes a PGN from memory.
PgnResult
Game result.
Definition pgnreader-types.h:32
PgnReaderActionClass
PGN reader filterable action classes.
Definition pgnreader.h:219
PgnReaderOnErrorAction
Action for recoverable PGN reader error.
Definition pgnreader.h:41
@ Variation
Variation start/end, including any actions within a variation.
@ NAG
Numeric annotation glyph.
Definition pgnscannertokens.h:121
Definition chessboard-types-squareset.h:30
Additional error info.
Definition pgnreader.h:54
std::uint64_t lineNumber
Line number of the error. Numbering starts from 1.
Definition pgnreader.h:56