changeset 8008:4d13a7a2f6ab

dir_path: use singleton class for static data members
author John W. Eaton <jwe@octave.org>
date Tue, 05 Aug 2008 12:04:10 -0400
parents a2ab20ba78f7
children d936b21b3a6b
files liboctave/ChangeLog liboctave/pathsearch.cc liboctave/pathsearch.h src/ChangeLog src/defaults.cc src/dirfns.cc src/load-path.cc src/load-path.h
diffstat 8 files changed, 108 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Mon Aug 04 23:44:50 2008 -0400
+++ b/liboctave/ChangeLog	Tue Aug 05 12:04:10 2008 -0400
@@ -1,3 +1,10 @@
+2008-08-05  John W. Eaton  <jwe@octave.org>
+
+	* pathsearch.cc (dir_path::init): Move octave_kpathsea_initialized
+	here from file scope.
+	* pathsearch.h, pathsearch.cc (class dir_path::static_members):
+	New singleton class for static members of dir_path.
+
 2008-08-04  John W. Eaton  <jwe@octave.org>
 
 	* oct-env.cc (octave_env::do_set_program_name,
--- a/liboctave/pathsearch.cc	Mon Aug 04 23:44:50 2008 -0400
+++ b/liboctave/pathsearch.cc	Tue Aug 05 12:04:10 2008 -0400
@@ -37,11 +37,29 @@
 
 #include "kpse.cc"
 
-char dir_path::path_sep_char = SEPCHAR;
+dir_path::static_members *dir_path::static_members::instance = 0;
+
+dir_path::static_members::static_members (void)
+  : xpath_sep_char (SEPCHAR), xpath_sep_str (SEPCHAR_STR) { }
+
+bool
+dir_path::static_members::instance_ok (void)
+{
+  bool retval = true;
 
-std::string dir_path::path_sep_str (SEPCHAR_STR);
+  if (! instance)
+    instance = new static_members ();
 
-static bool octave_kpathsea_initialized = false;
+  if (! instance)
+    {
+      (*current_liboctave_error_handler)
+	("unable to create dir_path::static_members object!");
+
+      retval = false;
+    }
+
+  return retval;
+}
 
 string_vector
 dir_path::elements (void)
@@ -123,6 +141,8 @@
 void
 dir_path::init (void)
 {
+  static bool octave_kpathsea_initialized = false;
+
   if (! octave_kpathsea_initialized)
     {
       std::string val = octave_env::getenv ("KPATHSEA_DEBUG");
--- a/liboctave/pathsearch.h	Mon Aug 04 23:44:50 2008 -0400
+++ b/liboctave/pathsearch.h	Tue Aug 05 12:04:10 2008 -0400
@@ -83,10 +83,58 @@
       init ();
     }
 
-  static bool is_path_sep (char c) { return c == path_sep_char; }
+private:
+
+  // Use a singleton class for these data members instead of just
+  // making them static members of the dir_path class so that we can
+  // ensure proper initialization.
+
+  class static_members
+  {
+  public:
+
+    static_members (void);
+
+    static char path_sep_char (void)
+    {
+      return instance_ok () ? instance->xpath_sep_char : 0;
+    }
+
+    static std::string path_sep_str (void)
+    {
+      return instance_ok () ? instance->xpath_sep_str : std::string ();
+    }
+
+  private:
+
+    static static_members *instance;
 
-  static char path_sep_char;
-  static std::string path_sep_str;
+    static bool instance_ok (void);
+
+    // No copying!
+
+    static_members (const static_members&);
+
+    static_members& operator = (const static_members&);
+
+    char xpath_sep_char;
+
+    std::string xpath_sep_str;
+  };
+
+public:
+
+  static char path_sep_char (void)
+  {
+    return static_members::path_sep_char ();
+  }
+
+  static std::string path_sep_str (void)
+  {
+    return static_members::path_sep_str ();
+  }
+
+  static bool is_path_sep (char c) { return c == path_sep_char (); }
 
 private:
 
--- a/src/ChangeLog	Mon Aug 04 23:44:50 2008 -0400
+++ b/src/ChangeLog	Tue Aug 05 12:04:10 2008 -0400
@@ -1,3 +1,12 @@
+2008-08-05  John W. Eaton  <jwe@octave.org>
+
+	* dirfns.cc (Fpathsep): Fix usage of dir_path::path_sep_str.
+	* defaults.cc (set_exec_path, set_image_path): Likewise.
+	* load-path.h (load_path::set_command_line_path): Likewise.
+	* load-path.cc (maybe_add_path_elts, load_path::do_initialize,
+	load_path::do_path, genpath, Fpath): Likewise.
+	(split_path): Fix usage of dir_path::path_sep_char.
+
 2008-08-04  John W. Eaton  <jwe@octave.org>
 
 	* symtab.cc (symbol_table::fcn_info::fcn_info_rep::find_autoload):
--- a/src/defaults.cc	Mon Aug 04 23:44:50 2008 -0400
+++ b/src/defaults.cc	Tue Aug 05 12:04:10 2008 -0400
@@ -233,10 +233,10 @@
 void
 set_exec_path (const std::string& path)
 {
-  VEXEC_PATH = Vlocal_ver_arch_lib_dir + dir_path::path_sep_str
-    + Vlocal_api_arch_lib_dir + dir_path::path_sep_str
-    + Vlocal_arch_lib_dir + dir_path::path_sep_str
-    + Varch_lib_dir + dir_path::path_sep_str
+  VEXEC_PATH = Vlocal_ver_arch_lib_dir + dir_path::path_sep_str ()
+    + Vlocal_api_arch_lib_dir + dir_path::path_sep_str ()
+    + Vlocal_arch_lib_dir + dir_path::path_sep_str ()
+    + Varch_lib_dir + dir_path::path_sep_str ()
     + Vbin_dir;
   
   // This is static so that even if set_exec_path is called more than
@@ -245,7 +245,7 @@
   static std::string shell_path = octave_env::getenv ("PATH");
 
   if (! shell_path.empty ())
-    VEXEC_PATH += dir_path::path_sep_str + shell_path;
+    VEXEC_PATH += dir_path::path_sep_str () + shell_path;
 
   std::string tpath = path;
 
@@ -253,7 +253,7 @@
     tpath = octave_env::getenv ("OCTAVE_EXEC_PATH");
 
   if (! tpath.empty ())
-    VEXEC_PATH = tpath + dir_path::path_sep_str + VEXEC_PATH;
+    VEXEC_PATH = tpath + dir_path::path_sep_str () + VEXEC_PATH;
 
   octave_env::putenv ("PATH", VEXEC_PATH);
 }
@@ -269,12 +269,12 @@
     tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH");
 
   if (! tpath.empty ())
-    VIMAGE_PATH += dir_path::path_sep_str + tpath;
+    VIMAGE_PATH += dir_path::path_sep_str () + tpath;
 
   tpath = genpath (Vimage_dir, "");
 
   if (! tpath.empty ())
-    VIMAGE_PATH += dir_path::path_sep_str + tpath;
+    VIMAGE_PATH += dir_path::path_sep_str () + tpath;
 }
 
 static void
--- a/src/dirfns.cc	Mon Aug 04 23:44:50 2008 -0400
+++ b/src/dirfns.cc	Tue Aug 05 12:04:10 2008 -0400
@@ -662,7 +662,7 @@
   octave_value retval;
 
   if (args.length () == 0)
-    retval = dir_path::path_sep_str;
+    retval = dir_path::path_sep_str ();
   else
     print_usage ();
 
--- a/src/load-path.cc	Mon Aug 04 23:44:50 2008 -0400
+++ b/src/load-path.cc	Tue Aug 05 12:04:10 2008 -0400
@@ -419,7 +419,7 @@
       if (path.empty ())
 	path = tpath;
       else
-	path += dir_path::path_sep_str + tpath;
+	path += dir_path::path_sep_str () + tpath;
     }
 }
 
@@ -448,10 +448,10 @@
   std::string xpath = ".";
 
   if (! tpath.empty ())
-    xpath += dir_path::path_sep_str + tpath;
+    xpath += dir_path::path_sep_str () + tpath;
 
   if (! sys_path.empty ())
-    xpath += dir_path::path_sep_str + sys_path;
+    xpath += dir_path::path_sep_str () + sys_path;
 
   do_set (xpath, false);
 }
@@ -473,7 +473,7 @@
   std::list<std::string> retval;
 
   size_t beg = 0;
-  size_t end = p.find (dir_path::path_sep_char);
+  size_t end = p.find (dir_path::path_sep_char ());
 
   size_t len = p.length ();
 
@@ -489,7 +489,7 @@
       if (beg == len)
 	break;
 
-      end = p.find (dir_path::path_sep_char, beg);
+      end = p.find (dir_path::path_sep_char (), beg);
     }
 
   std::string elt = p.substr (beg);
@@ -1246,7 +1246,7 @@
     xpath = xdirs[0];
 
   for (octave_idx_type i = 1; i < len; i++)
-    xpath += dir_path::path_sep_str + xdirs[i];
+    xpath += dir_path::path_sep_str () + xdirs[i];
 
   return xpath;
 }
@@ -1610,7 +1610,7 @@
 		  file_stat fs (nm);
 
 		  if (fs && fs.is_dir ())
-		    retval += dir_path::path_sep_str + genpath (nm);
+		    retval += dir_path::path_sep_str () + genpath (nm);
 		}
 	    }
 	}
@@ -1754,7 +1754,7 @@
 	  std::string path = argv[1];
 
 	  for (int i = 2; i < argc; i++)
-	    path += dir_path::path_sep_str + argv[i];
+	    path += dir_path::path_sep_str () + argv[i];
 
 	  load_path::set (path, true);
 	}
--- a/src/load-path.h	Mon Aug 04 23:44:50 2008 -0400
+++ b/src/load-path.h	Tue Aug 05 12:04:10 2008 -0400
@@ -209,7 +209,7 @@
     if (command_line_path.empty ())
       command_line_path = p;
     else
-      command_line_path += dir_path::path_sep_str + p;
+      command_line_path += dir_path::path_sep_str () + p;
   }
 
   static std::string system_path (void)