From 469fcc9c5279635dcd4565b51e6927480fb6ebc0 Mon Sep 17 00:00:00 2001 From: Steve Cotton Date: Mon, 15 Jul 2019 16:23:52 +0200 Subject: [PATCH] Convert help_impl's ref-counting to std::shared_ptr This is part of #4166, as topic_text wasn't consistent with the Rule of Three. --- src/help/help_impl.cpp | 27 ++++++--------------------- src/help/help_impl.hpp | 30 ++++++++++++++---------------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/help/help_impl.cpp b/src/help/help_impl.cpp index 8a8ca04cf9f8..1e6f3532f13e 100644 --- a/src/help/help_impl.cpp +++ b/src/help/help_impl.cpp @@ -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 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& 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(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(var_type, variation_id); base_unit.topics.push_back(var_topic); } @@ -938,7 +923,7 @@ std::vector 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(type); topics.push_back(unit_topic); if (!type.hide_help()) { diff --git a/src/help/help_impl.hpp b/src/help/help_impl.hpp index 43687953efed..d12fe738bf4e 100644 --- a/src/help/help_impl.hpp +++ b/src/help/help_impl.hpp @@ -62,10 +62,8 @@ typedef std::vector
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,30 +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 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(t)) { } - explicit topic_text(topic_generator *g): + explicit topic_text(std::shared_ptr g): parsed_text_(), generator_(g) { } - topic_text &operator=(topic_generator *g); - topic_text(const topic_text& t); - const std::vector& 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 g); + + const std::vector& parsed_text() const; }; /// A topic contains a title, an id and some text. @@ -127,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 g) : title(_title), id(_id), text(g) {} /// Two topics are equal if their IDs are equal. bool operator==(const topic &) const;