Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

HexModState Class Reference

Make a const board temporarily modifiable. More...

#include <HexModState.hpp>

List of all members.

Public Member Functions

 HexModState (const HexState &state, bool locked=false)
 Constructor.
 ~HexModState ()
 Destructor.
HexStateState () 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
HexStatem_state
HexStateAssertRestored m_assertRestored

Detailed Description

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.


Constructor & Destructor Documentation

HexModState::HexModState ( const HexState state,
bool  locked = false 
) [inline]

Constructor.

Remembers the current board state.

Parameters:
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.


Member Function Documentation

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.


Member Data Documentation

Definition at line 51 of file HexModState.hpp.

Referenced by Lock(), and Unlock().

bool HexModState::m_locked [private]

Definition at line 47 of file HexModState.hpp.

Referenced by Lock(), State(), and Unlock().

Definition at line 49 of file HexModState.hpp.

Referenced by State(), and Unlock().


The documentation for this class was generated from the following file:


6 Jan 2011 Doxygen 1.6.3