WMI Container: removed iterator and container interface
This probably existed to allow simple range-for code in pre-C++11 days when we were using the BOOST_FOREACH macro. IIRC that required an iterator. However, with C++11 and range-for, it's much simple to simply specify the `.second` when necessary than having a whole convoluted interface and iterator extension just to avoid having to type that. This also changes the return type of 'erase' to bool. It also leaves the empty() and size() functions, since those might be useful.
This commit is contained in:
parent
e6fc4cb0b6
commit
08cdfba96b
2 changed files with 22 additions and 58 deletions
|
@ -51,15 +51,15 @@ wmi_container::~wmi_container()
|
|||
}
|
||||
|
||||
/** Erases the item with id @a key. */
|
||||
wmi_container::size_type wmi_container::erase(const std::string& id)
|
||||
bool wmi_container::erase(const std::string& id)
|
||||
{
|
||||
// Locate the item to erase.
|
||||
const map_t::iterator iter = wml_menu_items_.find(id);
|
||||
const auto iter = wml_menu_items_.find(id);
|
||||
|
||||
if(iter == wml_menu_items_.end()) {
|
||||
WRN_NG << "Trying to remove non-existent menu item '" << id << "'; ignoring." << std::endl;
|
||||
// No such item.
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clean up our bookkeeping.
|
||||
|
@ -68,7 +68,7 @@ wmi_container::size_type wmi_container::erase(const std::string& id)
|
|||
// Remove the item from the map.
|
||||
wml_menu_items_.erase(iter);
|
||||
|
||||
return 1; // Erased one item.
|
||||
return true; // Erased one item.
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,11 +81,10 @@ bool wmi_container::fire_item(
|
|||
const std::string& id, const map_location& hex, game_data& gamedata, filter_context& fc, unit_map& units) const
|
||||
{
|
||||
// Does this item exist?
|
||||
const_iterator iter = find(id);
|
||||
if(iter == end()) {
|
||||
item_ptr wmi = get_item(id);
|
||||
if(!wmi) {
|
||||
return false;
|
||||
}
|
||||
const wml_menu_item& wmi = **iter;
|
||||
|
||||
// Prepare for can show().
|
||||
gamedata.get_variable("x1") = hex.wml_x();
|
||||
|
@ -93,8 +92,8 @@ bool wmi_container::fire_item(
|
|||
scoped_xy_unit highlighted_unit("unit", hex, units);
|
||||
|
||||
// Can this item be shown?
|
||||
if(wmi.can_show(hex, gamedata, fc)) {
|
||||
wmi.fire_event(hex, gamedata);
|
||||
if(wmi->can_show(hex, gamedata, fc)) {
|
||||
wmi->fire_event(hex, gamedata);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -123,7 +122,9 @@ void wmi_container::get_items(const map_location& hex,
|
|||
scoped_xy_unit highlighted_unit("unit", hex, units);
|
||||
|
||||
// Check each menu item.
|
||||
for(const item_ptr& item : *this) {
|
||||
for(const auto& item_pair : wml_menu_items_) {
|
||||
item_ptr item = item_pair.second;
|
||||
|
||||
// Can this item be shown?
|
||||
if(item->use_wml_menu() && (!item->is_synced() || resources::controller->can_use_synced_wml_menu())
|
||||
&& item->can_show(hex, gamedata, fc)) {
|
||||
|
@ -132,7 +133,6 @@ void wmi_container::get_items(const map_location& hex,
|
|||
descriptions.emplace_back(config_of("id", item->menu_text()));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
wmi_container::item_ptr wmi_container::get_item(const std::string& id) const
|
||||
|
@ -162,9 +162,10 @@ void wmi_container::init_handlers() const
|
|||
unsigned wmi_count = 0;
|
||||
|
||||
// Loop through each menu item.
|
||||
for(const item_ptr& wmi : *this) {
|
||||
for(const auto& item : wml_menu_items_) {
|
||||
// If this menu item has a [command], add a handler for it.
|
||||
wmi->init_handler();
|
||||
item.second->init_handler();
|
||||
|
||||
// Count the menu items (for the diagnostic message).
|
||||
++wmi_count;
|
||||
}
|
||||
|
@ -178,9 +179,9 @@ void wmi_container::init_handlers() const
|
|||
void wmi_container::to_config(config& cfg) const
|
||||
{
|
||||
// Loop through our items.
|
||||
for(const item_ptr& item : *this) {
|
||||
for(const auto& item : wml_menu_items_) {
|
||||
// Add this item as a child of cfg.
|
||||
item->to_config(cfg.add_child("menu_item"));
|
||||
item.second->to_config(cfg.add_child("menu_item"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,7 +192,7 @@ void wmi_container::set_item(const std::string& id, const vconfig& menu_item)
|
|||
{
|
||||
// Try to insert a dummy value. This combines looking for an existing
|
||||
// entry with insertion.
|
||||
map_t::iterator add_it = wml_menu_items_.insert(map_t::value_type(id, item_ptr())).first;
|
||||
auto add_it = wml_menu_items_.emplace(id, item_ptr()).first;
|
||||
|
||||
if(add_it->second)
|
||||
// Create a new menu item based on the old. This leaves the old item
|
||||
|
|
|
@ -19,8 +19,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "utils/iterator.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
@ -33,48 +31,24 @@ struct map_location;
|
|||
class unit_map;
|
||||
class vconfig;
|
||||
|
||||
|
||||
namespace game_events
|
||||
{
|
||||
class wml_menu_item;
|
||||
|
||||
|
||||
/// A container of wml_menu_item.
|
||||
class wmi_container{
|
||||
class wmi_container
|
||||
{
|
||||
public:
|
||||
/// Pointers to our elements.
|
||||
typedef std::shared_ptr<wml_menu_item> item_ptr;
|
||||
private:
|
||||
/// The underlying storage type.
|
||||
typedef std::map<std::string, item_ptr> map_t;
|
||||
/// The key for interaction with our iterators.
|
||||
struct key {
|
||||
/// Instructions for converting a map_t iterator to an item_ptr.
|
||||
static const item_ptr & eval(const map_t::const_iterator & iter)
|
||||
{ return iter->second; }
|
||||
};
|
||||
|
||||
public:
|
||||
// Typedefs required of a container:
|
||||
typedef item_ptr value_type;
|
||||
typedef value_type * pointer;
|
||||
typedef value_type & reference;
|
||||
typedef const value_type & const_reference;
|
||||
typedef map_t::difference_type difference_type;
|
||||
typedef map_t::size_type size_type;
|
||||
|
||||
typedef utils::iterator_extend <value_type, map_t, key, key> iterator;
|
||||
typedef utils::const_iterator_extend<value_type, map_t, key, key> const_iterator;
|
||||
|
||||
|
||||
public:
|
||||
wmi_container();
|
||||
~wmi_container();
|
||||
|
||||
/// Returns true if *this contains no data.
|
||||
bool empty() const { return wml_menu_items_.empty(); }
|
||||
/// Erases the item with the provided @a id.
|
||||
size_type erase(const std::string & id);
|
||||
bool erase(const std::string & id);
|
||||
|
||||
/// Fires the menu item with the given @a id.
|
||||
bool fire_item(const std::string & id, const map_location & hex, game_data & gamedata, filter_context & fc, unit_map & units) const;
|
||||
|
@ -100,21 +74,10 @@ public:
|
|||
/// Sets the current menu items to the "menu_item" children of @a cfg.
|
||||
void set_menu_items(const config& cfg);
|
||||
|
||||
private:
|
||||
/// Returns an iterator to a menu item with the given @a id, if one exists.
|
||||
iterator find(const std::string & id) { return iterator(wml_menu_items_.find(id)); }
|
||||
public:
|
||||
/// Returns an iterator to a menu item with the given @a id, if one exists.
|
||||
const_iterator find(const std::string & id) const { return const_iterator(wml_menu_items_.find(id)); }
|
||||
// Iteration support:
|
||||
iterator begin() { return iterator(wml_menu_items_.begin()); }
|
||||
iterator end() { return iterator(wml_menu_items_.end()); }
|
||||
const_iterator begin() const { return const_iterator(wml_menu_items_.begin()); }
|
||||
const_iterator end() const { return const_iterator(wml_menu_items_.end()); }
|
||||
|
||||
size_t size() const { return wml_menu_items_.size(); }
|
||||
|
||||
private: // data
|
||||
map_t wml_menu_items_;
|
||||
std::map<std::string, item_ptr> wml_menu_items_;
|
||||
};
|
||||
|
||||
} // end namespace game_events
|
||||
|
|
Loading…
Add table
Reference in a new issue