Merge branch 'help_ref_count_master' of https://github.com/stevecotton/wesnoth into stevecotton-help_ref_count_master

This commit is contained in:
loonycyborg 2019-07-16 16:47:23 +03:00
commit b72181c0e6
2 changed files with 20 additions and 38 deletions

View file

@ -325,22 +325,8 @@ std::string generate_topic_text(const std::string &generator, const config *help
return empty_string;
}
topic_text::~topic_text()
topic_text& topic_text::operator=(std::shared_ptr<topic_generator> g)
{
if (generator_ && --generator_->count == 0)
delete generator_;
}
topic_text::topic_text(const topic_text& t): parsed_text_(t.parsed_text_), generator_(t.generator_)
{
if (generator_)
++generator_->count;
}
topic_text &topic_text::operator=(topic_generator *g)
{
if (generator_ && --generator_->count == 0)
delete generator_;
generator_ = g;
return *this;
}
@ -349,9 +335,8 @@ const std::vector<std::string>& topic_text::parsed_text() const
{
if (generator_) {
parsed_text_ = parse_text((*generator_)());
if (--generator_->count == 0)
delete generator_;
generator_ = nullptr;
// This caches the result, so doesn't need the generator any more
generator_.reset();
}
return parsed_text_;
}
@ -855,7 +840,7 @@ void generate_terrain_sections(const config* /*help_cfg*/, section& sec, int /*l
topic terrain_topic;
terrain_topic.title = info.editor_name();
terrain_topic.id = hidden_symbol(hidden) + terrain_prefix + info.id();
terrain_topic.text = new terrain_topic_generator(info);
terrain_topic.text = std::make_shared<terrain_topic_generator>(info);
t_translation::ter_list base_terrains = tdata->underlying_union_terrain(t);
for (const t_translation::terrain_code& base : base_terrains) {
@ -900,7 +885,7 @@ void generate_unit_sections(const config* /*help_cfg*/, section& sec, int level,
const std::string var_ref = hidden_symbol(var_type.hide_help()) + variation_prefix + var_type.id() + "_" + variation_id;
topic var_topic(topic_name, var_ref, "");
var_topic.text = new unit_topic_generator(var_type, variation_id);
var_topic.text = std::make_shared<unit_topic_generator>(var_type, variation_id);
base_unit.topics.push_back(var_topic);
}
@ -938,7 +923,7 @@ std::vector<topic> generate_unit_topics(const bool sort_generated, const std::st
const std::string real_prefix = type.show_variations_in_help() ? ".." : "";
const std::string ref_id = hidden_symbol(type.hide_help()) + real_prefix + unit_prefix + type.id();
topic unit_topic(type_name, ref_id, "");
unit_topic.text = new unit_topic_generator(type);
unit_topic.text = std::make_shared<unit_topic_generator>(type);
topics.push_back(unit_topic);
if (!type.hide_help()) {

View file

@ -62,10 +62,8 @@ typedef std::vector<section *> section_list;
/// Generate a topic text on the fly.
class topic_generator
{
unsigned count;
friend class topic_text;
public:
topic_generator(): count(1) {}
topic_generator() = default;
virtual std::string operator()() const = 0;
virtual ~topic_generator() {}
};
@ -82,31 +80,30 @@ public:
class topic_text
{
mutable std::vector< std::string > parsed_text_;
mutable topic_generator *generator_;
mutable std::shared_ptr<topic_generator> generator_;
public:
~topic_text();
topic_text():
parsed_text_(),
generator_(nullptr)
{
}
topic_text() = default;
~topic_text() = default;
topic_text(const std::string& t):
parsed_text_(),
generator_(new text_topic_generator(t))
generator_(std::make_shared<text_topic_generator>(t))
{
}
explicit topic_text(topic_generator *g):
explicit topic_text(std::shared_ptr<topic_generator> g):
parsed_text_(),
generator_(g)
{
}
topic_text &operator=(topic_generator *g);
topic_text(const topic_text& t);
topic_text& operator=(const topic_text& t) = default;
const std::vector<std::string>& parsed_text() const;
topic_text(const topic_text& t) = default;
topic_text(topic_text&& t) = default;
topic_text& operator=(topic_text&& t) = default;
topic_text& operator=(const topic_text& t) = default;
topic_text& operator=(std::shared_ptr<topic_generator> g);
const std::vector<std::string>& parsed_text() const;
};
/// A topic contains a title, an id and some text.
@ -128,7 +125,7 @@ struct topic
topic(const std::string &_title, const std::string &_id, const std::string &_text)
: title(_title), id(_id), text(_text) {}
topic(const std::string &_title, const std::string &_id, topic_generator *g)
topic(const std::string &_title, const std::string &_id, std::shared_ptr<topic_generator> g)
: title(_title), id(_id), text(g) {}
/// Two topics are equal if their IDs are equal.
bool operator==(const topic &) const;