lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 8726945 2/2: Improve const correctness


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 8726945 2/2: Improve const correctness
Date: Fri, 25 May 2018 17:28:41 -0400 (EDT)

branch: master
commit 87269451ee859377286aa6b7ab1a9087dcca2a9b
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Improve const correctness
    
    Adding const private accessor dc() and using it to call const member
    functions of class wxDC does seem somewhat artificial, but makes it
    easier to discern which member functions of the present class actually
    are logically const.
    
    Resisted the temptation to use dc() in ctor-initializers, e.g.:
    -    ,char_height_      (dc_.GetCharHeight())
    +    ,char_height_      (dc().GetCharHeight())
    because it would serve no useful purpose, yet would make the code less
    obviously correct.
---
 wx_table_generator.cpp | 27 ++++++++++++++++-----------
 wx_table_generator.hpp |  7 ++++---
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/wx_table_generator.cpp b/wx_table_generator.cpp
index 5bb02bb..5248df3 100644
--- a/wx_table_generator.cpp
+++ b/wx_table_generator.cpp
@@ -178,6 +178,11 @@ wx_table_generator::wx_table_generator(wx_table_generator 
const&) = default;
 
 wx_table_generator::~wx_table_generator() = default;
 
+wxDC const& wx_table_generator::dc() const
+{
+    return dc_;
+}
+
 int wx_table_generator::column_margin() const
 {
     return column_margin_;
@@ -224,7 +229,7 @@ void wx_table_generator::enroll_column(column_parameters 
const& z)
             }
 
         wxCoord w, h, lh;
-        dc_.GetMultiLineTextExtent(z.header, &w, &h, &lh, &dc_.GetFont());
+        dc().GetMultiLineTextExtent(z.header, &w, &h, &lh, &dc().GetFont());
         LMI_ASSERT(0 != lh);
         LMI_ASSERT(0 == h % lh);
 // Temporarily assert that this does the same as the code it replaced:
@@ -240,7 +245,7 @@ LMI_ASSERT(std::size_t(h / lh) == 1u + 
count_newlines(z.header));
             case oe_inelastic:
                 {
                 // Greater of header width and 'widest_text' width.
-                width = std::max(w, dc_.GetTextExtent(z.widest_text).x);
+                width = std::max(w, dc().GetTextExtent(z.widest_text).x);
                 // PDF !! Reconsider whether margin should be added here,
                 // because compute_column_widths() may need to remove it.
                 width += 2 * column_margin();
@@ -264,7 +269,7 @@ LMI_ASSERT(std::size_t(h / lh) == 1u + 
count_newlines(z.header));
 
 wxFont wx_table_generator::get_header_font() const
 {
-    return dc_.GetFont().Bold();
+    return dc().GetFont().Bold();
 }
 
 // Horizontal and vertical separators are considered to be drawn in
@@ -282,7 +287,7 @@ void wx_table_generator::do_output_vert_separator(int x, 
int y1, int y2)
     dc_.DrawLine(x, y1, x, y2);
 }
 
-int wx_table_generator::do_get_cell_x(std::size_t column)
+int wx_table_generator::do_get_cell_x(std::size_t column) const
 {
     int x = left_margin_;
     for(std::size_t col = 0; col < column; ++col)
@@ -302,7 +307,7 @@ int wx_table_generator::row_height() const
 
 /// Return the rectangle containing the cell area.
 
-wxRect wx_table_generator::cell_rect(std::size_t column, int y)
+wxRect wx_table_generator::cell_rect(std::size_t column, int y) const
 {
     return wxRect
         (do_get_cell_x(column)
@@ -317,9 +322,9 @@ wxRect wx_table_generator::cell_rect(std::size_t column, 
int y)
 /// text and its vertical position is adjusted so that it can be directly
 /// passed to wxDC::DrawLabel().
 
-wxRect wx_table_generator::text_rect(std::size_t column, int y)
+wxRect wx_table_generator::text_rect(std::size_t column, int y) const
 {
-    wxRect z = cell_rect(column, y).Deflate(dc_.GetCharWidth(), 0);
+    wxRect z = cell_rect(column, y).Deflate(dc().GetCharWidth(), 0);
     z.Offset(0, char_height_);
     return z;
 }
@@ -563,12 +568,12 @@ void wx_table_generator::do_output_single_row
                     break;
                 case oe_center:
                     {
-                    x_text += (ci.col_width() - dc_.GetTextExtent(s).x) / 2;
+                    x_text += (ci.col_width() - dc().GetTextExtent(s).x) / 2;
                     }
                     break;
                 case oe_right:
                     {
-                    x_text += ci.col_width() - dc_.GetTextExtent(s).x;
+                    x_text += ci.col_width() - dc().GetTextExtent(s).x;
                     }
                     break;
                 }
@@ -692,10 +697,10 @@ void wx_table_generator::output_headers
         header_font_setter.Set(get_header_font());
         // The distance from the font's descender line to its ascender
         // line must not exceed the distance between lines.
-        LMI_ASSERT(dc_.GetCharHeight() <= row_height());
+        LMI_ASSERT(dc().GetCharHeight() <= row_height());
         // do_output_single_row(), called below, uses a cached char_height_
         // that is assumed not to differ from the bold GetCharHeight().
-        LMI_ASSERT(dc_.GetCharHeight() == char_height_);
+        LMI_ASSERT(dc().GetCharHeight() == char_height_);
         }
 
     // Split headers in single lines and fill up the entire columns*lines 2D
diff --git a/wx_table_generator.hpp b/wx_table_generator.hpp
index 468351c..fc35091 100644
--- a/wx_table_generator.hpp
+++ b/wx_table_generator.hpp
@@ -115,7 +115,7 @@ class wx_table_generator
 
     int row_height() const;
 
-    wxRect text_rect(std::size_t column, int y);
+    wxRect text_rect(std::size_t column, int y) const;
 
     void output_horz_separator
         (std::size_t                  begin_column
@@ -134,9 +134,9 @@ class wx_table_generator
 
     wxFont get_header_font() const;
 
-    wxRect cell_rect(std::size_t column, int y);
+    wxRect cell_rect(std::size_t column, int y) const;
 
-    int do_get_cell_x(std::size_t column);
+    int do_get_cell_x(std::size_t column) const;
 
     void do_output_horz_separator(int x1, int x2, int y );
     void do_output_vert_separator(int x , int y1, int y2);
@@ -149,6 +149,7 @@ class wx_table_generator
 
     // Const private accessors. Used in the implementation to
     // distinguish access from mutation.
+    wxDC const& dc() const;
     int column_margin() const;
     std::vector<column_info> const& all_columns() const;
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]