changeset 25344:4d3ce214da32

use std::function object for load-path add/remove hook functions * load-path.h, load-path.cc (load_path::add_hook, load_path::remove_hook): Now std::function objects. (load_path::load_path): Initialize them with lambda expressions. (load_path::get_add_hook, load_path::get_remove_hook): Return std::function objects. (load_path::set_add_hook, load_path::set_remove_hook): Accept std::function objects as parameters. (load_path::hook_fcn_ptr): Delete typedef. (execute_pkg_add_or_del): Now a load_path member function.
author John W. Eaton <jwe@octave.org>
date Thu, 03 May 2018 01:40:27 -0400
parents 4d7790d9793f
children ce6f7a5cd68e
files libinterp/corefcn/load-path.cc libinterp/corefcn/load-path.h
diffstat 2 files changed, 50 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/load-path.cc	Thu May 03 01:13:32 2018 -0400
+++ b/libinterp/corefcn/load-path.cc	Thu May 03 01:40:27 2018 -0400
@@ -148,23 +148,6 @@
   return retval;
 }
 
-static void
-execute_pkg_add_or_del (const std::string& dir,
-                        const std::string& script_file)
-{
-  if (! octave_interpreter_ready)
-    return;
-
-  octave::unwind_protect frame;
-
-  std::string file = octave::sys::file_ops::concat (dir, script_file);
-
-  octave::sys::file_stat fs (file);
-
-  if (fs.exists ())
-    octave::source_file (file, "base");
-}
-
 // True if a path is contained in a path list separated by path_sep_char
 
 static bool
@@ -205,6 +188,13 @@
   std::string load_path::sys_path;
   load_path::abs_dir_cache_type load_path::abs_dir_cache;
 
+  load_path::load_path (void)
+    : package_map (), top_level_package (), dir_info_list (), init_dirs (),
+      m_command_line_path (),
+      add_hook ([this] (const std::string& dir) { this->execute_pkg_add (dir); }),
+      remove_hook ([this] (const std::string& dir) { this->execute_pkg_del (dir); })
+  { }
+
   void
   load_path::initialize (bool set_initial_path)
   {
@@ -845,6 +835,22 @@
     execute_pkg_add_or_del (dir, "PKG_DEL");
   }
 
+  void load_path::execute_pkg_add_or_del (const std::string& dir,
+                                          const std::string& script_file)
+  {
+    if (! octave_interpreter_ready)
+      return;
+
+    octave::unwind_protect frame;
+
+    std::string file = octave::sys::file_ops::concat (dir, script_file);
+
+    octave::sys::file_stat fs (file);
+
+    if (fs.exists ())
+      octave::source_file (file, "base");
+  }
+
   // FIXME: maybe we should also maintain a map to speed up this method of access.
 
   load_path::const_dir_info_list_iterator
--- a/libinterp/corefcn/load-path.h	Thu May 03 01:13:32 2018 -0400
+++ b/libinterp/corefcn/load-path.h	Thu May 03 01:40:27 2018 -0400
@@ -26,6 +26,7 @@
 
 #include "octave-config.h"
 
+#include <functional>
 #include <iosfwd>
 #include <list>
 #include <map>
@@ -44,11 +45,7 @@
   {
   public:
 
-    load_path (void)
-      : package_map (), top_level_package (), dir_info_list (), init_dirs (),
-        m_command_line_path (), add_hook (load_path::execute_pkg_add),
-        remove_hook (load_path::execute_pkg_del)
-    { }
+    load_path (void);
 
     typedef void (*hook_fcn_ptr) (const std::string& dir);
 
@@ -169,14 +166,28 @@
 
     void display (std::ostream& os) const;
 
-    hook_fcn_ptr get_add_hook (void) { return add_hook; }
-    hook_fcn_ptr get_remove_hook (void) { return remove_hook; }
+    std::function<void (const std::string&)> get_add_hook (void)
+    {
+      return add_hook;
+    }
+
+    std::function<void (const std::string&)> get_remove_hook (void)
+    {
+      return remove_hook;
+    }
 
-    void set_add_hook (hook_fcn_ptr f) { add_hook = f; }
-    void set_remove_hook (hook_fcn_ptr f) { remove_hook = f; }
+    void set_add_hook (const std::function<void (const std::string&)>& f)
+    {
+      add_hook = f;
+    }
 
-    static void execute_pkg_add (const std::string& dir);
-    static void execute_pkg_del (const std::string& dir);
+    void set_remove_hook (const std::function<void (const std::string&)>& f)
+    {
+      remove_hook = f;
+    }
+
+    void execute_pkg_add (const std::string& dir);
+    void execute_pkg_del (const std::string& dir);
 
     void set_command_line_path (const std::string& p)
     {
@@ -510,9 +521,12 @@
 
     static abs_dir_cache_type abs_dir_cache;
 
-    hook_fcn_ptr add_hook;
+    std::function<void (const std::string&)> add_hook;
 
-    hook_fcn_ptr remove_hook;
+    std::function<void (const std::string&)> remove_hook;
+
+    void execute_pkg_add_or_del (const std::string& dir,
+                                 const std::string& script_file);
 
     const_dir_info_list_iterator find_dir_info (const std::string& dir) const;
     dir_info_list_iterator find_dir_info (const std::string& dir);