From d39556e2d59be2537f4905d1ec6ce2b6286aedb6 Mon Sep 17 00:00:00 2001 From: Ali El Gariani Date: Mon, 30 Nov 2009 21:49:53 +0000 Subject: [PATCH] Add a 'temporary unit mover'. --- src/unit.cpp | 14 ++++++++++++++ src/unit.hpp | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/unit.cpp b/src/unit.cpp index ac828fb820e..623684de238 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -2830,6 +2830,20 @@ temporary_unit_placer::~temporary_unit_placer() } } +temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst) + : m_(m), src_(src), dst_(dst), temp_(m.extract(dst)) +{ + m.move(src_, dst_); +} + +temporary_unit_mover::~temporary_unit_mover() +{ + m_.move(dst_, src_); + if(temp_) { + m_.insert(temp_); + } +} + std::string unit::image_mods() const{ std::stringstream modifier; if(flag_rgb_.size()){ diff --git a/src/unit.hpp b/src/unit.hpp index 4b0573b74fd..0493cad63ad 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -522,6 +522,23 @@ private: std::pair *temp_; }; +/** + * This object is used to temporary move a unit in the unit map, swapping out + * any unit that is already there. On destruction, it restores the unit map to + * its original. + */ +struct temporary_unit_mover +{ + temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst); + ~temporary_unit_mover(); + +private: + unit_map& m_; + const map_location& src_; + const map_location& dst_; + std::pair *temp_; +}; + /** * Gets a checksum for a unit. *