Harden unit_movement_resetter class.

Add a warning regarding its usage and test whether the unit exists
before assigning to it. (Still a bit experimental, but initial tests
show no regressions.)
This commit is contained in:
Mark de Wever 2011-10-15 18:06:24 +00:00
parent 9a44eaaa76
commit af7ef732bd
2 changed files with 17 additions and 1 deletions

View File

@ -2799,6 +2799,14 @@ unit_movement_resetter::unit_movement_resetter(unit &u, bool operate) :
unit_movement_resetter::~unit_movement_resetter()
{
assert(resources::units);
/*
* This assert should be safe, but not 100% sure so if it fails need
* some extra validation code in this function.
*/
assert(resources::units->has_unit(&u_));
u_.movement_ = moves_;
}

View File

@ -467,8 +467,16 @@ private:
void clear_visibility_cache() const { invisibility_cache_.clear(); }
};
/** Object which temporarily resets a unit's movement */
/**
* Object which temporarily resets a unit's movement.
*
* @warning
* The unit whose movement is reset may not be deleted while a
* @ref unit_movement_resetter object 'holds'. So best use it only in a small
* scope.
*/
struct unit_movement_resetter
: private boost::noncopyable
{
unit_movement_resetter(unit& u, bool operate=true);
~unit_movement_resetter();