changeset 8009:d936b21b3a6b

file_ops: use singleton class for static data members
author John W. Eaton <jwe@octave.org>
date Tue, 05 Aug 2008 12:14:16 -0400
parents 4d13a7a2f6ab
children 0e9b6af36559
files liboctave/ChangeLog liboctave/file-ops.cc liboctave/file-ops.h
diffstat 3 files changed, 53 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Tue Aug 05 12:04:10 2008 -0400
+++ b/liboctave/ChangeLog	Tue Aug 05 12:14:16 2008 -0400
@@ -1,9 +1,13 @@
 2008-08-05  John W. Eaton  <jwe@octave.org>
 
+	* file-ops.h, file-ops.cc (file_ops::static_members):
+	New singleton class for static members of file_ops.
+
+	* pathsearch.h, pathsearch.cc (class dir_path::static_members):
+	New singleton class for static members of dir_path.
+
 	* 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>
 
--- a/liboctave/file-ops.cc	Tue Aug 05 12:04:10 2008 -0400
+++ b/liboctave/file-ops.cc	Tue Aug 05 12:14:16 2008 -0400
@@ -51,7 +51,9 @@
 #include "statdefs.h"
 #include "str-vec.h"
 
-file_ops::file_ops (void)
+file_ops::static_members *file_ops::static_members::instance = 0;
+
+file_ops::static_members::static_members (void)
   :
 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
   xdir_sep_char ('\\'),
@@ -67,20 +69,18 @@
 #endif
 { }
 
-file_ops *file_ops::instance = 0;
-
 bool
-file_ops::instance_ok (void)
+file_ops::static_members::instance_ok (void)
 {
   bool retval = true;
 
   if (! instance)
-    instance = new file_ops ();
+    instance = new static_members ();
 
   if (! instance)
     {
       (*current_liboctave_error_handler)
-	("unable to create file_ops object!");
+	("unable to create file_ops::static_members object!");
 
       retval = false;
     }
--- a/liboctave/file-ops.h	Tue Aug 05 12:04:10 2008 -0400
+++ b/liboctave/file-ops.h	Tue Aug 05 12:14:16 2008 -0400
@@ -35,10 +35,6 @@
 OCTAVE_API
 file_ops
 {
-protected:
-
-  file_ops (void);
-
 public:
 
   static int mkdir (const std::string&, mode_t);
@@ -92,44 +88,71 @@
 
   static bool is_dir_sep (char c)
   {
-    return instance_ok () ? instance->do_is_dir_sep (c) : false;
+    std::string tmp = dir_sep_chars ();
+    return tmp.find (c) != NPOS;
   }
 
   static std::string concat (const std::string&, const std::string&);
 
   static char dir_sep_char (void)
   {
-    return instance_ok () ? instance->xdir_sep_char : 0;
+    return static_members::dir_sep_char ();
   }
 
   static std::string dir_sep_str (void)
   {
-    return instance_ok () ? instance->xdir_sep_str : std::string ();
+    return static_members::dir_sep_str ();
   }
 
   static std::string dir_sep_chars (void)
   {
-    return instance_ok () ? instance->xdir_sep_chars : std::string ();
+    return static_members::dir_sep_chars ();
   }
 
 private:
 
-  // No copying!
+  // 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);
 
-  file_ops (const file_ops&);
+    static char dir_sep_char (void)
+    {
+      return instance_ok () ? instance->xdir_sep_char : 0;
+    }
 
-  file_ops& operator = (const file_ops&);
+    static std::string dir_sep_str (void)
+    {
+      return instance_ok () ? instance->xdir_sep_str : std::string ();
+    }
 
-  // The real thing.
-  static file_ops *instance;
+    static std::string dir_sep_chars (void)
+    {
+      return instance_ok () ? instance->xdir_sep_chars : std::string ();
+    }
 
-  static bool instance_ok (void);
+  private:
+
+    // The real thing.
+    static static_members *instance;
 
-  char xdir_sep_char;
-  std::string xdir_sep_str;
-  std::string xdir_sep_chars;
+    // No copying!
+
+    static_members (const static_members&);
+
+    static_members& operator = (const static_members&);
 
-  bool do_is_dir_sep (char c) { return xdir_sep_chars.find (c) != NPOS; }
+    static bool instance_ok (void);
+
+    char xdir_sep_char;
+    std::string xdir_sep_str;
+    std::string xdir_sep_chars;
+  };
 };
 
 #endif