23 #include <boost/algorithm/string.hpp> 28 #define DBG_NG LOG_STREAM(debug, log_engine) 29 #define LOG_NG LOG_STREAM(info, log_engine) 30 #define WRN_NG LOG_STREAM(warn, log_engine) 33 #define DBG_EH LOG_STREAM(debug, log_event_handler) 36 #define ERR_WML LOG_STREAM(err, log_wml) 37 #define DBG_WML LOG_STREAM(debug, log_wml) 54 const config& cfg =
h->get_config();
55 ss <<
"name=" << cfg[
"name"] <<
", with id=" << cfg[
"id"] <<
"; ";
58 DBG_EH <<
"active handlers are now " << ss.str() <<
"\n";
74 boost::replace_all(retval,
" ",
"_");
89 return find_it ==
by_name_.end() ? empty_list : find_it->second;
104 std::string name = cfg[
"name"];
105 std::string
id = cfg[
"id"];
109 auto find_it =
id_map_.find(
id);
111 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
112 DBG_EH <<
"ignoring event handler for name='" << name <<
"' with id '" <<
id <<
"'\n";
117 if(name.empty() &&
id.empty()) {
119 ERR_WML <<
"[event] is missing name or id field\n";
120 DBG_WML <<
"Content of that event:\n" << cfg.
debug() <<
"\n";
129 std::vector<std::string> standardized_names =
utils::split(name);
133 for(std::string& single_name : standardized_names) {
139 assert(!standardized_names.empty());
143 event_cfg[
"name"] = name;
149 DBG_EH <<
"inserting event handler for name=" << name <<
" with id=" <<
id <<
"\n";
150 active_.emplace_back(
new event_handler(std::move(event_cfg), is_menu_item, standardized_names));
160 for(
const std::string& single_name : standardized_names) {
183 DBG_EH <<
"removing event handler with id " <<
id <<
"\n";
186 auto find_it =
id_map_.find(
id);
190 if(handler && !handler->disabled()) {
217 for(
const std::string& name :
utils::split(event_name)) {
231 auto find_it =
id_map_.find(
id);
232 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
233 return find_it->second.lock();
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
handler_list & get(const std::string &name)
Access to the handlers with fixed event names, by event name.
void clean_up_expired_handlers(const std::string &event_name)
Removes all expired event handlers and any weak_ptrs to them.
id_map_t id_map_
Allows quick locating of handlers by id.
static lg::log_domain log_wml("wml")
void remove_event_handler(const std::string &id)
Removes an event handler, identified by its ID.
handler_list dynamic_
Active event handlers with variables in their event names.
static lg::log_domain log_engine("engine")
std::weak_ptr< event_handler > weak_handler_ptr
handler_queue_t active_
Active event handlers.
std::string id
Text to match against addon_info.tags()
void add_event_handler(const config &cfg, bool is_menu_item=false)
Adds an event handler.
map_t by_name_
Active event handlers with fixed event names, organized by event name.
bool might_contain_variables(const std::string &str)
Determines if a string might contain variables to interpolate.
std::list< weak_handler_ptr > handler_list
Define the handlers for the game's events mechanism.
std::vector< std::string > split(const config_attribute_value &val)
Standard logging facilities (interface).
void trim(std::string_view &s)
static lg::log_domain log_event_handler("event_handler")
retval
Default window/dialog return values.
std::shared_ptr< event_handler > handler_ptr
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler, identified by its ID.
A config object defines a single node in a WML file, with access to child nodes.
std::string debug() const
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.