24 #include <boost/algorithm/string.hpp>
27 #define DBG_NG LOG_STREAM(debug, log_engine)
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 #define WRN_NG LOG_STREAM(warn, log_engine)
32 #define ERR_EH LOG_STREAM(err, log_event_handler)
33 #define LOG_EH LOG_STREAM(info, log_event_handler)
34 #define DBG_EH LOG_STREAM(debug, log_event_handler)
37 #define ERR_WML LOG_STREAM(err, log_wml)
38 #define DBG_WML LOG_STREAM(debug, log_wml)
55 ss <<
"name=" <<
h->names_raw() <<
", with id=" <<
h->id() <<
"; ";
58 DBG_EH <<
"active handlers are now " << ss.str();
74 boost::replace_all(
retval,
" ",
"_");
81 return lhs->priority() < rhs->priority();
94 return find_it ==
by_name_.end() ? empty_list : find_it->second;
106 auto find_it =
id_map_.find(
id);
108 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
109 LOG_EH <<
"ignoring event handler for name='" << name <<
"' with id '" <<
id <<
"' because an event with that id already exists";
110 return {*
this,
nullptr};
114 if(name.empty() &&
id.empty()) {
115 static const char*
msg =
"[event] is missing name or id field";
118 ERR_EH <<
msg <<
" (run with --log-info=event_handler for more info)";
122 return {*
this,
nullptr};
126 auto handler = std::make_shared<event_handler>(name,
id);
127 handler->set_menu_item(is_menu_item);
128 handler->set_priority(priority);
129 handler->set_repeatable(repeat);
130 return {*
this, handler};
136 if(handler->empty()) {
140 const std::string&
names = handler->names_raw();
141 const std::string&
id = handler->id();
146 DBG_EH <<
"inserting event handler for name=" <<
names <<
" with id=" <<
id;
153 for(
const std::string& single_name : handler->names(
nullptr)) {
182 DBG_EH <<
"removing event handler with id " <<
id;
185 auto find_it =
id_map_.find(
id);
189 if(handler && !handler->disabled()) {
212 for(
const std::string& name :
utils::split(event_name)) {
226 auto find_it =
id_map_.find(
id);
227 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
228 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, double priority=0., 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.
static bool cmp(const handler_ptr lhs, const handler_ptr rhs)
Compare function to sort event handlers by priority.
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)
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
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="")