00001 //---------------------------------------------------------------------------- 00002 /** @file HexStateAssertRestored.h */ 00003 //---------------------------------------------------------------------------- 00004 00005 #ifndef HEXSTATEASSERTRESTORED_HPP 00006 #define HEXSTATEASSERTRESTORED_HPP 00007 00008 #include "HexState.hpp" 00009 00010 _BEGIN_BENZENE_NAMESPACE_ 00011 00012 //---------------------------------------------------------------------------- 00013 00014 /** Assert that the state has been restored to previous state. */ 00015 class HexStateAssertRestored 00016 { 00017 public: 00018 /** Constructor for later explicit call of Init() */ 00019 HexStateAssertRestored(); 00020 00021 /** Constructor, calls Init(). */ 00022 HexStateAssertRestored(const HexState& state); 00023 00024 /** Destructor, calls CheckRestored(). */ 00025 ~HexStateAssertRestored(); 00026 00027 /** Checks with assertions that the state is the same 00028 as it was at the last call to Init() or the constructor. 00029 */ 00030 void AssertRestored(); 00031 00032 void Init(const HexState& state); 00033 00034 /** Set to a state in which the destructor does not call 00035 AssertRestored() anymore. 00036 */ 00037 void Clear(); 00038 00039 private: 00040 #ifndef NDEBUG 00041 HexState* m_state; 00042 00043 HexState m_origState; 00044 #endif // NDEBUG 00045 00046 /** Not implemented. */ 00047 HexStateAssertRestored(const HexStateAssertRestored&); 00048 00049 /** Not implemented. */ 00050 HexStateAssertRestored& operator=(const HexStateAssertRestored&); 00051 }; 00052 00053 inline HexStateAssertRestored::HexStateAssertRestored() 00054 { 00055 #ifndef NDEBUG 00056 m_state = 0; 00057 #endif 00058 } 00059 00060 inline HexStateAssertRestored::HexStateAssertRestored(const HexState& state) 00061 { 00062 SG_DEBUG_ONLY(state); 00063 #ifndef NDEBUG 00064 Init(state); 00065 #endif 00066 } 00067 00068 inline HexStateAssertRestored::~HexStateAssertRestored() 00069 { 00070 #ifndef NDEBUG 00071 AssertRestored(); 00072 #endif 00073 } 00074 00075 inline void HexStateAssertRestored::AssertRestored() 00076 { 00077 #ifndef NDEBUG 00078 if (m_state == 0) 00079 return; 00080 SG_ASSERT(m_state->ToPlay() == m_origState.ToPlay()); 00081 SG_ASSERT(m_state->Position() == m_origState.Position()); 00082 #endif 00083 } 00084 00085 inline void HexStateAssertRestored::Clear() 00086 { 00087 #ifndef NDEBUG 00088 m_state = 0; 00089 #endif 00090 } 00091 00092 inline void HexStateAssertRestored::Init(const HexState& state) 00093 { 00094 SG_DEBUG_ONLY(state); 00095 #ifndef NDEBUG 00096 m_state = const_cast<HexState*>(&state); 00097 m_origState = state; 00098 #endif 00099 } 00100 00101 //---------------------------------------------------------------------------- 00102 00103 _END_BENZENE_NAMESPACE_ 00104 00105 #endif // HEXSTATEASSERTRESTORED_HPP