lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [6231] Refactor


From: Greg Chicares
Subject: [lmi-commits] [6231] Refactor
Date: Tue, 04 Aug 2015 15:58:39 +0000

Revision: 6231
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=6231
Author:   chicares
Date:     2015-08-04 15:58:36 +0000 (Tue, 04 Aug 2015)
Log Message:
-----------
Refactor

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/emit_ledger.cpp
    lmi/trunk/emit_ledger.hpp
    lmi/trunk/group_values.cpp
    lmi/trunk/illustration_view.cpp
    lmi/trunk/illustrator.cpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2015-08-03 23:43:48 UTC (rev 6230)
+++ lmi/trunk/ChangeLog 2015-08-04 15:58:36 UTC (rev 6231)
@@ -36468,5 +36468,20 @@
   emit_ledger.cpp
   illustrator.cpp
 Don't overwrite input file. See:
-  http://lists.nongnu.org/archive/html/lmi/2015-07/msg00030.html
+  http://lists.nongnu.org/archive/html/lmi/2015-08/msg00000.html
 
+20150803T2343Z <address@hidden> [508]
+
+  illustrator.cpp
+Refactor.
+
+20150804T1558Z <address@hidden> [508]
+
+  emit_ledger.cpp
+  emit_ledger.hpp
+  group_values.cpp
+  illustration_view.cpp
+  illustrator.cpp
+Refactor. See:
+  http://lists.nongnu.org/archive/html/lmi/2015-08/msg00001.html
+

Modified: lmi/trunk/emit_ledger.cpp
===================================================================
--- lmi/trunk/emit_ledger.cpp   2015-08-03 23:43:48 UTC (rev 6230)
+++ lmi/trunk/emit_ledger.cpp   2015-08-04 15:58:36 UTC (rev 6231)
@@ -1,4 +1,4 @@
-// Emit a ledger in various guises.
+// Emit a ledger or a group of ledgers in various guises.
 //
 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015 Gregory W. Chicares.
 //
@@ -45,29 +45,42 @@
 #include <iostream>
 #include <string>
 
-/// Prepare to emit ledger(s) in various guises.
+/// Emit a group of ledgers in various guises.
+///
+/// The ledgers constitute a 'case' consisting of 'cells' as those
+/// concepts are defined for class multiple_cell_document.
 
-double pre_emit_ledger
-    (fs::path const& tsv_filepath
+ledger_emitter::ledger_emitter
+    (fs::path const& case_filepath
     ,mcenum_emission emission
     )
+    :case_filepath_ (case_filepath)
+    ,emission_      (emission)
+{}
+
+ledger_emitter::~ledger_emitter()
+{}
+
+/// Perform initial case-level steps such as writing headers.
+
+double ledger_emitter::initiate()
 {
     Timer timer;
 
-    if(emission & mce_emit_spreadsheet)
+    if(emission_ & mce_emit_spreadsheet)
         {
-        LMI_ASSERT(!tsv_filepath.empty());
+        LMI_ASSERT(!case_filepath_.empty());
         std::string spreadsheet_filename =
-                tsv_filepath.string()
+                case_filepath_.string()
             +   configurable_settings::instance().spreadsheet_file_extension()
             ;
         std::remove(spreadsheet_filename.c_str());
         }
-    if(emission & mce_emit_group_roster)
+    if(emission_ & mce_emit_group_roster)
         {
-        LMI_ASSERT(!tsv_filepath.empty());
+        LMI_ASSERT(!case_filepath_.empty());
         std::string spreadsheet_filename =
-                tsv_filepath.string()
+                case_filepath_.string()
             +   ".roster"
             +   configurable_settings::instance().spreadsheet_file_extension()
             ;
@@ -78,103 +91,81 @@
     return timer.stop().elapsed_seconds();
 }
 
-/// Emit a ledger in various guises.
-///
-/// The commands for
-///   mce_emit_pdf_file
-///   mce_emit_pdf_to_printer
-///   mce_emit_pdf_to_viewer
-/// are spelled out separately and in full, which is redundant.
-/// Reason: in the future, they may be implemented differently.
-/// In particular, mce_emit_pdf_to_printer might pass '-print' to
-/// 'fop' instead of using wxTheMimeTypesManager, which would make
-/// it available with the command-line interface. That interface
-/// doesn't support mce_emit_pdf_to_viewer at all: it could be
-/// supported in a platform-dependent way, but it would be strange
-/// to require a command-line program to invoke an external GUI
-/// program.
-///
-/// The 'tsv_filepath' argument is used only for
-///   mce_emit_spreadsheet
-///   mce_emit_group_roster
-/// for which a single output file encompasses all cells in a census,
-/// whereas other output types produce a separate file for each cell.
+/// Perform cell-level steps.
 
-double emit_ledger
-    (fs::path const& filepath
-    ,fs::path const& tsv_filepath
-    ,Ledger const&   ledger
-    ,mcenum_emission emission
+double ledger_emitter::emit_cell
+    (fs::path const& cell_filepath
+    ,Ledger const& ledger
     )
 {
     Timer timer;
-    if((emission & mce_emit_composite_only) && !ledger.GetIsComposite())
+    if((emission_ & mce_emit_composite_only) && !ledger.GetIsComposite())
         {
         goto done;
         }
 
-    if(emission & mce_emit_pdf_file)
+    if(emission_ & mce_emit_pdf_file)
         {
-        write_ledger_as_pdf(ledger, filepath);
+        write_ledger_as_pdf(ledger, cell_filepath);
         }
-    if(emission & mce_emit_pdf_to_printer)
+    if(emission_ & mce_emit_pdf_to_printer)
         {
-        std::string pdf_out_file = write_ledger_as_pdf(ledger, filepath);
+        std::string pdf_out_file = write_ledger_as_pdf(ledger, cell_filepath);
         file_command()(pdf_out_file, "print");
         }
-    if(emission & mce_emit_pdf_to_viewer)
+    if(emission_ & mce_emit_pdf_to_viewer)
         {
-        std::string pdf_out_file = write_ledger_as_pdf(ledger, filepath);
+        std::string pdf_out_file = write_ledger_as_pdf(ledger, cell_filepath);
         file_command()(pdf_out_file, "open");
         }
-    if(emission & mce_emit_test_data)
+    if(emission_ & mce_emit_test_data)
         {
         fs::ofstream ofs
-            (fs::change_extension(filepath, ".test")
+            (fs::change_extension(cell_filepath, ".test")
             ,ios_out_trunc_binary()
             );
         ledger.Spew(ofs);
         }
-    if(emission & mce_emit_spreadsheet)
+    if(emission_ & mce_emit_spreadsheet)
         {
-        LMI_ASSERT(!tsv_filepath.empty());
+        LMI_ASSERT(!case_filepath_.empty());
         PrintCellTabDelimited
             (ledger
-            ,   tsv_filepath.string()
+            ,   case_filepath_.string()
             +   configurable_settings::instance().spreadsheet_file_extension()
             );
         }
-    if(emission & mce_emit_group_roster)
+    if(emission_ & mce_emit_group_roster)
         {
-        LMI_ASSERT(!tsv_filepath.empty());
+        LMI_ASSERT(!case_filepath_.empty());
         PrintRosterTabDelimited
             (ledger
-            ,   tsv_filepath.string()
+            ,   case_filepath_.string()
             +   ".roster"
             +   configurable_settings::instance().spreadsheet_file_extension()
             );
         }
-    if(emission & mce_emit_text_stream)
+    if(emission_ & mce_emit_text_stream)
         {
         PrintLedgerFlatText(ledger, std::cout);
         }
-    if(emission & mce_emit_custom_0)
+    if(emission_ & mce_emit_custom_0)
         {
         configurable_settings const& c = configurable_settings::instance();
         fs::path out_file =
-            filepath.string() == c.custom_input_0_filename()
+            cell_filepath.string() == c.custom_input_0_filename()
             ? c.custom_output_0_filename()
-            : fs::change_extension(filepath, ".test0")
+            : fs::change_extension(cell_filepath, ".test0")
             ;
         custom_io_0_write(ledger, out_file.string());
         }
-    if(emission & mce_emit_custom_1)
+    if(emission_ & mce_emit_custom_1)
         {
         configurable_settings const& c = configurable_settings::instance();
         fs::path out_file =
-            filepath.string() == c.custom_input_1_filename()
+            cell_filepath.string() == c.custom_input_1_filename()
             ? c.custom_output_1_filename()
-            : fs::change_extension(filepath, ".test1")
+            : fs::change_extension(cell_filepath, ".test1")
             ;
         custom_io_1_write(ledger, out_file.string());
         }
@@ -183,3 +174,28 @@
     return timer.stop().elapsed_seconds();
 }
 
+/// Perform final case-level steps such as numbering output pages.
+
+double ledger_emitter::finish()
+{
+    Timer timer;
+
+    ; // Nothing to do for now.
+
+    return timer.stop().elapsed_seconds();
+}
+
+/// Emit a single ledger in various guises.
+///
+/// Return time spent, which is almost always wanted.
+
+double emit_ledger
+    (fs::path const& cell_filepath
+    ,Ledger const&   ledger
+    ,mcenum_emission emission
+    )
+{
+    ledger_emitter emitter("", emission);
+    return emitter.emit_cell(cell_filepath, ledger);
+}
+

Modified: lmi/trunk/emit_ledger.hpp
===================================================================
--- lmi/trunk/emit_ledger.hpp   2015-08-03 23:43:48 UTC (rev 6230)
+++ lmi/trunk/emit_ledger.hpp   2015-08-04 15:58:36 UTC (rev 6231)
@@ -1,4 +1,4 @@
-// Emit a ledger in various guises.
+// Emit a ledger or a group of ledgers in various guises.
 //
 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015 Gregory W. Chicares.
 //
@@ -26,21 +26,39 @@
 
 #include "config.hpp"
 
-#include "mc_enum_type_enums.hpp" // enum mcenum_emission
+#include "mc_enum_type_enums.hpp"       // enum mcenum_emission
+#include "obstruct_slicing.hpp"
 #include "so_attributes.hpp"
+#include "uncopyable_lmi.hpp"
 
 #include <boost/filesystem/path.hpp>
 
 class Ledger;
 
-double LMI_SO pre_emit_ledger
-    (fs::path const& tsv_filepath
-    ,mcenum_emission emission
-    );
+/// Emit a group of ledgers in various guises.
+///
+/// Each member function (except the lightweight ctor and dtor)
+/// returns time spent, which is almost always wanted.
 
+class LMI_SO ledger_emitter
+    :        private lmi::uncopyable <ledger_emitter>
+    ,virtual private obstruct_slicing<ledger_emitter>
+{
+  public:
+    ledger_emitter(fs::path const& case_filepath, mcenum_emission emission);
+    ~ledger_emitter();
+
+    double initiate ();
+    double emit_cell(fs::path const& cell_filepath, Ledger const& ledger);
+    double finish   ();
+
+  private:
+    fs::path const& case_filepath_;
+    mcenum_emission emission_;
+};
+
 double LMI_SO emit_ledger
-    (fs::path const& filepath
-    ,fs::path const& tsv_filepath
+    (fs::path const& cell_filepath
     ,Ledger const&   ledger
     ,mcenum_emission emission
     );

Modified: lmi/trunk/group_values.cpp
===================================================================
--- lmi/trunk/group_values.cpp  2015-08-03 23:43:48 UTC (rev 6230)
+++ lmi/trunk/group_values.cpp  2015-08-04 15:58:36 UTC (rev 6231)
@@ -130,7 +130,8 @@
             )
         );
 
-    result.seconds_for_output_ += pre_emit_ledger(file, emission);
+    ledger_emitter emitter(file, emission);
+    result.seconds_for_output_ += emitter.initiate();
 
     for(unsigned int j = 0; j < cells.size(); ++j)
         {
@@ -140,11 +141,9 @@
             IllusVal IV(serial_file_path(file, name, j, "hastur").string());
             IV.run(cells[j]);
             composite.PlusEq(*IV.ledger());
-            result.seconds_for_output_ += emit_ledger
+            result.seconds_for_output_ += emitter.emit_cell
                 (serial_file_path(file, name, j, "hastur")
-                ,file
                 ,*IV.ledger()
-                ,emission
                 );
             meter->dawdle(intermission_between_printouts(emission));
             }
@@ -156,12 +155,11 @@
         }
     meter->culminate();
 
-    result.seconds_for_output_ += emit_ledger
+    result.seconds_for_output_ += emitter.emit_cell
         (serial_file_path(file, "composite", -1, "hastur")
-        ,file
         ,composite
-        ,emission
         );
+    result.seconds_for_output_ += emitter.finish();
 
   done:
     double total_seconds = timer.stop().elapsed_seconds();
@@ -247,12 +245,6 @@
 {
     Timer timer;
     census_run_result result;
-
-    std::vector<Input>::const_iterator ip;
-    std::vector<boost::shared_ptr<AccountValue> > cell_values;
-    std::vector<boost::shared_ptr<AccountValue> >::iterator i;
-    std::vector<mcenum_run_basis> const& RunBases = composite.GetRunBases();
-
     boost::shared_ptr<progress_meter> meter
         (create_progress_meter
             (cells.size()
@@ -260,6 +252,14 @@
             ,progress_meter_mode(emission)
             )
         );
+
+    ledger_emitter emitter(file, emission);
+
+    std::vector<Input>::const_iterator ip;
+    std::vector<boost::shared_ptr<AccountValue> > cell_values;
+    std::vector<boost::shared_ptr<AccountValue> >::iterator i;
+    std::vector<mcenum_run_basis> const& RunBases = composite.GetRunBases();
+
     int j = 0;
     int const first_cell_inforce_year  = 
value_cast<int>((*cells.begin())["InforceYear"].str());
     int const first_cell_inforce_month = 
value_cast<int>((*cells.begin())["InforceMonth"].str());
@@ -634,7 +634,7 @@
         }
     meter->culminate();
 
-    result.seconds_for_output_ += pre_emit_ledger(file, emission);
+    result.seconds_for_output_ += emitter.initiate();
 
     meter = create_progress_meter
         (cell_values.size()
@@ -644,11 +644,9 @@
     for(j = 0, i = cell_values.begin(); i != cell_values.end(); ++i, ++j)
         {
         std::string const name(cells[j]["InsuredName"].str());
-        result.seconds_for_output_ += emit_ledger
+        result.seconds_for_output_ += emitter.emit_cell
             (serial_file_path(file, name, j, "hastur")
-            ,file
             ,*(*i)->ledger_from_av()
-            ,emission
             );
         meter->dawdle(intermission_between_printouts(emission));
         if(!meter->reflect_progress())
@@ -659,12 +657,11 @@
         }
     meter->culminate();
 
-    result.seconds_for_output_ += emit_ledger
+    result.seconds_for_output_ += emitter.emit_cell
         (serial_file_path(file, "composite", -1, "hastur")
-        ,file
         ,composite
-        ,emission
         );
+    result.seconds_for_output_ += emitter.finish();
 
   done:
     double total_seconds = timer.stop().elapsed_seconds();

Modified: lmi/trunk/illustration_view.cpp
===================================================================
--- lmi/trunk/illustration_view.cpp     2015-08-03 23:43:48 UTC (rev 6230)
+++ lmi/trunk/illustration_view.cpp     2015-08-04 15:58:36 UTC (rev 6231)
@@ -224,7 +224,7 @@
 void IllustrationView::emit_pdf(mcenum_emission e)
 {
     LMI_ASSERT(ledger_values_.get());
-    double s = emit_ledger(base_filename(), "", *ledger_values_, e);
+    double s = emit_ledger(base_filename(), *ledger_values_, e);
     status() << "Output: " << Timer::elapsed_msec_str(s) << std::flush;
 }
 

Modified: lmi/trunk/illustrator.cpp
===================================================================
--- lmi/trunk/illustrator.cpp   2015-08-03 23:43:48 UTC (rev 6230)
+++ lmi/trunk/illustrator.cpp   2015-08-04 15:58:36 UTC (rev 6231)
@@ -91,12 +91,7 @@
         z.run(input);
         principal_ledger_ = z.ledger();
         seconds_for_calculations_ = timer.stop().elapsed_seconds();
-        seconds_for_output_ = emit_ledger
-            (file_path
-            ,file_path
-            ,*z.ledger()
-            ,emission_
-            );
+        seconds_for_output_ = emit_ledger(file_path, *z.ledger(), emission_);
         conditionally_show_timings_on_stdout();
         return close_when_done;
         }
@@ -113,7 +108,7 @@
         seconds_for_calculations_ = timer.stop().elapsed_seconds();
         mcenum_emission x = emit_pdf_too ? mce_emit_pdf_file : 
mce_emit_nothing;
         mcenum_emission y = static_cast<mcenum_emission>(x | emission_);
-        seconds_for_output_ = emit_ledger(file_path, file_path, *z.ledger(), 
y);
+        seconds_for_output_ = emit_ledger(file_path, *z.ledger(), y);
         conditionally_show_timings_on_stdout();
         return true;
         }
@@ -138,12 +133,7 @@
     IV.run(z);
     principal_ledger_ = IV.ledger();
     seconds_for_calculations_ = timer.stop().elapsed_seconds();
-    seconds_for_output_ = emit_ledger
-        (file_path
-        ,file_path
-        ,*IV.ledger()
-        ,emission_
-        );
+    seconds_for_output_ = emit_ledger(file_path, *IV.ledger(), emission_);
     conditionally_show_timings_on_stdout();
     return true;
 }




reply via email to

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