BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
File.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <cctype> // IWYU pragma: keep - for std::tolower()
23#include <expected>
24#include <format>
26#include <magic_enum/magic_enum.hpp>
27#include <string>
28
29namespace chess::board {
30
36enum class File : BitboardIndex {
37 A,
38 B,
39 C,
40 D,
41 E,
42 F,
43 G,
45};
46
55[[nodiscard]] auto file_from_char(char character)
56 -> std::expected<File, std::string>;
57
63[[nodiscard]] auto file_to_char(File file) -> char;
64
65} // namespace chess::board
66
73template <>
74struct std::formatter<chess::board::File> final {
75 template <typename ParseContext>
76 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
77 {
78 return ctx.begin();
79 }
80
81 template <typename FormatContext>
82 auto format(
83 chess::board::File file, FormatContext& ctx) const -> typename FormatContext::iterator
84 {
85 return std::format_to(ctx.out(), "{}", chess::board::file_to_char(file));
86 }
87};
88
89/*
90 ___ ,--,
91 ,---, ,--.'|_ ,--, ,--.'|
92 ,---.'| | | :,' ,--.'| | | :
93 | | : : : ' : | |, : : ' .--.--.
94 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
95 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
96 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
97. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
98' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
99| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
100| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
101 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
102 `----' `----' `--`---' ---`-' `--" `--" `--"
103
104 */
105
106namespace chess::board {
107
108inline auto file_from_char(char character)
109 -> std::expected<File, std::string>
110{
111 switch (character) {
112 case 'a': [[fallthrough]];
113 case 'A': return File::A;
114
115 case 'b': [[fallthrough]];
116 case 'B': return File::B;
117
118 case 'c': [[fallthrough]];
119 case 'C': return File::C;
120
121 case 'd': [[fallthrough]];
122 case 'D': return File::D;
123
124 case 'e': [[fallthrough]];
125 case 'E': return File::E;
126
127 case 'f': [[fallthrough]];
128 case 'F': return File::F;
129
130 case 'g': [[fallthrough]];
131 case 'G': return File::G;
132
133 case 'h': [[fallthrough]];
134 case 'H': return File::H;
135
136 default:
137 return std::unexpected {
138 std::format(
139 "Cannot parse File from character: {}",
140 character)
141 };
142 }
143}
144
145inline auto file_to_char(const File file) -> char
146{
147 const auto upperChar = magic_enum::enum_name(file).front();
148
149 return static_cast<char>(
150 std::tolower(static_cast<unsigned char>(upperChar)));
151}
152
153} // namespace chess::board
std::uint_fast8_t BitboardIndex
auto file_from_char(char character) -> std::expected< File, std::string >
Definition File.hpp:108
auto file_to_char(File file) -> char
Definition File.hpp:145
@ C
The C file.
Definition File.hpp:39
@ E
The E file. This is the file that the kings start on.
Definition File.hpp:41
@ A
The A file.
Definition File.hpp:37
@ F
The F file.
Definition File.hpp:42
@ B
The B file.
Definition File.hpp:38
@ H
The H file.
Definition File.hpp:44
@ G
The G file.
Definition File.hpp:43
@ D
The D file. This is the file that the queens start on.
Definition File.hpp:40
auto format(chess::board::File file, FormatContext &ctx) const -> typename FormatContext::iterator
Definition File.hpp:82
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator
Definition File.hpp:76