Mercurial > hg > octave-nkf
diff liboctave/file-ops.h @ 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 | a2ab20ba78f7 |
children | 0ef13e15319b |
line wrap: on
line diff
--- 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