Make a const board temporarily modifiable. More...
#include <GoModBoard.h>
Public Member Functions | |
GoModBoard (const GoBoard &bd, bool locked=false) | |
Constructor. | |
~GoModBoard () | |
Destructor. | |
GoBoard & | Board () const |
Explicit conversion to non-const reference. | |
operator GoBoard & () const | |
Automatic conversion to non-const reference. | |
void | Unlock () |
Explicitely unlock the board. | |
void | Lock () |
Explicitely lock the board. | |
Private Attributes | |
bool | m_locked |
GoBoard & | m_bd |
GoAssertBoardRestored | m_assertRestored |
Make a const board temporarily modifiable.
Allows functions to use a const board for performing temporary operations on the board (e.g. searches), as long as the board is in the same state after the function is finished. This class facilitates const-correctness and encapsulation, because it allows the owner of a board, which is the only one who is allowed to do persistent changes on the board, to hand out only a const reference to other code. The other code can still use the board for temporary operations without needing a copy of the board. GoModBoard does a const_cast from the const reference to a non-const reference in its constructor and checks with GoAssertBoardRestored in its destructor that the board is returned in the same state.
Example:
// myFunction is not supposed to do persistent changes on the board // and therefore gets a const-reference. However it wants to use // the board temporarily void myFunction(const GoBoard& constBoard) { GoModBoard modBoard(constBoard); GoBoard& bd = modBoard.Board(); // get a nonconst-reference // ... play some moves and undo them // end of lifetime for modBoard, GoAssertBoardRestored is // automatically called in the destructor of modBoard }
There are also functions that allow to lock and unlock the board explicitly, for cases in which the period of temporary modifications cannot be mapped to the lifetime of a GoModBoard instance (e.g. because the period starts end ends in different functions).
Definition at line 45 of file GoModBoard.h.
GoModBoard::GoModBoard | ( | const GoBoard & | bd, | |
bool | locked = false | |||
) |
Constructor.
Remembers the current board state.
bd | The board | |
locked | Whether to start in locked mode (for explicit usage of Lock() and Unlock()) |
Definition at line 86 of file GoModBoard.h.
GoModBoard::~GoModBoard | ( | ) |
Destructor.
Checks with assertions that the board state is restored.
Definition at line 93 of file GoModBoard.h.
GoBoard & GoModBoard::Board | ( | ) | const |
Explicit conversion to non-const reference.
This function triggers an assertion, if the board is currently in locked mode.
Definition at line 103 of file GoModBoard.h.
References m_bd, m_locked, and SG_ASSERT.
Referenced by GoGtpEngine::CheckLegal(), GoSafetyCommands::CmdDameStatic(), GoSafetyCommands::GetSafe(), GoSafetyUtil::GetWinner(), GoLadderUtil::IsProtectedLiberty(), GoBoardUtil::IsSnapback(), GoLadder::Ladder(), GoBoardUtil::MoveLegalAndNotAtari(), GoBoardUtil::MoveNotLegalOrAtari(), operator GoBoard &(), and GoBookCommands::PositionInfo().
void GoModBoard::Lock | ( | ) |
Explicitely lock the board.
Checks with assertions that the board state is restored. See Lock()
Definition at line 115 of file GoModBoard.h.
References GoAssertBoardRestored::AssertRestored(), GoAssertBoardRestored::Clear(), m_assertRestored, and m_locked.
GoModBoard::operator GoBoard & | ( | ) | const |
Automatic conversion to non-const reference.
Allows to pass GoModBoard to functions that expect a non-const GoBoard reference without explicitely calling GoModBoard.Board().
Definition at line 98 of file GoModBoard.h.
References Board().
void GoModBoard::Unlock | ( | ) |
Explicitely unlock the board.
Definition at line 109 of file GoModBoard.h.
References GoAssertBoardRestored::Init(), m_assertRestored, m_bd, and m_locked.
Definition at line 83 of file GoModBoard.h.
GoBoard& GoModBoard::m_bd [private] |
Definition at line 81 of file GoModBoard.h.
bool GoModBoard::m_locked [private] |
Definition at line 79 of file GoModBoard.h.