Flags.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Flags.h This file contains the declaration of class Flags.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef FLAGS_H
25 #define FLAGS_H
26 
27 #ifndef USE_PCH
28 #include <stdint.h>
29 #endif
30 
31 namespace cwchess {
32 
33 /** @brief The POD base type of class Flags.
34 *
35  * @sa Flags, fl_pawn_can_take_queen_side, fl_pawn_is_not_blocked, fl_pawn_can_take_king_side, fl_pawn_can_move_two_squares
36 * /
37 struct FlagsData {
38  uint8_t M_bits; //!< 0TKNQ000, T=can move two squares, K=can take king side, N=is not blocked, Q=can take queen side.
39 };
40 
41 FlagsData const fl_none = { 0 };
42 
43 uint8_t const fl_pawn_mask = { 120 };
44 
45 // Do not change these values, their exact value has a deeper meaning.
46 FlagsData const fl_pawn_can_take_queen_side = { 8 }; //!< A constant representing the flag 'pawn can take queen side'.
47 FlagsData const fl_pawn_is_not_blocked = { 16 }; //!< A constant representing the flag 'pawn is not blocked'.
48 FlagsData const fl_pawn_can_take_king_side = { 32 }; //!< A constant representing the flag 'pawn can take king side'.
49 FlagsData const fl_pawn_can_move_two_squares = { 64 }; //!< A constant representing the flag 'pawn can move two squares'.
50 
51 // Compare constants (this should never be needed, but why not add it).
52 inline bool operator==(FlagsData f1, FlagsData f2) { return f1.M_bits == f2.M_bits; }
53 inline bool operator!=(FlagsData f1, FlagsData f2) { return f1.M_bits != f2.M_bits; }
54 
55 /** @brief Calculate the union of two Flags constants.* /
57 {
58  FlagsData result;
59  result.M_bits = x.M_bits | y.M_bits;
60  return result;
61 }
62 
63 /** @brief Calculate the intersection of two Flags constants.* /
65 {
66  FlagsData result;
67  result.M_bits = x.M_bits & y.M_bits;
68  return result;
69 }
70 
71 /** @brief Calculate the union minus the intersection of two Flags constants.* /
73 {
74  FlagsData result;
75  result.M_bits = x.M_bits ^ y.M_bits;
76  return result;
77 }
78 
79 /** @brief %Flags representing the state of a piece on the chessboard.
80 *
81  * This class is for internal use. You should not need it.
82 *
83  * @sa FlagsData
84 * /
85 class Flags : protected FlagsData {
86  public:
87  /** @name Constructors* /
88  //@{
89 
90  //! Construct an uninitialized Flags object.
91  Flags(void) { }
92 
93  //! Copy-constructor.
94  Flags(Flags const& flags) { M_bits = flags.M_bits; }
95 
96  //! Construct a Flags object from a constant.
97  Flags(FlagsData flags) { M_bits = flags.M_bits; }
98 
99  //@}
100 
101  /** @name Assignment operators* /
102  //@{
103 
104  //! Assign from another Flags object.
105  Flags& operator=(Flags const& flags) { M_bits = flags.M_bits; return* this; }
106 
107  //! Assign from a constant.
108  Flags& operator=(FlagsData flags) { M_bits = flags.M_bits; return* this; }
109 
110  //! Set all flags to 0.
111  void clear(void) { M_bits = 0; }
112 
113  //@}
114 
115  /** @name Comparision operators* /
116  //@{
117 
118  friend bool operator==(Flags const& f1, Flags const& f2) { return f1.M_bits == f2.M_bits; }
119  friend bool operator==(Flags const& f1, FlagsData f2) { return f1.M_bits == f2.M_bits; }
120  friend bool operator==(FlagsData f1, Flags const& f2) { return f1.M_bits == f2.M_bits; }
121  friend bool operator!=(Flags const& f1, Flags const& f2) { return f1.M_bits != f2.M_bits; }
122  friend bool operator!=(Flags const& f1, FlagsData f2) { return f1.M_bits != f2.M_bits; }
123  friend bool operator!=(FlagsData f1, Flags const& f2) { return f1.M_bits != f2.M_bits; }
124 
125  //@}
126 
127  /** @name Bit fiddling* /
128  //@{
129 
130  //! Set all bits that are set in \a flags.
131  Flags& operator|=(Flags const& flags) { M_bits |= flags.M_bits; return* this; }
132 
133  //! Set all bits that are set in \a flags.
134  Flags& operator|=(FlagsData flags) { M_bits |= flags.M_bits; return* this; }
135 
136  //! Reset all bits that are not set in \a flags.
137  Flags& operator&=(Flags const& flags) { M_bits& = flags.M_bits; return* this; }
138 
139  //! Reset all bits that are not set in \a flags.
140  Flags& operator&=(FlagsData flags) { M_bits& = flags.M_bits; return* this; }
141 
142  //! Toggle all bits that are set in \a flags.
143  Flags& operator^=(Flags const& flags) { M_bits ^= flags.M_bits; return* this; }
144 
145  //! Toggle all bits that are set in \a flags.
146  Flags& operator^=(FlagsData flags) { M_bits ^= flags.M_bits; return* this; }
147 
148  //! Set the bits that are set in \a flags.
149  void set(FlagsData flags) { M_bits |= flags.M_bits; }
150 
151  //! Clear the bits that are set in \a flags.
152  void reset(FlagsData flags) { M_bits& = ~flags.M_bits; }
153 
154  //@}
155 
156  /** @name Special functions* /
157  //@{
158 
159  //! Set the fl_pawn_can_move_two_squares bit iff fl_pawn_is_not_blocked bit is already set.
161 
162  //@}
163 
164  /** @name Accessors* /
165  //@{
166 
167  //! Return the underlaying integral value.
168  uint8_t operator()(void) const { return M_bits; }
169 
170  //@}
171 
172 };
173 
174 } // namespace cwchess
175 
176 #endif // FLAGS_H
Flags(FlagsData flags)
Construct a Flags object from a constant.
Definition: Flags.h:97
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
The POD base type of class Flags.
Definition: Flags.h:37
BitBoardData const f1
The square f1.
Definition: BitBoard.h:101
FlagsData const fl_pawn_is_not_blocked
A constant representing the flag& #39;pawn is not blocked&#39;.
Definition: Flags.h:47
Flags representing the state of a piece on the chessboard.
Definition: Flags.h:85
uint8_t operator()(void) const
Return the underlaying integral value.
Definition: Flags.h:168
uint8_t M_bits
0TKNQ000, T=can move two squares, K=can take king side, N=is not blocked, Q=can take queen side...
Definition: Flags.h:38
FlagsData const fl_pawn_can_take_king_side
A constant representing the flag& #39;pawn can take king side&#39;.
Definition: Flags.h:48
void reset(FlagsData flags)
Clear the bits that are set in flags.
Definition: Flags.h:152
Flags(void)
Construct an uninitialized Flags object.
Definition: Flags.h:91
BitBoardData operator|(BitBoardData x, BitBoardData y)
Calculate the union of two bit board constants.
Definition: BitBoard.h:166
BitBoardData operator&(BitBoardData x, BitBoardData y)
Calculate the intersection of two bit board constants.
Definition: BitBoard.h:174
Flags & operator&=(Flags const& flags)
Reset all bits that are not set in flags.
Definition: Flags.h:137
Flags(Flags const& flags)
Copy-constructor.
Definition: Flags.h:94
Flags & operator=(Flags const& flags)
Assign from another Flags object.
Definition: Flags.h:105
Flags & operator|=(FlagsData flags)
Set all bits that are set in flags.
Definition: Flags.h:134
Flags & operator^=(Flags const& flags)
Toggle all bits that are set in flags.
Definition: Flags.h:143
Flags & operator&=(FlagsData flags)
Reset all bits that are not set in flags.
Definition: Flags.h:140
Flags & operator^=(FlagsData flags)
Toggle all bits that are set in flags.
Definition: Flags.h:146
Flags & operator=(FlagsData flags)
Assign from a constant.
Definition: Flags.h:108
Flags & operator|=(Flags const& flags)
Set all bits that are set in flags.
Definition: Flags.h:131
FlagsData const fl_pawn_can_move_two_squares
A constant representing the flag& #39;pawn can move two squares&#39;.
Definition: Flags.h:49
BitBoardData const f2
The square f2.
Definition: BitBoard.h:109
void set_can_move_two_squares_if_not_blocked(void)
Set the fl_pawn_can_move_two_squares bit iff fl_pawn_is_not_blocked bit is already set...
Definition: Flags.h:160
BitBoardData operator^(BitBoardData x, BitBoardData y)
Calculate the union minus the intersection of two bit board constants.
Definition: BitBoard.h:182
FlagsData const fl_pawn_can_take_queen_side
A constant representing the flag& #39;pawn can take queen side&#39;.
Definition: Flags.h:46
void clear(void)
Set all flags to 0.
Definition: Flags.h:111

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.