Map/Label: cleanup and refactoring
Mostly making it so the map_labels objects store their labels directly instead of as pointers. Pointers were probably used to avoid copy operations since the copy ctor was deliberately deleted. However, that's moot now with C++11 and move semantics.
This commit is contained in:
parent
5e0dfb0abb
commit
7b85d7b113
2 changed files with 46 additions and 44 deletions
|
@ -78,10 +78,10 @@ map_labels& map_labels::operator=(const map_labels& other)
|
|||
|
||||
void map_labels::write(config& res) const
|
||||
{
|
||||
for(team_label_map::const_iterator labs = labels_.begin(); labs != labels_.end(); ++labs) {
|
||||
for(label_map::const_iterator i = labs->second.begin(); i != labs->second.end(); ++i) {
|
||||
for(const auto& group : labels_) {
|
||||
for(const auto& label : group.second) {
|
||||
config item;
|
||||
i->second->write(item);
|
||||
label.second.write(item);
|
||||
|
||||
res.add_child("label", item);
|
||||
}
|
||||
|
@ -93,9 +93,7 @@ void map_labels::read(const config& cfg)
|
|||
clear_all();
|
||||
|
||||
for(const config& i : cfg.child_range("label")) {
|
||||
const map_location loc(i, resources::gamedata);
|
||||
terrain_label* label = new terrain_label(*this, i);
|
||||
add_label(loc, label);
|
||||
add_label(*this, i);
|
||||
}
|
||||
|
||||
recalculate_labels();
|
||||
|
@ -103,11 +101,11 @@ void map_labels::read(const config& cfg)
|
|||
|
||||
terrain_label* map_labels::get_label_private(const map_location& loc, const std::string& team_name)
|
||||
{
|
||||
team_label_map::const_iterator label_map = labels_.find(team_name);
|
||||
auto label_map = labels_.find(team_name);
|
||||
if(label_map != labels_.end()) {
|
||||
map_labels::label_map::const_iterator itor = label_map->second.find(loc);
|
||||
auto itor = label_map->second.find(loc);
|
||||
if(itor != label_map->second.end()) {
|
||||
return itor->second;
|
||||
return &itor->second;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,7 +168,6 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
// Found old checking if need to erase it
|
||||
if(text.str().empty()) {
|
||||
// Erase the old label.
|
||||
delete current_label->second;
|
||||
current_label_map->second.erase(current_label);
|
||||
|
||||
// Restore the global label in the same spot, if any.
|
||||
|
@ -178,19 +175,18 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
global_label->recalculate();
|
||||
}
|
||||
} else {
|
||||
current_label->second->update_info(
|
||||
text, creator, tooltip, team_name, color, visible_in_fog, visible_in_shroud, immutable, category);
|
||||
res = current_label->second;
|
||||
current_label->second.update_info(
|
||||
text, creator, tooltip, team_name, color, visible_in_fog, visible_in_shroud, immutable, category);
|
||||
|
||||
res = ¤t_label->second;
|
||||
}
|
||||
} else if(!text.str().empty()) {
|
||||
// See if we will be replacing a global label.
|
||||
terrain_label* global_label = get_label_private(loc, "");
|
||||
|
||||
// Add the new label.
|
||||
res = new terrain_label(
|
||||
text, creator, team_name, loc, *this, color, visible_in_fog, visible_in_shroud, immutable, category, tooltip);
|
||||
|
||||
add_label(loc, res);
|
||||
res = add_label(
|
||||
*this, text, creator, team_name, loc, color, visible_in_fog, visible_in_shroud, immutable, category, tooltip);
|
||||
|
||||
// Hide the old label.
|
||||
if(global_label != nullptr) {
|
||||
|
@ -202,10 +198,13 @@ const terrain_label* map_labels::set_label(const map_location& loc,
|
|||
return res;
|
||||
}
|
||||
|
||||
void map_labels::add_label(const map_location& loc, terrain_label* new_label)
|
||||
template<typename... T>
|
||||
terrain_label* map_labels::add_label(T&&... args)
|
||||
{
|
||||
labels_[new_label->team_name()][loc] = new_label;
|
||||
categories_dirty = true;
|
||||
|
||||
terrain_label t(std::forward<T>(args)...);
|
||||
return &(*labels_[t.team_name()].emplace(t.location(), std::move(t)).first).second;
|
||||
}
|
||||
|
||||
void map_labels::clear(const std::string& team_name, bool force)
|
||||
|
@ -227,8 +226,7 @@ void map_labels::clear_map(label_map& m, bool force)
|
|||
{
|
||||
label_map::iterator i = m.begin();
|
||||
while(i != m.end()) {
|
||||
if(!i->second->immutable() || force) {
|
||||
delete i->second;
|
||||
if(!i->second.immutable() || force) {
|
||||
m.erase(i++);
|
||||
} else {
|
||||
++i;
|
||||
|
@ -240,18 +238,14 @@ void map_labels::clear_map(label_map& m, bool force)
|
|||
|
||||
void map_labels::clear_all()
|
||||
{
|
||||
for(team_label_map::value_type& m : labels_) {
|
||||
clear_map(m.second, true);
|
||||
}
|
||||
|
||||
labels_.clear();
|
||||
}
|
||||
|
||||
void map_labels::recalculate_labels()
|
||||
{
|
||||
for(team_label_map::value_type& m : labels_) {
|
||||
for(label_map::value_type& l : m.second) {
|
||||
l.second->recalculate();
|
||||
for(auto& m : labels_) {
|
||||
for(auto& l : m.second) {
|
||||
l.second.recalculate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -277,9 +271,9 @@ bool map_labels::visible_global_label(const map_location& loc) const
|
|||
|
||||
void map_labels::recalculate_shroud()
|
||||
{
|
||||
for(team_label_map::value_type& m : labels_) {
|
||||
for(label_map::value_type& l : m.second) {
|
||||
l.second->calculate_shroud();
|
||||
for(auto& m : labels_) {
|
||||
for(auto& l : m.second) {
|
||||
l.second.calculate_shroud();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -296,13 +290,13 @@ const std::vector<std::string>& map_labels::all_categories() const
|
|||
}
|
||||
|
||||
std::set<std::string> unique_cats;
|
||||
for(const team_label_map::value_type& m : labels_) {
|
||||
for(const label_map::value_type& l : m.second) {
|
||||
if(l.second->category().empty()) {
|
||||
for(const auto& m : labels_) {
|
||||
for(const auto& l : m.second) {
|
||||
if(l.second.category().empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unique_cats.insert("cat:" + l.second->category());
|
||||
unique_cats.insert("cat:" + l.second.category());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -313,11 +307,11 @@ const std::vector<std::string>& map_labels::all_categories() const
|
|||
}
|
||||
|
||||
/** Create a new label. */
|
||||
terrain_label::terrain_label(const t_string& text,
|
||||
terrain_label::terrain_label(const map_labels& parent,
|
||||
const t_string& text,
|
||||
const int creator,
|
||||
const std::string& team_name,
|
||||
const map_location& loc,
|
||||
const map_labels& parent,
|
||||
const color_t color,
|
||||
const bool visible_in_fog,
|
||||
const bool visible_in_shroud,
|
||||
|
|
|
@ -31,11 +31,12 @@ class terrain_label;
|
|||
class map_labels
|
||||
{
|
||||
public:
|
||||
typedef std::map<map_location, terrain_label*> label_map;
|
||||
typedef std::map<map_location, terrain_label> label_map;
|
||||
typedef std::map<std::string, label_map> team_label_map;
|
||||
|
||||
map_labels(const map_labels&);
|
||||
map_labels(const team*);
|
||||
|
||||
~map_labels();
|
||||
|
||||
map_labels& operator=(const map_labels&);
|
||||
|
@ -50,6 +51,7 @@ public:
|
|||
|
||||
// search a team-only label, if fails then try public labels
|
||||
const terrain_label* get_label(const map_location& loc) const;
|
||||
|
||||
const terrain_label* set_label(const map_location& loc,
|
||||
const t_string& text,
|
||||
const int creator = -1,
|
||||
|
@ -82,9 +84,11 @@ public:
|
|||
void clear_all();
|
||||
|
||||
private:
|
||||
void add_label(const map_location&, terrain_label*);
|
||||
template<typename... T>
|
||||
terrain_label* add_label(T&&... args);
|
||||
|
||||
void clear_map(label_map&, bool);
|
||||
|
||||
terrain_label* get_label_private(const map_location& loc, const std::string& 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.
|
||||
|
@ -103,11 +107,15 @@ private:
|
|||
class terrain_label
|
||||
{
|
||||
public:
|
||||
terrain_label(const t_string& text,
|
||||
/** Delete copy ctor and assignment ops. */
|
||||
terrain_label(const terrain_label&) = delete;
|
||||
terrain_label& operator=(const terrain_label&) = delete;
|
||||
|
||||
terrain_label(const map_labels& parent,
|
||||
const t_string& text,
|
||||
const int creator,
|
||||
const std::string& team_name,
|
||||
const map_location& loc,
|
||||
const map_labels& parent,
|
||||
const color_t color = font::NORMAL_COLOR,
|
||||
const bool visible_in_fog = true,
|
||||
const bool visible_in_shroud = false,
|
||||
|
@ -117,6 +125,9 @@ public:
|
|||
|
||||
terrain_label(const map_labels&, const config&);
|
||||
|
||||
/** Default move ctor. */
|
||||
terrain_label(terrain_label&&) = default;
|
||||
|
||||
~terrain_label();
|
||||
|
||||
void write(config& res) const;
|
||||
|
@ -193,9 +204,6 @@ public:
|
|||
void calculate_shroud();
|
||||
|
||||
private:
|
||||
terrain_label(const terrain_label&);
|
||||
const terrain_label& operator=(const terrain_label&);
|
||||
|
||||
void clear();
|
||||
void draw();
|
||||
bool hidden() const;
|
||||
|
|
Loading…
Add table
Reference in a new issue