-
Notifications
You must be signed in to change notification settings - Fork 2
/
File.h
65 lines (51 loc) · 1.52 KB
/
File.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef LIBCHESS_FILE_H
#define LIBCHESS_FILE_H
#include <cstdint>
#include <optional>
#include "internal/MetaValueType.h"
namespace libchess {
class File : public MetaValueType<int> {
public:
class Value {
public:
enum FileValue : value_type
{
FILE_A,
FILE_B,
FILE_C,
FILE_D,
FILE_E,
FILE_F,
FILE_G,
FILE_H
};
};
constexpr explicit File(value_type value) : MetaValueType<value_type>(value) {
}
constexpr char to_char() const {
return 'a' + value();
}
constexpr static std::optional<File> from(char c) {
int offset = c >= 'a' ? (c - 'a') : (c - 'A');
if (offset < 0 || offset > 7) {
return {};
}
return File{Value::FILE_A + offset};
}
};
namespace constants {
constexpr static File FILE_A = File{File::Value::FILE_A};
constexpr static File FILE_B = File{File::Value::FILE_B};
constexpr static File FILE_C = File{File::Value::FILE_C};
constexpr static File FILE_D = File{File::Value::FILE_D};
constexpr static File FILE_E = File{File::Value::FILE_E};
constexpr static File FILE_F = File{File::Value::FILE_F};
constexpr static File FILE_G = File{File::Value::FILE_G};
constexpr static File FILE_H = File{File::Value::FILE_H};
} // namespace constants
} // namespace libchess
namespace std {
template <>
struct hash<libchess::File> : public hash<libchess::File::value_type> {};
} // namespace std
#endif // LIBCHESS_FILE_H