mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-07 21:35:37 +00:00
Added support for dynamic report generators.
This commit is contained in:
parent
ef36d05b20
commit
19c1b89910
@ -38,6 +38,7 @@
|
||||
#include "map_label.hpp"
|
||||
#include "map_exception.hpp"
|
||||
#include "replay.hpp"
|
||||
#include "reports.hpp"
|
||||
#include "resources.hpp"
|
||||
#include "scripting/lua.hpp"
|
||||
#include "sound.hpp"
|
||||
@ -3098,6 +3099,7 @@ namespace game_events {
|
||||
manager_running = false;
|
||||
events_queue.clear();
|
||||
event_handlers.clear();
|
||||
reports::reset_generators();
|
||||
delete resources::lua_kernel;
|
||||
resources::lua_kernel = NULL;
|
||||
unit_wml_ids.clear();
|
||||
|
@ -90,23 +90,31 @@ static std::string flush(std::ostringstream &s)
|
||||
return r;
|
||||
}
|
||||
|
||||
struct report_generator
|
||||
typedef config (*generator_function)(const report_data &);
|
||||
|
||||
struct static_report_generator
|
||||
{
|
||||
typedef config (*fun)(report_data const &);
|
||||
fun generator;
|
||||
generator_function generator;
|
||||
bool for_units;
|
||||
report_generator(fun g, bool u): generator(g), for_units(u) {}
|
||||
};
|
||||
|
||||
typedef std::map<std::string, report_generator> report_generators;
|
||||
static report_generators static_generators;
|
||||
struct dynamic_report_generator
|
||||
{
|
||||
reports::generator *generator;
|
||||
bool for_units;
|
||||
};
|
||||
|
||||
typedef std::map<std::string, static_report_generator> static_report_generators;
|
||||
typedef std::map<std::string, dynamic_report_generator> dynamic_report_generators;
|
||||
static static_report_generators static_generators;
|
||||
static dynamic_report_generators dynamic_generators;
|
||||
|
||||
struct report_generator_helper
|
||||
{
|
||||
report_generator_helper(const char *name, report_generator::fun g, bool u)
|
||||
report_generator_helper(const char *name, generator_function g, bool u)
|
||||
{
|
||||
static_generators.insert(report_generators::value_type(name,
|
||||
report_generator(g, u)));
|
||||
static_report_generator rg = { g, u };
|
||||
static_generators.insert(static_report_generators::value_type(name, rg));
|
||||
}
|
||||
};
|
||||
|
||||
@ -862,11 +870,34 @@ REPORT_GENERATOR(report_countdown, false, data)
|
||||
return text_report(str.str());
|
||||
}
|
||||
|
||||
void reports::reset_generators()
|
||||
{
|
||||
foreach (dynamic_report_generators::value_type &rg, dynamic_generators) {
|
||||
delete rg.second.generator;
|
||||
}
|
||||
dynamic_generators.clear();
|
||||
}
|
||||
|
||||
void reports::register_generator(const std::string &name, reports::generator *g, bool for_units)
|
||||
{
|
||||
dynamic_report_generator rg = { g, for_units };
|
||||
std::pair<dynamic_report_generators::iterator, bool> ib =
|
||||
dynamic_generators.insert(std::make_pair(name, rg));
|
||||
if (!ib.second) {
|
||||
delete ib.first->second.generator;
|
||||
ib.first->second = rg;
|
||||
}
|
||||
}
|
||||
|
||||
config reports::generate_report(const std::string &name, const report_data &data)
|
||||
{
|
||||
report_generators::const_iterator i = static_generators.find(name);
|
||||
if (i == static_generators.end()) return report();
|
||||
return (i->second.generator)(data);
|
||||
dynamic_report_generators::const_iterator i = dynamic_generators.find(name);
|
||||
if (i != dynamic_generators.end())
|
||||
return i->second.generator->generate(data);
|
||||
static_report_generators::const_iterator j = static_generators.find(name);
|
||||
if (j != static_generators.end())
|
||||
return j->second.generator(data);
|
||||
return report();
|
||||
}
|
||||
|
||||
static std::set<std::string> unit_reports, status_reports;
|
||||
@ -875,7 +906,10 @@ const std::set<std::string> &reports::report_list(bool for_units)
|
||||
{
|
||||
std::set<std::string> &l = *(for_units ? &unit_reports : &status_reports);
|
||||
if (!l.empty()) return l;
|
||||
foreach (const report_generators::value_type &v, static_generators) {
|
||||
foreach (const static_report_generators::value_type &v, static_generators) {
|
||||
if (v.second.for_units == for_units) l.insert(v.first);
|
||||
}
|
||||
foreach (const dynamic_report_generators::value_type &v, dynamic_generators) {
|
||||
if (v.second.for_units == for_units) l.insert(v.first);
|
||||
}
|
||||
return l;
|
||||
|
@ -35,6 +35,15 @@ struct report_data
|
||||
bool show_everything;
|
||||
};
|
||||
|
||||
struct generator
|
||||
{
|
||||
virtual config generate(report_data const &) = 0;
|
||||
virtual ~generator() {}
|
||||
};
|
||||
|
||||
void reset_generators();
|
||||
void register_generator(const std::string &name, generator *, bool for_units);
|
||||
|
||||
config generate_report(const std::string &name, const report_data &data);
|
||||
|
||||
const std::set<std::string> &report_list(bool for_units);
|
||||
|
Loading…
x
Reference in New Issue
Block a user