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();
93 auto find_it =
id_map_.find(
id);
95 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
96 LOG_EH <<
"ignoring event handler for name='" << name <<
"' with id '" <<
id <<
"' because an event with that id already exists";
97 return {*
this,
nullptr};
101 if(name.empty() &&
id.empty()) {
102 static const char*
msg =
"[event] is missing name or id field";
105 ERR_EH <<
msg <<
" (run with --log-info=event_handler for more info)";
109 return {*
this,
nullptr};
113 auto handler = std::make_shared<event_handler>(name,
id);
114 handler->set_menu_item(is_menu_item);
115 handler->set_priority(priority);
116 handler->set_repeatable(repeat);
117 return {*
this, handler};
123 if(handler->empty()) {
127 const std::string&
names = handler->names_raw();
128 const std::string&
id = handler->id();
133 DBG_EH <<
"inserting event handler for name=" <<
names <<
" with id=" <<
id;
140 for(
const std::string& single_name : handler->names(
nullptr)) {
169 DBG_EH <<
"removing event handler with id " <<
id;
172 auto find_it =
id_map_.find(
id);
176 if(handler && !handler->disabled()) {
199 for(
const std::string& name :
utils::split(event_name)) {
213 auto find_it =
id_map_.find(
id);
214 if(find_it !=
id_map_.end() && !find_it->second.expired()) {
215 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 finish_adding_event_handler(const handler_ptr &new_handler)
void clean_up_expired_handlers(const std::string &event_name)
Removes all expired event handlers and any weak_ptrs to them.
static bool cmp(const handler_ptr &lhs, const handler_ptr &rhs)
Compare function to sort event handlers by priority.
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.
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::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="")