# HG changeset patch # User John W. Eaton # Date 1364415155 14400 # Node ID b28ae106e316a3feb10481ed1657788de22814ab # Parent 8430ea8c1594461c6746698f691c1ea1935a7cdc use class for lists of input hook functions * input.cc (hook_function_list): New class. (hook_fcn_map_type): Delete typedef. (pre_input_event_hook_functions): Declare as hook_function_list. Rename from pre_input_event_hook_fcn_map. Change all uses. (input_event_hook_functions): Declare as hook_function_list. Rename from input_event_hook_fcn_map. Change all uses. (post_input_event_hook_functions): Declare as hook_function_list. Rename from post_input_event_hook_fcn_map. Change all uses. (dbstop_event_hook_functions): Declare as hook_function_list. Rename from dbstop_event_hook_fcn_map. Change all uses. (process_input_event_hook_functions): Delete. Change all uses to call hook_function_list::run instead. diff --git a/libinterp/interpfcn/input.cc b/libinterp/interpfcn/input.cc --- a/libinterp/interpfcn/input.cc +++ b/libinterp/interpfcn/input.cc @@ -291,39 +291,90 @@ error ("invalid hook function"); } -typedef std::map hook_fcn_map_type; +class +hook_function_list +{ +public: + + typedef std::map map_type; -static hook_fcn_map_type pre_input_event_hook_fcn_map; -static hook_fcn_map_type input_event_hook_fcn_map; -static hook_fcn_map_type post_input_event_hook_fcn_map; -static hook_fcn_map_type dbstop_event_hook_fcn_map; + typedef map_type::iterator iterator; + typedef map_type::const_iterator const_iterator; + + hook_function_list (void) : fcn_map () { } + + ~hook_function_list (void) { } -static void -process_input_event_hook_functions - (hook_fcn_map_type& hook_fcn_map, - const octave_value_list& initial_args = octave_value_list ()) -{ - hook_fcn_map_type::iterator p = hook_fcn_map.begin (); + hook_function_list (const hook_function_list& lst) + : fcn_map (lst.fcn_map) + { } + + hook_function_list& operator = (const hook_function_list& lst) + { + if (&lst != this) + fcn_map = lst.fcn_map; + + return *this; + } + + bool empty (void) const { return fcn_map.empty (); } + + void clear (void) { fcn_map.clear (); } + + void insert (const std::string& id, const hook_function& f) + { + fcn_map[id] = f; + } - while (p != hook_fcn_map.end ()) - { - std::string hook_fcn_id = p->first; - hook_function hook_fcn = p->second; + iterator find (const std::string& id) + { + return fcn_map.find (id); + } + + const_iterator find (const std::string& id) const + { + return fcn_map.find (id); + } - hook_fcn_map_type::iterator q = p++; + iterator end (void) { return fcn_map.end (); } + + const_iterator end (void) const { return fcn_map.end (); } + + void erase (iterator p) { fcn_map.erase (p); } + + void run (const octave_value_list& initial_args = octave_value_list ()) + { + iterator p = fcn_map.begin (); - if (hook_fcn.is_valid ()) - hook_fcn.eval (initial_args); - else - hook_fcn_map.erase (q); - } -} + while (p != fcn_map.end ()) + { + std::string hook_fcn_id = p->first; + hook_function hook_fcn = p->second; + + iterator q = p++; + + if (hook_fcn.is_valid ()) + hook_fcn.eval (initial_args); + else + fcn_map.erase (q); + } + } + +private: + + map_type fcn_map; +}; + +static hook_function_list pre_input_event_hook_functions; +static hook_function_list input_event_hook_functions; +static hook_function_list post_input_event_hook_functions; +static hook_function_list dbstop_event_hook_functions; // For octave_quit. void remove_input_event_hook_functions (void) { - input_event_hook_fcn_map.clear (); + input_event_hook_functions.clear (); } void @@ -406,7 +457,7 @@ // printing the prompt. if (interactive || forced_interactive) - process_input_event_hook_functions (pre_input_event_hook_fcn_map); + pre_input_event_hook_functions.run (); bool history_skip_auto_repeated_debugging_command = false; @@ -462,7 +513,7 @@ // list has been updated. if (interactive || forced_interactive) - process_input_event_hook_functions (post_input_event_hook_fcn_map); + post_input_event_hook_functions.run (); return retval; } @@ -700,8 +751,7 @@ octave_value location_info (location_info_map); - process_input_event_hook_functions (dbstop_event_hook_fcn_map, - location_info); + dbstop_event_hook_functions.run (location_info); std::string line_buf = get_file_line (nm, curr_debug_line); @@ -1310,7 +1360,7 @@ if (! error_state) { - pre_input_event_hook_fcn_map[hook_fcn.id ()] = hook_fcn; + pre_input_event_hook_functions.insert (hook_fcn.id (), hook_fcn); retval = hook_fcn.id (); } @@ -1345,11 +1395,11 @@ if (! error_state) { - hook_fcn_map_type::iterator p - = pre_input_event_hook_fcn_map.find (hook_fcn_id); + hook_function_list::iterator p + = pre_input_event_hook_functions.find (hook_fcn_id); - if (p != pre_input_event_hook_fcn_map.end ()) - pre_input_event_hook_fcn_map.erase (p); + if (p != pre_input_event_hook_functions.end ()) + pre_input_event_hook_functions.erase (p); else if (warn) warning ("remove_pre_input_event_hook: %s not found in list", hook_fcn_id.c_str ()); @@ -1366,9 +1416,9 @@ static int internal_input_event_hook_fcn (void) { - process_input_event_hook_functions (input_event_hook_fcn_map); + input_event_hook_functions.run (); - if (input_event_hook_fcn_map.empty ()) + if (input_event_hook_functions.empty ()) command_editor::remove_event_hook (internal_input_event_hook_fcn); return 0; @@ -1409,10 +1459,10 @@ if (! error_state) { - if (input_event_hook_fcn_map.empty ()) + if (input_event_hook_functions.empty ()) command_editor::add_event_hook (internal_input_event_hook_fcn); - input_event_hook_fcn_map[hook_fcn.id ()] = hook_fcn; + input_event_hook_functions.insert (hook_fcn.id (), hook_fcn); retval = hook_fcn.id (); } @@ -1447,16 +1497,16 @@ if (! error_state) { - hook_fcn_map_type::iterator p - = input_event_hook_fcn_map.find (hook_fcn_id); + hook_function_list::iterator p + = input_event_hook_functions.find (hook_fcn_id); - if (p != input_event_hook_fcn_map.end ()) - input_event_hook_fcn_map.erase (p); + if (p != input_event_hook_functions.end ()) + input_event_hook_functions.erase (p); else if (warn) warning ("remove_input_event_hook: %s not found in list", hook_fcn_id.c_str ()); - if (input_event_hook_fcn_map.empty ()) + if (input_event_hook_functions.empty ()) command_editor::remove_event_hook (internal_input_event_hook_fcn); } else @@ -1503,7 +1553,7 @@ if (! error_state) { - post_input_event_hook_fcn_map[hook_fcn.id ()] = hook_fcn; + post_input_event_hook_functions.insert (hook_fcn.id (), hook_fcn); retval = hook_fcn.id (); } @@ -1538,11 +1588,11 @@ if (! error_state) { - hook_fcn_map_type::iterator p - = post_input_event_hook_fcn_map.find (hook_fcn_id); + hook_function_list::iterator p + = post_input_event_hook_functions.find (hook_fcn_id); - if (p != post_input_event_hook_fcn_map.end ()) - post_input_event_hook_fcn_map.erase (p); + if (p != post_input_event_hook_functions.end ()) + post_input_event_hook_functions.erase (p); else if (warn) warning ("remove_post_input_event_hook: %s not found in list", hook_fcn_id.c_str ()); @@ -1600,7 +1650,7 @@ if (! error_state) { - dbstop_event_hook_fcn_map[hook_fcn.id ()] = hook_fcn; + dbstop_event_hook_functions.insert (hook_fcn.id (), hook_fcn); retval = hook_fcn.id (); } @@ -1635,11 +1685,11 @@ if (! error_state) { - hook_fcn_map_type::iterator p - = dbstop_event_hook_fcn_map.find (hook_fcn_id); + hook_function_list::iterator p + = dbstop_event_hook_functions.find (hook_fcn_id); - if (p != dbstop_event_hook_fcn_map.end ()) - dbstop_event_hook_fcn_map.erase (p); + if (p != dbstop_event_hook_functions.end ()) + dbstop_event_hook_functions.erase (p); else if (warn) warning ("remove_dbstop_event_hook: %s not found in list", hook_fcn_id.c_str ());