# HG changeset patch # User John W. Eaton # Date 1217952856 14400 # Node ID d936b21b3a6b1c1895e034fab174053dbc9c5faf # Parent 4d13a7a2f6abb53bc3768d3ce2d2747a29692597 file_ops: use singleton class for static data members diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,9 +1,13 @@ 2008-08-05 John W. Eaton + * 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 diff --git a/liboctave/file-ops.cc b/liboctave/file-ops.cc --- a/liboctave/file-ops.cc +++ b/liboctave/file-ops.cc @@ -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; } diff --git a/liboctave/file-ops.h b/liboctave/file-ops.h --- a/liboctave/file-ops.h +++ b/liboctave/file-ops.h @@ -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