23 #include <boost/algorithm/string.hpp>
26 #define DBG_NG LOG_STREAM(debug, log_engine)
27 #define LOG_NG LOG_STREAM(info, log_engine)
28 #define WRN_NG LOG_STREAM(warn, log_engine)
31 #define ERR_EH LOG_STREAM(err, log_event_handler)
32 #define LOG_EH LOG_STREAM(info, log_event_handler)
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 ss <<
"name=" <<
h->names_raw() <<
", with id=" <<
h->id() <<
"; ";
57 DBG_EH <<
"active handlers are now " << ss.str();
73 boost::replace_all(
retval,
" ",
"_");
88 return find_it ==
by_name_.end() ? empty_list : find_it->second;
100 auto find_it =
id_map_.find(
id);
102 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
103 LOG_EH <<
"ignoring event handler for name='" << name <<
"' with id '" <<
id <<
"' because an event with that id already exists";
104 return {*
this,
nullptr};
108 if(name.empty() &&
id.empty()) {
109 static const char*
msg =
"[event] is missing name or id field";
112 ERR_EH <<
msg <<
" (run with --log-info=event_handler for more info)";
116 return {*
this,
nullptr};
120 auto handler = std::make_shared<event_handler>(name,
id);
121 handler->set_menu_item(is_menu_item);
122 handler->set_repeatable(repeat);
123 return {*
this, handler};
129 if(handler->empty()) {
133 const std::string&
names = handler->names_raw();
134 const std::string&
id = handler->id();
139 DBG_EH <<
"inserting event handler for name=" <<
names <<
" with id=" <<
id;
146 for(
const std::string& single_name : handler->names(
nullptr)) {
174 DBG_EH <<
"removing event handler with id " <<
id;
177 auto find_it =
id_map_.find(
id);
181 if(handler && !handler->disabled()) {
208 for(
const std::string& name :
utils::split(event_name)) {
222 auto find_it =
id_map_.find(
id);
223 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
224 return find_it->second.lock();
std::vector< std::string > names
map_t by_name_
Active event handlers with fixed event names, organized by event name.
void clean_up_expired_handlers(const std::string &event_name)
Removes all expired event handlers and any weak_ptrs to them.
handler_list & get(const std::string &name)
Access to the handlers with fixed event names, by event name.
void remove_event_handler(const std::string &id)
Removes an event handler, identified by its ID.
pending_event_handler add_event_handler(const std::string &name, const std::string &id, bool repeat, bool is_menu_item=false)
Adds an event handler.
void finish_adding_event_handler(handler_ptr new_handler)
handler_list dynamic_
Active event handlers with variables in their event names.
id_map_t id_map_
Allows quick locating of handlers by id.
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler, identified by its ID.
handler_queue_t active_
Active event handlers.
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
Represents a handler that is about to be added to the events manager but is still waiting for some da...
bool valid() const
Check if this handler is valid.
std::string id
Text to match against addon_info.tags()
Define the handlers for the game's events mechanism.
Standard logging facilities (interface).
static lg::log_domain log_engine("engine")
static lg::log_domain log_event_handler("event_handler")
static lg::log_domain log_wml("wml")
std::list< weak_handler_ptr > handler_list
std::shared_ptr< event_handler > handler_ptr
std::weak_ptr< event_handler > weak_handler_ptr
retval
Default window/dialog return values.
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
void trim(std::string_view &s)
bool might_contain_variables(const std::string &str)
Determines if a string might contain variables to interpolate.
std::vector< std::string > split(const config_attribute_value &val)
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")