The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wmi_container.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Declarations for a container for wml_menu_item.
18  */
19 
20 #ifndef GAME_EVENTS_WMI_CONTAINER_HPP_INCLUDED
21 #define GAME_EVENTS_WMI_CONTAINER_HPP_INCLUDED
22 
23 #include "utils/iterator.hpp"
24 
25 #include <boost/shared_ptr.hpp>
26 
27 #include <map>
28 #include <vector>
29 
30 class config;
31 class filter_context;
32 class game_data;
33 struct map_location;
34 class unit_map;
35 class vconfig;
36 
37 
38 namespace game_events
39 {
40 class wml_menu_item;
41 
42 
43 /// A container of wml_menu_item.
45  /// Pointers to our elements.
47  /// The underlying storage type.
48  typedef std::map<std::string, item_ptr> map_t;
49  /// The key for interaction with our iterators.
50  struct key {
51  /// Instructions for converting a map_t iterator to an item_ptr.
52  static const item_ptr & eval(const map_t::const_iterator & iter)
53  { return iter->second; }
54  };
55 
56 public:
57  // Typedefs required of a container:
58  typedef item_ptr value_type;
59  typedef value_type * pointer;
60  typedef value_type & reference;
61  typedef const value_type & const_reference;
62  typedef map_t::difference_type difference_type;
63  typedef map_t::size_type size_type;
64 
67 
68 
69 public:
70  wmi_container();
72 
73  /// Returns true if *this contains no data.
74  bool empty() const { return wml_menu_items_.empty(); }
75  /// Erases the item with the provided @a id.
76  size_type erase(const std::string & id);
77 
78  /// Fires the menu item with the given @a id.
79  bool fire_item(const std::string & id, const map_location & hex, game_data & gamedata, filter_context & fc, unit_map & units) const;
80  /// Returns the menu items that can be shown for the given location.
81  std::vector<std::pair<boost::shared_ptr<const wml_menu_item>, std::string> > get_items(const map_location& hex,
82  game_data & gamedata, filter_context & fc, unit_map & units, const_iterator start, const_iterator finish) const;
83  /// Range over all items by default
84  std::vector<std::pair<boost::shared_ptr<const wml_menu_item>, std::string> > get_items(const map_location& hex, game_data & gamedata, filter_context & fc, unit_map & units) const {
85  return get_items(hex, gamedata, fc, units, begin(), end());
86  }
87  /// Initializes the implicit event handlers for inlined [command]s.
88  void init_handlers() const;
89  void to_config(config& cfg) const;
90  /// Updates or creates (as appropriate) the menu item with the given @a id.
91  void set_item(const std::string& id, const vconfig& menu_item);
92  /// Sets the current menu items to the "menu_item" children of @a cfg.
93  void set_menu_items(const config& cfg);
94 
95 private:
96  /// Returns an iterator to a menu item with the given @a id, if one exists.
97  iterator find(const std::string & id) { return iterator(wml_menu_items_.find(id)); }
98 public:
99  /// Returns an iterator to a menu item with the given @a id, if one exists.
100  const_iterator find(const std::string & id) const { return const_iterator(wml_menu_items_.find(id)); }
101  // Iteration support:
102  iterator begin() { return iterator(wml_menu_items_.begin()); }
103  iterator end() { return iterator(wml_menu_items_.end()); }
104  const_iterator begin() const { return const_iterator(wml_menu_items_.begin()); }
105  const_iterator end() const { return const_iterator(wml_menu_items_.end()); }
106 
107  size_t size() const { return wml_menu_items_.size(); }
108 private: // data
110 };
111 
112 } // end namespace game_events
113 
114 #endif // GAME_EVENTS_WMI_CONTAINER_HPP_INCLUDED
115 
std::vector< std::pair< boost::shared_ptr< const wml_menu_item >, std::string > > get_items(const map_location &hex, game_data &gamedata, filter_context &fc, unit_map &units) const
Range over all items by default.
const_iterator begin() const
util::const_iterator_extend< value_type, map_t, key, key > const_iterator
std::map< std::string, item_ptr > map_t
The underlying storage type.
bool empty() const
Returns true if *this contains no data.
bool fire_item(const std::string &id, const map_location &hex, game_data &gamedata, filter_context &fc, unit_map &units) const
Fires the menu item with the given id.
iterator find(const std::string &id)
Returns an iterator to a menu item with the given id, if one exists.
game_data * gamedata
Definition: resources.cpp:22
const_iterator find(const std::string &id) const
Returns an iterator to a menu item with the given id, if one exists.
const value_type & const_reference
map_t::size_type size_type
void to_config(config &cfg) const
GLuint start
Definition: glew.h:1221
boost::shared_ptr< wml_menu_item > item_ptr
Pointers to our elements.
void set_menu_items(const config &cfg)
Sets the current menu items to the "menu_item" children of cfg.
Encapsulates the map of the game.
Definition: location.hpp:38
Domain specific events.
Definition: action_wml.cpp:93
A container of wml_menu_item.
std::vector< std::pair< boost::shared_ptr< const wml_menu_item >, std::string > > get_items(const map_location &hex, game_data &gamedata, filter_context &fc, unit_map &units, const_iterator start, const_iterator finish) const
Returns the menu items that can be shown for the given location.
util::iterator_extend< value_type, map_t, key, key > iterator
static const item_ptr & eval(const map_t::const_iterator &iter)
Instructions for converting a map_t iterator to an item_ptr.
void init_handlers() const
Initializes the implicit event handlers for inlined [command]s.
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
Container associating units to locations.
Definition: map.hpp:90
void set_item(const std::string &id, const vconfig &menu_item)
Updates or creates (as appropriate) the menu item with the given id.
This is a const_iterator class that extends an existing const_iterator by overriding dereference...
Definition: iterator.hpp:27
const_iterator end() const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
The key for interaction with our iterators.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unit_map * units
Definition: resources.cpp:35
size_type erase(const std::string &id)
Erases the item with the provided id.
map_t::difference_type difference_type
This is an iterator class that extends an existing iterator by overriding dereference.
Definition: iterator.hpp:49