Author: silene
Date: Sat Apr 18 12:51:15 2009
New Revision: 35003

URL: http://svn.gna.org/viewcvs/wesnoth?rev=35003&view=rev
Log:
Removed the mutated_, skip_messages_, and rebuild_screen_ fields from class 
game_events::event_handler, as they do not have the same lifetime.

Modified:
    trunk/src/game_events.cpp
    trunk/src/game_events.hpp

Modified: trunk/src/game_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=35003&r1=35002&r2=35003&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Sat Apr 18 12:51:15 2009
@@ -53,6 +53,42 @@
 #define DBG_DP LOG_STREAM(debug, display)
 #define LOG_DP LOG_STREAM(info, display)
 #define ERR_CF LOG_STREAM(err, config)
+
+/**
+ * State when processing a flight of events or commands.
+ */
+struct event_context
+{
+       bool mutated;
+       bool skip_messages;
+};
+
+static event_context *current_context;
+
+/**
+ * Context state with automatic lifetime handling.
+ */
+struct scoped_context
+{
+       event_context *old_context;
+       event_context new_context;
+
+       scoped_context()
+               : old_context(current_context)
+       {
+               new_context.skip_messages = old_context ? 
old_context->skip_messages : false;
+               new_context.mutated = true;
+               current_context = &new_context;
+       }
+
+       ~scoped_context()
+       {
+               if (old_context) old_context->mutated |= new_context.mutated;
+               current_context = old_context;
+       }
+};
+
+static bool screen_needs_rebuild;
 
 namespace {
 
@@ -1815,7 +1851,7 @@
        }
 
        // Changing the terrain
-       WML_HANDLER_FUNCTION(terrain,handler,/*event_info*/,cfg)
+       WML_HANDLER_FUNCTION(terrain,/*handler*/,/*event_info*/,cfg)
        {
                const std::vector<map_location> locs = multiple_locs(cfg);
 
@@ -1858,12 +1894,12 @@
                                        };
                                }
                        }
-                       handler.set_rebuild_screen(true);
+                       screen_needs_rebuild = true;
                }
        }
 
        // Creating a mask of the terrain
-       WML_HANDLER_FUNCTION(terrain_mask,handler,/*event_info*/,cfg)
+       WML_HANDLER_FUNCTION(terrain_mask,/*handler*/,/*event_info*/,cfg)
        {
                map_location loc = cfg_to_loc(cfg, 1, 1);
 
@@ -1880,7 +1916,7 @@
                }
                bool border = utils::string_bool(cfg["border"]);
                game_map->overlay(mask, cfg.get_parsed_config(), loc.x, loc.y, 
border);
-               handler.set_rebuild_screen(true);
+               screen_needs_rebuild = true;
        }
 
     static bool try_add_unit_to_recall_list(const map_location& loc, const 
unit& u)
@@ -2703,7 +2739,7 @@
                }
        }
 
-       WML_HANDLER_FUNCTION(redraw,handler,/*event_info*/,cfg)
+       WML_HANDLER_FUNCTION(redraw,/*handler*/,/*event_info*/,cfg)
        {
                std::string side = cfg["side"];
                assert(state_of_game != NULL);
@@ -2712,8 +2748,8 @@
                        
clear_shroud(*screen,*game_map,*units,*teams,side_num-1);
                        (screen)->recalculate_minimap();
                }
-               if(handler.rebuild_screen()) {
-                       handler.set_rebuild_screen(false);
+               if (screen_needs_rebuild) {
+                       screen_needs_rebuild = false;
                        (screen)->recalculate_minimap();
                        (screen)->rebuild_all();
                }
@@ -2799,9 +2835,9 @@
 
 
                // Allow undo sets the flag saying whether the event has 
mutated the game to false
-       WML_HANDLER_FUNCTION(allow_undo,handler,/*event_info*/,/*cfg*/)
-       {
-               handler.set_mutated(false);
+       WML_HANDLER_FUNCTION(allow_undo,/*handler*/,/*event_info*/,/*cfg*/)
+       {
+               current_context->mutated = false;
        }
                // Conditional statements
        static void if_while_handler(bool is_if, game_events::event_handler& 
handler, const game_events::queued_event& event_info, const vconfig& cfg)
@@ -2999,7 +3035,7 @@
 
                bool has_input= (has_text_input || !menu_items.empty() );
 
-               if (handler.skip_messages() && !has_input ) {
+               if (current_context->skip_messages && !has_input ) {
                        return;
                }
 
@@ -3135,7 +3171,7 @@
                                        text_input_result = text_input_content;
                                }
                                if(dlg_result == gui2::twindow::CANCEL) {
-                                       handler.set_skip_messages(true);
+                                       current_context->skip_messages = true;
                                }
 
                                /**
@@ -3270,7 +3306,7 @@
        }
 
        // Experimental map replace
-       WML_HANDLER_FUNCTION(replace_map,handler,/*event_info*/,cfg)
+       WML_HANDLER_FUNCTION(replace_map,/*handler*/,/*event_info*/,cfg)
        {
                gamemap map(*game_map);
                try {
@@ -3309,7 +3345,7 @@
         }
                *game_map = map;
         screen->reload_map();
-               handler.set_rebuild_screen(true);
+               screen_needs_rebuild = true;
        }
 
        /** Handles all the different types of actions that can be triggered by 
an event. */
@@ -3436,24 +3472,22 @@
        }
 
        // The event hasn't been filtered out, so execute the handler.
-       // First reset the skip_messages to avoid the escape of the previous 
event
-       // to be carried over into the next.
-       handler.set_skip_messages(false);
-       handler.set_mutated(true);
+       scoped_context evc;
        handler.handle_event(ev);
+
        if(ev.name == "select") {
                state_of_game->last_selected = ev.loc1;
        }
 
-       if(handler.rebuild_screen()) {
-               handler.set_rebuild_screen(false);
+       if (screen_needs_rebuild) {
+               screen_needs_rebuild = false;
                (screen)->recalculate_minimap();
                (screen)->invalidate_all();
                (screen)->rebuild_all();
        }
 
 
-       return handler.mutated();
+       return current_context->mutated;
 }
 
 namespace game_events {

Modified: trunk/src/game_events.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.hpp?rev=35003&r1=35002&r2=35003&view=diff
==============================================================================
--- trunk/src/game_events.hpp (original)
+++ trunk/src/game_events.hpp Sat Apr 18 12:51:15 2009
@@ -94,9 +94,7 @@
                public:
                        event_handler(const vconfig& cfg, bool 
is_menu_item=false) :
                                
first_time_only_(utils::string_bool(cfg["first_time_only"],true)),
-                               disabled_(false), mutated_(true),
-                               skip_messages_(false), rebuild_screen_(false),
-                               is_menu_item_(is_menu_item), cfg_(cfg)
+                               disabled_(false), is_menu_item_(is_menu_item), 
cfg_(cfg)
                        {}
 
                        void read(const vconfig& cfg) { cfg_ = cfg; }
@@ -142,23 +140,12 @@
                        void handle_event(const queued_event& event_info,
                                        const vconfig cfg = vconfig());
 
-                       bool rebuild_screen() const {return rebuild_screen_;}
-                       bool mutated() const {return mutated_;}
-                       bool skip_messages() const {return skip_messages_;}
-
-                       void set_rebuild_screen(bool newval) {rebuild_screen_ = 
newval;}
-                       void set_mutated(bool newval) {mutated_ = newval;}
-                       void set_skip_messages(bool newval) {skip_messages_ = 
newval;}
-
                        const vconfig& get_vconfig() { return cfg_; }
                private:
                        void handle_event_command(const queued_event& 
event_info, const std::string& cmd, const vconfig cfg);
 
                        bool first_time_only_;
                        bool disabled_;
-                       bool mutated_;
-                       bool skip_messages_;
-                       bool rebuild_screen_;
                        bool is_menu_item_;
                        vconfig cfg_;
        };


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to