Make a const board temporarily modifiable. More...
#include <HexModState.hpp>
Public Member Functions | |
HexModState (const HexState &state, bool locked=false) | |
Constructor. | |
~HexModState () | |
Destructor. | |
HexState & | State () const |
Explicit conversion to non-const reference. | |
operator HexState & () const | |
Automatic conversion to non-const reference. | |
void | Unlock () |
Explicitly unlock the board. | |
void | Lock () |
Explicitly lock the board. | |
Private Attributes | |
bool | m_locked |
HexState & | m_state |
HexStateAssertRestored | m_assertRestored |
Make a const board temporarily modifiable.
Allows functions to use a const state for performing temporary operations (e.g. searches), as long as the state is the same state after the function is finished. This class facilitates const-correctness and encapsulation, because it allows the owner of a state, 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. HexModState does a const_cast from the const reference to a non-const reference in its constructor and checks with HexStateAssertRestored 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 HexState& constState) { HexModState modState(constState); HexState& state = modState.State(); // get a nonconst-reference // ... play some moves and undo them // end of lifetime for modState, HexStateAssertRestored 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 HexModState instance (e.g. because the period starts end ends in different functions).
Definition at line 49 of file HexModState.hpp.
HexModState::HexModState | ( | const HexState & | state, | |
bool | locked = false | |||
) | [inline] |
Constructor.
Remembers the current board state.
state | The state | |
locked | Whether to start in locked mode (for explicit usage of Lock() and Unlock()) |
Definition at line 95 of file HexModState.hpp.
HexModState::~HexModState | ( | ) | [inline] |
Destructor.
Checks with assertions that the board state is restored.
Definition at line 102 of file HexModState.hpp.
void HexModState::Lock | ( | ) | [inline] |
Explicitly lock the board.
Checks with assertions that the board state is restored. See Lock()
Definition at line 124 of file HexModState.hpp.
References HexStateAssertRestored::AssertRestored(), HexStateAssertRestored::Clear(), m_assertRestored, and m_locked.
HexModState::operator HexState & | ( | ) | const [inline] |
Automatic conversion to non-const reference.
Allows to pass HexModState to functions that expect a non-const GoBoard reference without explicitely calling HexModState.Board(). See State()
Definition at line 107 of file HexModState.hpp.
References State().
HexState & HexModState::State | ( | ) | const [inline] |
Explicit conversion to non-const reference.
This function triggers an assertion, if the board is currently in locked mode.
Definition at line 112 of file HexModState.hpp.
References m_locked, and m_state.
Referenced by BookUtil::DumpPolarizedLeafs(), BookUtil::DumpVisualizationData(), and operator HexState &().
void HexModState::Unlock | ( | ) | [inline] |
Explicitly unlock the board.
Definition at line 118 of file HexModState.hpp.
References HexStateAssertRestored::Init(), m_assertRestored, m_locked, and m_state.
Definition at line 51 of file HexModState.hpp.
bool HexModState::m_locked [private] |
Definition at line 47 of file HexModState.hpp.
HexState& HexModState::m_state [private] |
Definition at line 49 of file HexModState.hpp.