Mercurial > octave
changeset 23745:a732be902061
don't use singleton pattern for mach_info
* mach-info.h, mach-info.cc: Don't use singleton. Convert mach_info
class to namespace.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 07 Jul 2017 11:35:55 -0400 |
parents | ae74608b6a5d |
children | 7f176909ab86 |
files | liboctave/system/mach-info.cc liboctave/system/mach-info.h |
diffstat | 2 files changed, 131 insertions(+), 188 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/system/mach-info.cc Fri Jul 07 08:17:32 2017 -0400 +++ b/liboctave/system/mach-info.cc Fri Jul 07 11:35:55 2017 -0400 @@ -27,7 +27,6 @@ #include "f77-fcn.h" #include "lo-error.h" #include "mach-info.h" -#include "singleton-cleanup.h" extern "C" { @@ -36,187 +35,159 @@ namespace octave { - mach_info *mach_info::instance = nullptr; - - union equiv + namespace mach_info { - double d; - int i[2]; - }; + union equiv + { + double d; + int i[2]; + }; - struct - float_params - { - mach_info::float_format fp_fmt; - equiv fp_par[4]; - }; + struct float_params + { + float_format fp_fmt; + equiv fp_par[4]; + }; #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \ - do \ - { \ - fp.fp_fmt = (fmt); \ - fp.fp_par[0].i[0] = (sm1); fp.fp_par[0].i[1] = (sm2); \ - fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \ - fp.fp_par[2].i[0] = (rt1); fp.fp_par[2].i[1] = (rt2); \ - fp.fp_par[3].i[0] = (dv1); fp.fp_par[3].i[1] = (dv2); \ - } \ - while (0) + do \ + { \ + fp.fp_fmt = (fmt); \ + fp.fp_par[0].i[0] = (sm1); fp.fp_par[0].i[1] = (sm2); \ + fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \ + fp.fp_par[2].i[0] = (rt1); fp.fp_par[2].i[1] = (rt2); \ + fp.fp_par[3].i[0] = (dv1); fp.fp_par[3].i[1] = (dv2); \ + } \ + while (0) - static int - equiv_compare (const equiv *std, const equiv *v, int len) - { - int i; - for (i = 0; i < len; i++) - if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1]) - return 0; - return 1; - } + static int equiv_compare (const equiv *std, const equiv *v, int len) + { + int i; + for (i = 0; i < len; i++) + if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1]) + return 0; + return 1; + } - static mach_info::float_format - get_float_format (void) - { - mach_info::float_format retval = mach_info::flt_fmt_unknown; + static float_format get_float_format (void) + { + float_format retval = flt_fmt_unknown; - float_params fp[5]; + float_params fp[5]; - INIT_FLT_PAR (fp[0], mach_info::flt_fmt_ieee_big_endian, - 1048576, 0, - 2146435071, -1, - 1017118720, 0, - 1018167296, 0); - - INIT_FLT_PAR (fp[1], mach_info::flt_fmt_ieee_little_endian, - 0, 1048576, - -1, 2146435071, - 0, 1017118720, - 0, 1018167296); + INIT_FLT_PAR (fp[0], flt_fmt_ieee_big_endian, + 1048576, 0, + 2146435071, -1, + 1017118720, 0, + 1018167296, 0); - INIT_FLT_PAR (fp[4], mach_info::flt_fmt_unknown, - 0, 0, - 0, 0, - 0, 0, - 0, 0); + INIT_FLT_PAR (fp[1], flt_fmt_ieee_little_endian, + 0, 1048576, + -1, 2146435071, + 0, 1017118720, + 0, 1018167296); - equiv mach_fp_par[4]; + INIT_FLT_PAR (fp[4], flt_fmt_unknown, + 0, 0, + 0, 0, + 0, 0, + 0, 0); - mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (1); - mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (2); - mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (3); - mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (4); + equiv mach_fp_par[4]; - int i = 0; - do - { - if (equiv_compare (fp[i].fp_par, mach_fp_par, 4)) - { - retval = fp[i].fp_fmt; - break; - } - } - while (fp[++i].fp_fmt != mach_info::flt_fmt_unknown); - - return retval; - } + mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (1); + mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (2); + mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (3); + mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (4); - static bool - ten_little_endians (void) - { - // Are we little or big endian? From Harbison & Steele. + int i = 0; + do + { + if (equiv_compare (fp[i].fp_par, mach_fp_par, 4)) + { + retval = fp[i].fp_fmt; + break; + } + } + while (fp[++i].fp_fmt != flt_fmt_unknown); - union + return retval; + } + + static bool is_big_endian (void) { - long l; - char c[sizeof (long)]; - } u; - - u.l = 1; - - return (u.c[sizeof (long) - 1] == 1); - } + // Are we little or big endian? From Harbison & Steele. - mach_info::mach_info (void) - : native_float_fmt (get_float_format ()), - big_chief (ten_little_endians ()) { } + union + { + long l; + char c[sizeof (long)]; + } u; + + u.l = 1; - bool - mach_info::instance_ok (void) - { - bool retval = true; + return (u.c[sizeof (long) - 1] == 1); + } - if (! instance) - { - instance = new mach_info (); + float_format native_float_format (void) + { + static float_format fmt = get_float_format (); - if (instance) - singleton_cleanup_list::add (cleanup_instance); - } + return fmt; + } - if (! instance) - (*current_liboctave_error_handler) - ("unable to create command history object!"); + bool words_big_endian (void) + { + static bool big_endian = is_big_endian (); - return retval; - } + return big_endian; + } - mach_info::float_format - mach_info::native_float_format (void) - { - return (instance_ok ()) - ? instance->native_float_fmt : mach_info::flt_fmt_unknown; - } + bool words_little_endian (void) + { + static bool little_endian = ! is_big_endian (); - bool - mach_info::words_big_endian (void) - { - return (instance_ok ()) - ? instance->big_chief : false; - } + return little_endian; + } + + float_format string_to_float_format (const std::string& s) + { + float_format retval = flt_fmt_unknown; - bool - mach_info::words_little_endian (void) - { - return (instance_ok ()) - ? (! instance->big_chief) : false; - } + if (s == "native" || s == "n") + retval = native_float_format (); + else if (s == "ieee-be" || s == "b") + retval = flt_fmt_ieee_big_endian; + else if (s == "ieee-le" || s == "l") + retval = flt_fmt_ieee_little_endian; + else if (s == "unknown") + retval = flt_fmt_unknown; + else + (*current_liboctave_error_handler) + ("invalid architecture type specified"); - mach_info::float_format - mach_info::string_to_float_format (const std::string& s) - { - mach_info::float_format retval = mach_info::flt_fmt_unknown; - - if (s == "native" || s == "n") - retval = mach_info::native_float_format (); - else if (s == "ieee-be" || s == "b") - retval = mach_info::flt_fmt_ieee_big_endian; - else if (s == "ieee-le" || s == "l") - retval = mach_info::flt_fmt_ieee_little_endian; - else if (s == "unknown") - retval = mach_info::flt_fmt_unknown; - else - (*current_liboctave_error_handler) ("invalid architecture type specified"); + return retval; + } - return retval; - } + std::string float_format_as_string (float_format flt_fmt) + { + std::string retval = "unknown"; - std::string - mach_info::float_format_as_string (float_format flt_fmt) - { - std::string retval = "unknown"; + switch (flt_fmt) + { + case flt_fmt_ieee_big_endian: + retval = "ieee-be"; + break; - switch (flt_fmt) - { - case flt_fmt_ieee_big_endian: - retval = "ieee-be"; - break; + case flt_fmt_ieee_little_endian: + retval = "ieee-le"; + break; - case flt_fmt_ieee_little_endian: - retval = "ieee-le"; - break; + default: + break; + } - default: - break; - } - - return retval; + return retval; + } } }
--- a/liboctave/system/mach-info.h Fri Jul 07 08:17:32 2017 -0400 +++ b/liboctave/system/mach-info.h Fri Jul 07 11:35:55 2017 -0400 @@ -29,22 +29,8 @@ namespace octave { - class - OCTAVE_API - mach_info + namespace mach_info { - protected: - - mach_info (void); - - public: - - // No copying! - - mach_info (const mach_info&) = delete; - - mach_info& operator = (const mach_info&) = delete; - enum float_format { flt_fmt_unknown, @@ -52,30 +38,16 @@ flt_fmt_ieee_big_endian, }; - static bool instance_ok (void); - - static float_format native_float_format (void); - - static bool words_big_endian (void); + float_format native_float_format (void); - static bool words_little_endian (void); - - static float_format string_to_float_format (const std::string&); - - static std::string float_format_as_string (float_format); + bool words_big_endian (void); - private: - - static mach_info *instance; - - static void cleanup_instance (void) { delete instance; instance = 0; } + bool words_little_endian (void); - // The floating point format for the current machine. - mutable float_format native_float_fmt; + float_format string_to_float_format (const std::string&); - // TRUE if the byte order on this system is big endian. - mutable bool big_chief; - }; + std::string float_format_as_string (float_format); + } } #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)