Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GoModBoard Class Reference

#include <GoModBoard.h>

List of all members.


Detailed Description

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 46 of file GoModBoard.h.

Public Member Functions

 GoModBoard (const GoBoard &bd, bool locked=false)
 Constructor.
 ~GoModBoard ()
 Destructor.
GoBoardBoard () 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
GoBoardm_bd
GoAssertBoardRestored m_assertRestored


Constructor & Destructor Documentation

GoModBoard::GoModBoard ( const GoBoard bd,
bool  locked = false 
)

Constructor.

Remembers the current board state.

Parameters:
bd The board
locked Whether to start in locked mode (for explicit usage of Lock() and Unlock())

Definition at line 92 of file GoModBoard.h.

GoModBoard::~GoModBoard (  ) 

Destructor.

Checks with assertions that the board state is restored.

Definition at line 99 of file GoModBoard.h.


Member Function Documentation

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 109 of file GoModBoard.h.

References m_bd, m_locked, and SG_ASSERT.

Referenced by GoSafetyCommands::CmdDameStatic(), GoSafetyCommands::GetSafe(), GoLadderUtil::IsProtectedLiberty(), GoBoardUtil::IsSnapback(), GoLadder::Ladder(), 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 121 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().

See also:
Board()

Definition at line 104 of file GoModBoard.h.

References Board().

void GoModBoard::Unlock (  ) 

Explicitely unlock the board.

Definition at line 115 of file GoModBoard.h.

References GoAssertBoardRestored::Init(), m_assertRestored, m_bd, and m_locked.


Member Data Documentation

GoAssertBoardRestored GoModBoard::m_assertRestored [private]

Definition at line 89 of file GoModBoard.h.

Referenced by Lock(), and Unlock().

GoBoard& GoModBoard::m_bd [private]

Definition at line 87 of file GoModBoard.h.

Referenced by Board(), and Unlock().

bool GoModBoard::m_locked [private]

Definition at line 85 of file GoModBoard.h.

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


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


17 Jun 2010 Doxygen 1.4.7