Skip to content

Commit

Permalink
Merge pull request #4169
Browse files Browse the repository at this point in the history
  • Loading branch information
loonycyborg committed Jul 16, 2019
2 parents 05d1092 + b72181c commit f8a18ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
27 changes: 6 additions & 21 deletions src/help/help_impl.cpp
Expand Up @@ -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;
}
Expand All @@ -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_;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()) {
Expand Down
29 changes: 13 additions & 16 deletions src/help/help_impl.hpp
Expand Up @@ -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() {}
};
Expand All @@ -82,31 +80,30 @@ class text_topic_generator: public topic_generator {
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(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;
const std::vector<std::string>& parsed_text() const;
};

/// A topic contains a title, an id and some text.
Expand All @@ -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;
Expand Down

0 comments on commit f8a18ac

Please sign in to comment.