changeset 27183:2d9decd77e58

revamp float_format object * pr-flt-fmt.h: Use m_ prefix for data members. Make data members privatae and provide accessor functions. Change all uses.
author John W. Eaton <jwe@octave.org>
date Thu, 13 Jun 2019 17:40:36 -0500
parents 0beeb6817376
children 04889e45f54e
files libgui/src/variable-editor-model.cc libinterp/corefcn/pr-flt-fmt.h libinterp/corefcn/pr-output.cc
diffstat 3 files changed, 105 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/variable-editor-model.cc	Thu Jun 13 13:49:53 2019 -0500
+++ b/libgui/src/variable-editor-model.cc	Thu Jun 13 17:40:36 2019 -0500
@@ -129,8 +129,8 @@
     float_format r_fmt = m_display_fmt.real_format ();
     float_format i_fmt = m_display_fmt.imag_format ();
 
-    int rw = r_fmt.fw;
-    int iw = i_fmt.fw;
+    int rw = r_fmt.width ();
+    int iw = i_fmt.width ();
 
     if (rw > 0)
       {
--- a/libinterp/corefcn/pr-flt-fmt.h	Thu Jun 13 13:49:53 2019 -0500
+++ b/libinterp/corefcn/pr-flt-fmt.h	Thu Jun 13 17:40:36 2019 -0500
@@ -47,10 +47,10 @@
 public:
 
   float_format (int w = 0, int p = output_precision (), int f = 0)
-    : fw (w), ex (0), prec (p), fmt (f), up (0), sp (0) { }
+    : m_fw (w), m_ex (0), m_prec (p), m_fmt (f), m_up (0), m_sp (0) { }
 
   float_format (int w, int e, int p, int f)
-    : fw (w), ex (e), prec (p), fmt (f), up (0), sp (0) { }
+    : m_fw (w), m_ex (e), m_prec (p), m_fmt (f), m_up (0), m_sp (0) { }
 
   float_format (const float_format&) = default;
 
@@ -60,53 +60,114 @@
 
   float_format& scientific (void)
   {
-    fmt = std::ios::scientific;
+    m_fmt = std::ios::scientific;
     return *this;
   }
 
   float_format& fixed (void)
   {
-    fmt = std::ios::fixed;
+    m_fmt = std::ios::fixed;
     return *this;
   }
 
   float_format& general (void)
   {
-    fmt = 0;
+    m_fmt = 0;
     return *this;
   }
 
   float_format& uppercase (void)
   {
-    up = std::ios::uppercase;
+    m_up = std::ios::uppercase;
     return *this;
   }
 
   float_format& lowercase (void)
   {
-    up = 0;
+    m_up = 0;
     return *this;
   }
 
   float_format& precision (int p)
   {
-    prec = p;
+    m_prec = p;
     return *this;
   }
 
   float_format& width (int w)
   {
-    fw = w;
+    m_fw = w;
+    return *this;
+  }
+
+  float_format& exponent_width (int w)
+  {
+    m_ex = w;
     return *this;
   }
 
   float_format& trailing_zeros (bool tz = true)
 
   {
-    sp = (tz ? std::ios::showpoint : 0);
+    m_sp = (tz ? std::ios::showpoint : 0);
     return *this;
   }
 
+  std::ios::fmtflags format_flags (void) const
+  {
+    return static_cast<std::ios::fmtflags> (m_fmt | m_up | m_sp);
+  }
+
+  int format (void) const
+  {
+    return m_fmt;
+  }
+
+  bool is_scientific (void) const
+  {
+    return m_fmt == std::ios::scientific;
+  }
+
+  bool is_fixed (void) const
+  {
+    return m_fmt == std::ios::fixed;
+  }
+
+  bool is_general (void) const
+  {
+    return m_fmt == 0;
+  }
+
+  bool is_uppercase (void) const
+  {
+    return m_up == std::ios::uppercase;
+  }
+
+  bool is_lowercase (void) const
+  {
+    return m_up == 0;
+  }
+
+  int precision (void) const
+  {
+    return m_prec;
+  }
+
+  int width (void) const
+  {
+    return m_fw;
+  }
+
+  int exponent_width (void) const
+  {
+    return m_ex;
+  }
+
+  bool show_trailing_zeros (void) const
+  {
+    return m_sp == std::ios::showpoint;
+  }
+
   template <typename T>
   friend std::ostream&
   operator << (std::ostream& os, const pr_engineering_float<T>& pef);
@@ -119,23 +180,25 @@
   friend std::ostream&
   operator << (std::ostream& os, const pr_rational_float<T>& prf);
 
+private:
+
   // Field width.  Zero means as wide as necessary.
-  int fw;
+  int m_fw;
 
   // Exponent Field width.  Zero means as wide as necessary.
-  int ex;
+  int m_ex;
 
   // Precision.
-  int prec;
+  int m_prec;
 
   // Format.
-  int fmt;
+  int m_fmt;
 
   // E or e.
-  int up;
+  int m_up;
 
   // Show trailing zeros.
-  int sp;
+  int m_sp;
 };
 
 class
@@ -169,8 +232,8 @@
 
   void set_precision (int prec)
   {
-    m_real_fmt.prec = prec;
-    m_imag_fmt.prec = prec;
+    m_real_fmt.precision (prec);
+    m_imag_fmt.precision (prec);
   }
 
 private:
--- a/libinterp/corefcn/pr-output.cc	Thu Jun 13 13:49:53 2019 -0500
+++ b/libinterp/corefcn/pr-output.cc	Thu Jun 13 17:40:36 2019 -0500
@@ -188,14 +188,13 @@
 
   float_format real_fmt = pef.m_ff;
 
-  if (real_fmt.fw >= 0)
-    os << std::setw (real_fmt.fw - real_fmt.ex);
-
-  if (real_fmt.prec >= 0)
-    os << std::setprecision (real_fmt.prec);
-
-  os.flags (static_cast<std::ios::fmtflags>
-            (real_fmt.fmt | real_fmt.up | real_fmt.sp));
+  if (real_fmt.width () >= 0)
+    os << std::setw (real_fmt.width () - real_fmt.exponent_width ());
+
+  if (real_fmt.precision () >= 0)
+    os << std::setprecision (real_fmt.precision ());
+
+  os.flags (real_fmt.format_flags ());
 
   os << pef.mantissa ();
 
@@ -208,7 +207,8 @@
   else
     os << std::setw (0) << "e+";
 
-  os << std::setw (real_fmt.ex - 2) << std::setfill ('0') << ex;
+  os << std::setw (real_fmt.exponent_width () - 2)
+     << std::setfill ('0') << ex;
 
   return os;
 }
@@ -221,14 +221,13 @@
 
   float_format real_fmt = pff.m_ff;
 
-  if (real_fmt.fw >= 0)
-    os << std::setw (real_fmt.fw);
-
-  if (real_fmt.prec >= 0)
-    os << std::setprecision (real_fmt.prec);
-
-  os.flags (static_cast<std::ios::fmtflags>
-            (real_fmt.fmt | real_fmt.up | real_fmt.sp));
+  if (real_fmt.width () >= 0)
+    os << std::setw (real_fmt.width ());
+
+  if (real_fmt.precision () >= 0)
+    os << std::setprecision (real_fmt.precision ());
+
+  os.flags (real_fmt.format_flags ());
 
   os << pff.m_val;
 
@@ -243,14 +242,13 @@
 
   float_format real_fmt = prf.m_ff;
 
-  int fw = (rat_string_len > 0 ? rat_string_len : real_fmt.fw);
+  int fw = (rat_string_len > 0 ? rat_string_len : real_fmt.width ());
   std::string s = rational_approx (prf.m_val, fw);
 
   if (fw >= 0)
     os << std::setw (fw);
 
-  os.flags (static_cast<std::ios::fmtflags>
-            (real_fmt.fmt | real_fmt.up | real_fmt.sp));
+  os.flags (real_fmt.format_flags ());
 
   if (fw > 0 && s.length () > static_cast<unsigned int> (fw))
     os << '*';
@@ -1300,7 +1298,7 @@
   //   {bit,hex}_format == 1: print big-endian
   //   {bit,hex}_format == 2: print native
 
-  int fw = fmt.fw;
+  int fw = fmt.width ();
 
   if (hex_format)
     {
@@ -1755,8 +1753,8 @@
 static inline int
 get_column_width (const float_display_format& fmt)
 {
-  int r_fw = fmt.real_format().fw;
-  int i_fw = fmt.imag_format().fw;
+  int r_fw = fmt.real_format().width ();
+  int i_fw = fmt.imag_format().width ();
 
   int retval = r_fw + i_fw + 2;
 
@@ -2807,7 +2805,7 @@
         {
           float_format r_fmt = fmt.real_format ();
 
-          pr_int (os, val, r_fmt.fw);
+          pr_int (os, val, r_fmt.width ());
         }
     }
 }