Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  
Public Member Functions | Private Attributes

GoModBoard Class Reference

Make a const board temporarily modifiable. More...

#include <GoModBoard.h>

List of all members.

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

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


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


Member Function Documentation

GoBoard & GoModBoard::Board (  )  const
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().

See also:
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.


Member Data Documentation

Definition at line 83 of file GoModBoard.h.

Referenced by Lock(), and Unlock().

Definition at line 81 of file GoModBoard.h.

Referenced by Board(), and Unlock().

bool GoModBoard::m_locked [private]

Definition at line 79 of file GoModBoard.h.

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


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


Sun Mar 13 2011 Doxygen 1.7.1