Simplify map_labels::set_label() by factoring out some find()s.
In two places, the code from get_label() was basically duplicated, presumably because the public get_label() function returns a pointer to a const object. I added a private version that returns a pointer to a non-const object, and now set_label() is easier to read.
This commit is contained in:
parent
df002cdfa1
commit
ed3446a3e7
2 changed files with 19 additions and 22 deletions
|
@ -144,6 +144,10 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
const bool immutable)
|
||||
{
|
||||
terrain_label* res = 0;
|
||||
|
||||
// See if there is already a label in this location for this team.
|
||||
// (We do not use get_label_private() here because we might need
|
||||
// the label_map as well as the terrain_label.)
|
||||
team_label_map::iterator current_label_map = labels_.find(team_name);
|
||||
label_map::iterator current_label;
|
||||
|
||||
|
@ -158,17 +162,9 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
delete current_label->second;
|
||||
current_label_map->second.erase(loc);
|
||||
|
||||
team_label_map::iterator global_label_map = labels_.find("");
|
||||
label_map::iterator itor;
|
||||
bool update = false;
|
||||
if(global_label_map != labels_.end()) {
|
||||
itor = global_label_map->second.find(loc);
|
||||
update = itor != global_label_map->second.end();
|
||||
}
|
||||
if (update)
|
||||
{
|
||||
itor->second->recalculate();
|
||||
}
|
||||
// Restore the global label in the same spot, if any.
|
||||
if ( terrain_label* global_label = get_label_private(loc, "") )
|
||||
global_label->recalculate();
|
||||
|
||||
}
|
||||
else
|
||||
|
@ -179,14 +175,10 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
}
|
||||
else if(!text.str().empty())
|
||||
{
|
||||
team_label_map::iterator global_label_map = labels_.find("");
|
||||
label_map::iterator itor;
|
||||
bool update = false;
|
||||
if(global_label_map != labels_.end()) {
|
||||
itor = global_label_map->second.find(loc);
|
||||
update = itor != global_label_map->second.end();
|
||||
}
|
||||
// See if we will be replacing a global label.
|
||||
terrain_label* global_label = get_label_private(loc, "");
|
||||
|
||||
// Add the new label.
|
||||
terrain_label* label = new terrain_label(text,
|
||||
team_name,
|
||||
loc,
|
||||
|
@ -199,10 +191,9 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
|
||||
res = label;
|
||||
|
||||
if (update)
|
||||
{
|
||||
itor->second->recalculate();
|
||||
}
|
||||
// Hide the old label.
|
||||
if ( global_label != NULL )
|
||||
global_label->recalculate();
|
||||
|
||||
}
|
||||
return res;
|
||||
|
|
|
@ -74,6 +74,12 @@ public:
|
|||
|
||||
private:
|
||||
void clear_map(label_map &, bool);
|
||||
/// For our private use, a wrapper for get_label() that can return a pointer
|
||||
/// to a non-const terrain_label.
|
||||
terrain_label* get_label_private(const map_location& loc, const std::string& team_name)
|
||||
{ return const_cast<terrain_label*>(get_label(loc, team_name)); }
|
||||
// Note: this is not an overload of get_label() so that we do not block
|
||||
// outsiders from calling get_label for a non-const map_labels object.
|
||||
|
||||
const display& disp_;
|
||||
const team* team_;
|
||||
|
|
Loading…
Add table
Reference in a new issue