wesnoth-cvs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-cvs-commits] wesnoth/src help.cpp help.hpp


From: Guillaume Melquiond
Subject: [Wesnoth-cvs-commits] wesnoth/src help.cpp help.hpp
Date: Sat, 30 Oct 2004 16:26:26 -0400

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     Guillaume Melquiond <address@hidden>    04/10/30 20:20:53

Modified files:
        src            : help.cpp help.hpp 

Log message:
        First step in improving the help system speed. Topics (unit topics only 
for the time being) are now created on the fly; they are generated by virtual 
function objects, owned by pseudo shared pointers, and their value is cached 
once computed (I agree, it's a bit ugly). The wrong code identation is 
intended: it considerably reduces the size of the patch.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/help.cpp.diff?tr1=1.42&tr2=1.43&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/help.hpp.diff?tr1=1.14&tr2=1.15&r1=text&r2=text

Patches:
Index: wesnoth/src/help.cpp
diff -u wesnoth/src/help.cpp:1.42 wesnoth/src/help.cpp:1.43
--- wesnoth/src/help.cpp:1.42   Sat Oct 30 19:09:23 2004
+++ wesnoth/src/help.cpp        Sat Oct 30 20:20:53 2004
@@ -452,6 +452,43 @@
        return empty_string;
 }
 
+struct topic_generator
+{
+       topic_generator(): count(1) {}
+       virtual std::string operator()() const = 0;
+       virtual ~topic_generator() {}
+private:
+       unsigned count;
+       friend class topic_text;
+};
+
+topic_text::~topic_text() {
+       if (generator_ && --generator_->count == 0)
+               delete generator_;
+}
+
+topic_text::topic_text(topic_text const &t): text_(t.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;
+}
+
+topic_text::operator std::string() const {
+       if (generator_) {
+               text_ = (*generator_)();
+               if (--generator_->count == 0)
+                       delete generator_;
+               generator_ = NULL;
+       }
+       return text_;
+}
+
 std::vector<topic> generate_weapon_special_topics() {
        std::vector<topic> topics;
        if (game_info == NULL) {
@@ -528,26 +565,12 @@
        return topics;
 }
 
-std::vector<topic> generate_unit_topics() {
-       std::vector<topic> topics;
-       if (game_info == NULL) {
-               return topics;
-       }
-       for(game_data::unit_type_map::const_iterator i = 
game_info->unit_types.begin();
-           i != game_info->unit_types.end(); i++) {
-               const unit_type &type = (*i).second;
-               UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
-               if (desc_type == NO_DESCRIPTION) {
-                       continue;
-               }
-               const std::string lang_name = type.language_name();
-               const std::string id = type.id();
-               topic unit_topic(lang_name, std::string("unit_") + id, "");
+struct unit_topic_generator: topic_generator
+{
+       unit_topic_generator(unit_type const &t): type(t) {}
+       unit_type type;
+       virtual std::string operator()() const {
                std::stringstream ss;
-               if (desc_type == NON_REVEALING_DESCRIPTION) {
-                       
-               }
-               else if (desc_type == FULL_DESCRIPTION) {
                        const std::string detailed_description = 
type.unit_description();
                        const unit_type& female_type = 
type.get_gender_unit_type(unit_race::FEMALE);
                        const unit_type& male_type = 
type.get_gender_unit_type(unit_race::MALE);
@@ -784,11 +807,31 @@
                                }
                                ss << generate_table(table);
                        }
+               return ss.str();
+       }
+};
+
+std::vector<topic> generate_unit_topics() {
+       std::vector<topic> topics;
+       if (game_info == NULL) {
+               return topics;
+       }
+       for(game_data::unit_type_map::const_iterator i = 
game_info->unit_types.begin();
+           i != game_info->unit_types.end(); i++) {
+               const unit_type &type = (*i).second;
+               UNIT_DESCRIPTION_TYPE desc_type = description_type(type);
+               if (desc_type == NO_DESCRIPTION) {
+                       continue;
                }
-               else {
+               const std::string lang_name = type.language_name();
+               const std::string id = type.id();
+               topic unit_topic(lang_name, std::string("unit_") + id, "");
+               if (desc_type == NON_REVEALING_DESCRIPTION) {
+               } else if (desc_type == FULL_DESCRIPTION) {
+                       unit_topic.text = new unit_topic_generator(type);
+               } else {
                        assert(false);
                }
-               unit_topic.text = ss.str();
                topics.push_back(unit_topic);
        }
        return topics;
@@ -926,7 +969,7 @@
 }
 
 
-section::section(const std::string _title, const std::string _id, const 
topic_list &_topics,
+section::section(const std::string &_title, const std::string &_id, const 
topic_list &_topics,
                const std::vector<section> &_sections)
        : title(_title), id(_id), topics(_topics) {
        std::transform(_sections.begin(), _sections.end(), 
std::back_inserter(sections),
@@ -1195,7 +1238,7 @@
 bool help_menu::visible_item::operator==(const visible_item &vis_item) const {
        return t == vis_item.t && sec == vis_item.sec;
 }
-       
+
 help_text_area::help_text_area(display &disp, const section &toplevel)
        : gui::widget(disp), disp_(disp), toplevel_(toplevel), 
shown_topic_(NULL),
          title_spacing_(16), curr_loc_(0, 0),
Index: wesnoth/src/help.hpp
diff -u wesnoth/src/help.hpp:1.14 wesnoth/src/help.hpp:1.15
--- wesnoth/src/help.hpp:1.14   Sun Aug 15 17:36:07 2004
+++ wesnoth/src/help.hpp        Sat Oct 30 20:20:53 2004
@@ -44,23 +44,41 @@
 
 typedef std::vector<section *> section_list;
 
+struct topic_generator;
+
+class topic_text {
+       mutable std::string text_;
+       mutable topic_generator *generator_;
+public:
+       ~topic_text();
+       topic_text(): generator_(NULL) {}
+       topic_text(std::string const &t): text_(t), generator_(NULL) {}
+       explicit topic_text(topic_generator *g): generator_(g) {}
+       topic_text &operator=(topic_generator *g);
+       topic_text(topic_text const &t);
+       operator std::string() const;
+};
+
 /// A topic contains a title, an id and some text.
 struct topic {
-       topic(const std::string _title, const std::string _id, const 
std::string _text)
+       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)
+               : title(_title), id(_id), text(g) {}
        topic() : title(""), id(""), text("") {}
        /// Two topics are equal if their IDs are equal.
        bool operator==(const topic &) const;
        /// Comparison on the ID.
        bool operator<(const topic &) const;
-       std::string title, id, text;
+       std::string title, id;
+       topic_text text;
 };
 
 typedef std::list<topic> topic_list;
 
 /// A section contains topics and sections along with title and ID.
 struct section {
-       section(const std::string _title, const std::string _id, const 
topic_list &_topics,
+       section(const std::string &_title, const std::string &_id, const 
topic_list &_topics,
                        const std::vector<section> &_sections);
        section() : title(""), id("") {}
        section(const section&);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]