|
3 | 3 | #ifndef BOOK_H_INCLUDED
|
4 | 4 | #define BOOK_H_INCLUDED
|
5 | 5 |
|
6 |
| -namespace Polyfish::Book |
| 6 | +namespace Polyfish |
7 | 7 | {
|
8 |
| - namespace |
9 |
| - { |
10 |
| - static const union { uint32_t i; char c[4]; } Le = { 0x01020304 }; |
11 |
| - static const bool IsBigEndian = (Le.c[0] == 1); |
12 |
| - } |
| 8 | + class BookManager; |
13 | 9 |
|
14 |
| - class BookUtil |
| 10 | + namespace Book |
15 | 11 | {
|
16 |
| - public: |
17 |
| - template <typename IntType> |
18 |
| - static IntType read_big_endian(const unsigned char* buffer, size_t& offset, size_t bufferLen) |
| 12 | + namespace |
19 | 13 | {
|
20 |
| - IntType result; |
21 |
| - constexpr size_t typeSize = sizeof(IntType); |
| 14 | + static const union { uint32_t i; char c[4]; } Le = { 0x01020304 }; |
| 15 | + static const bool IsBigEndian = (Le.c[0] == 1); |
| 16 | + } |
22 | 17 |
|
23 |
| - if (offset + typeSize > bufferLen) |
| 18 | + class BookUtil |
| 19 | + { |
| 20 | + public: |
| 21 | + template <typename IntType> |
| 22 | + static IntType read_big_endian(const unsigned char* buffer, size_t& offset, size_t bufferLen) |
24 | 23 | {
|
25 |
| - assert(false); |
26 |
| - return IntType(); |
27 |
| - } |
| 24 | + IntType result; |
| 25 | + constexpr size_t typeSize = sizeof(IntType); |
28 | 26 |
|
29 |
| - //Read the integer type and convert (if needed) |
30 |
| - memcpy(&result, buffer + offset, typeSize); |
| 27 | + if (offset + typeSize > bufferLen) |
| 28 | + { |
| 29 | + assert(false); |
| 30 | + return IntType(); |
| 31 | + } |
31 | 32 |
|
32 |
| - if (!IsBigEndian) |
33 |
| - { |
34 |
| - unsigned char u[typeSize]; |
35 |
| - typename std::make_unsigned<IntType>::type v = 0; |
| 33 | + //Read the integer type and convert (if needed) |
| 34 | + memcpy(&result, buffer + offset, typeSize); |
| 35 | + |
| 36 | + if (!IsBigEndian) |
| 37 | + { |
| 38 | + unsigned char u[typeSize]; |
| 39 | + typename std::make_unsigned<IntType>::type v = 0; |
| 40 | + |
| 41 | + memcpy(&u, &result, typeSize); |
| 42 | + for (size_t i = 0; i < typeSize; ++i) |
| 43 | + v = (v << 8) | u[i]; |
36 | 44 |
|
37 |
| - memcpy(&u, &result, typeSize); |
38 |
| - for (size_t i = 0; i < typeSize; ++i) |
39 |
| - v = (v << 8) | u[i]; |
| 45 | + memcpy(&result, &v, typeSize); |
| 46 | + } |
40 | 47 |
|
41 |
| - memcpy(&result, &v, typeSize); |
| 48 | + offset += typeSize; |
| 49 | + return result; |
42 | 50 | }
|
43 | 51 |
|
44 |
| - offset += typeSize; |
45 |
| - return result; |
46 |
| - } |
| 52 | + template <typename IntType> |
| 53 | + static IntType read_big_endian(const unsigned char* buffer, size_t bufferLen) |
| 54 | + { |
| 55 | + size_t offset = 0; |
| 56 | + return read_big_endian<IntType>(buffer, offset, bufferLen); |
| 57 | + } |
| 58 | + }; |
47 | 59 |
|
48 |
| - template <typename IntType> |
49 |
| - static IntType read_big_endian(const unsigned char* buffer, size_t bufferLen) |
| 60 | + class Book |
50 | 61 | {
|
51 |
| - size_t offset = 0; |
52 |
| - return read_big_endian<IntType>(buffer, offset, bufferLen); |
53 |
| - } |
54 |
| - }; |
| 62 | + friend class Polyfish::BookManager; |
55 | 63 |
|
56 |
| - class Book |
57 |
| - { |
58 |
| - friend class Polyfish::BookManager; |
59 |
| - |
60 |
| - private: |
61 |
| - static Book* create_book(const std::string& filename); |
| 64 | + private: |
| 65 | + static Book* create_book(const std::string& filename); |
62 | 66 |
|
63 |
| - public: |
64 |
| - Book() { } |
65 |
| - virtual ~Book() { } |
| 67 | + public: |
| 68 | + Book() { } |
| 69 | + virtual ~Book() { } |
66 | 70 |
|
67 |
| - Book(const Book&) = delete; |
68 |
| - Book& operator=(const Book&) = delete; |
| 71 | + Book(const Book&) = delete; |
| 72 | + Book& operator=(const Book&) = delete; |
69 | 73 |
|
70 |
| - virtual std::string type() const = 0; |
| 74 | + virtual std::string type() const = 0; |
71 | 75 |
|
72 |
| - virtual bool open(const std::string& filename) = 0; |
73 |
| - virtual void close() = 0; |
| 76 | + virtual bool open(const std::string& filename) = 0; |
| 77 | + virtual void close() = 0; |
74 | 78 |
|
75 |
| - virtual Move probe(const Position& pos, size_t width, bool onlyGreen) const = 0; |
76 |
| - virtual void show_moves(const Position& pos) const = 0; |
77 |
| - }; |
| 79 | + virtual Move probe(const Position& pos, size_t width, bool onlyGreen) const = 0; |
| 80 | + virtual void show_moves(const Position& pos) const = 0; |
| 81 | + }; |
| 82 | + } |
78 | 83 | }
|
79 | 84 |
|
80 | 85 | #endif
|
|
0 commit comments