lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] (no subject)


From: Greg Chicares
Subject: [lmi-commits] (no subject)
Date: Tue, 31 May 2016 12:19:08 +0000 (UTC)

branch: master
commit cdd9aa41fb4860a00705ccb80b9c8eb98b9928ad
Author: Gregory W. Chicares <address@hidden>
Date:   Tue May 31 12:16:59 2016 +0000

    Reduce dissimilarities between 'ce_*' classes
    
    Merge 'product_names.?pp' into the only TU that uses it, so that
    internals are hidden better.
    
    For both 'ce_*_names' classes, remove static '*_names' members:
    free functions in an unnamed namespace increase encapsulation.
    
    Rearrange functions in unnamed namespace, using the same order in
    both classes to make comparison easier.
---
 Makefile.am         |    3 --
 ce_product_name.cpp |   82 +++++++++++++++++++++++++++++++++++++++++++----
 ce_product_name.hpp |    2 --
 ce_skin_name.cpp    |   28 ++++++++--------
 ce_skin_name.hpp    |   14 ++++----
 objects.make        |    2 --
 product_names.cpp   |   89 ---------------------------------------------------
 product_names.hpp   |   51 -----------------------------
 8 files changed, 95 insertions(+), 176 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index d73025f..b59998c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -345,7 +345,6 @@ liblmi_common_sources = \
     outlay.cpp \
     path_utility.cpp \
     premium_tax.cpp \
-    product_names.cpp \
     progress_meter.cpp \
     sigfpe.cpp \
     single_cell_document.cpp \
@@ -739,7 +738,6 @@ test_input_SOURCES = \
   null_stream.cpp \
   path_utility.cpp \
   premium_tax.cpp \
-  product_names.cpp \
   single_cell_document.cpp \
   timer.cpp \
   tn_range_types.cpp \
@@ -1212,7 +1210,6 @@ noinst_HEADERS = \
     print_matrix.hpp \
     product_data.hpp \
     product_editor.hpp \
-    product_names.hpp \
     progress_meter.hpp \
     round_to.hpp \
     rounding_document.hpp \
diff --git a/ce_product_name.cpp b/ce_product_name.cpp
index 0dd3888..5c0fd62 100644
--- a/ce_product_name.cpp
+++ b/ce_product_name.cpp
@@ -22,10 +22,83 @@
 #include "ce_product_name.hpp"
 
 #include "alert.hpp"
+#include "contains.hpp"
 #include "facets.hpp"
-#include "product_names.hpp"
+#include "global_settings.hpp"
+#include "miscellany.hpp"               // lmi_tolower()
+#include "path_utility.hpp"             // fs::path inserter
 
-#include <algorithm>
+#include <boost/filesystem/convenience.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+#include <algorithm>                    // std::find(), std::transform()
+
+namespace
+{
+std::vector<std::string> fetch_product_names()
+{
+    fs::path path(global_settings::instance().data_directory());
+    std::vector<std::string> names;
+    fs::directory_iterator i(path);
+    fs::directory_iterator end_i;
+    for(; i != end_i; ++i)
+        {
+        if(is_directory(*i) || ".policy" != fs::extension(*i))
+            {
+            continue;
+            }
+        std::string name(basename(*i));
+        std::transform
+            (name.begin()
+            ,name.end()
+            ,name.begin()
+            ,lmi_tolower
+            );
+        names.push_back(name);
+        }
+
+    if(names.empty())
+        {
+        fatal_error()
+            << "Data directory '"
+            << path
+            << "' contains no product files."
+            << LMI_FLUSH
+            ;
+        }
+
+    return names;
+}
+
+std::vector<std::string> const& product_names()
+{
+    static std::vector<std::string> const names(fetch_product_names());
+    return names;
+}
+
+/// Default product name is "sample" if that product is available,
+/// else the name of the first product found.
+///
+/// Rationale: It is always possible to specify a different default
+/// product by using a default-input file. If none is used, then the
+/// first product found is not necessarily a better default than
+/// "sample". Defaults hardcoded in the input class are designed to
+/// be generally suitable, but might be inappropriate for some exotic
+/// product. If a user creates an invalid product that appears first
+/// in the list, then the system will still work in default cases
+/// with "sample".
+
+std::string const& default_product_name()
+{
+    static std::string const default_name =
+        contains(product_names(), "sample")
+        ? std::string("sample")
+        : product_names().front()
+        ;
+    return default_name;
+}
+} // Unnamed namespace.
 
 ce_product_name::ce_product_name()
     :mc_enum_base(product_names().size())
@@ -112,11 +185,6 @@ std::string ce_product_name::value() const
     return value_;
 }
 
-std::vector<std::string> const& ce_product_name::product_names()
-{
-    return ::product_names();
-}
-
 std::istream& ce_product_name::read(std::istream& is)
 {
     std::locale old_locale = is.imbue(blank_is_not_whitespace_locale());
diff --git a/ce_product_name.hpp b/ce_product_name.hpp
index 9a57581..80fc0ed 100644
--- a/ce_product_name.hpp
+++ b/ce_product_name.hpp
@@ -90,8 +90,6 @@ class ce_product_name
     std::string value() const;
 
   private:
-    static std::vector<std::string> const& product_names();
-
     // datum_base required implementation.
     // TODO ?? Consider moving the implementation into the base class.
     virtual std::istream& read (std::istream&);
diff --git a/ce_skin_name.cpp b/ce_skin_name.cpp
index 1c552e7..6945b42 100644
--- a/ce_skin_name.cpp
+++ b/ce_skin_name.cpp
@@ -30,17 +30,11 @@
 #include <boost/filesystem/operations.hpp>
 #include <boost/filesystem/path.hpp>
 
-#include <algorithm>
-#include <cstring>
+#include <algorithm>                    // std::find()
+#include <cstring>                      // std::strlen()
 
 namespace
 {
-std::string const& default_skin_name()
-{
-    static std::string const default_name("default");
-    return default_name;
-}
-
 std::vector<std::string> fetch_skin_names()
 {
     fs::path path(global_settings::instance().data_directory());
@@ -82,6 +76,18 @@ std::vector<std::string> fetch_skin_names()
 
     return names;
 }
+
+std::vector<std::string> const& skin_names()
+{
+    static std::vector<std::string> const names(fetch_skin_names());
+    return names;
+}
+
+std::string const& default_skin_name()
+{
+    static std::string const default_name("default");
+    return default_name;
+}
 } // Unnamed namespace.
 
 ce_skin_name::ce_skin_name()
@@ -169,12 +175,6 @@ std::string ce_skin_name::value() const
     return value_;
 }
 
-std::vector<std::string> const& ce_skin_name::skin_names()
-{
-    static std::vector<std::string> const names(fetch_skin_names());
-    return names;
-}
-
 std::istream& ce_skin_name::read(std::istream& is)
 {
     std::locale old_locale = is.imbue(blank_is_not_whitespace_locale());
diff --git a/ce_skin_name.hpp b/ce_skin_name.hpp
index 39c1dc3..8053c96 100644
--- a/ce_skin_name.hpp
+++ b/ce_skin_name.hpp
@@ -30,14 +30,14 @@
 #include <string>
 #include <vector>
 
-/// This class encapsulates skin names. It is similar to ce_product_name in
-/// that its values are only available at run time, so there can be no compile
-/// time enum to represent them.
+/// This class encapsulates skin names. As for the related class
+/// ce_product_name, its values are only available at run time, so
+/// there can be no compile time enum to represent them.
 ///
 /// Valid values are the base names of 'skin*.xrc' product files found
-/// in the (configurable) data directory. As with ce_product_name, the valid
-/// values never change during the program lifetime and it needs to be
-/// restarted to "notice" the new skins.
+/// in the (configurable) data directory. As with ce_product_name, the
+/// valid values never change during the program lifetime and it needs
+/// to be restarted to "notice" any new skins.
 
 class ce_skin_name
     :public mc_enum_base
@@ -66,8 +66,6 @@ class ce_skin_name
     std::string value() const;
 
   private:
-    static std::vector<std::string> const& skin_names();
-
     // datum_base required implementation.
     // TODO ?? Consider moving the implementation into the base class.
     virtual std::istream& read (std::istream&);
diff --git a/objects.make b/objects.make
index 24f26ea..6040ab5 100644
--- a/objects.make
+++ b/objects.make
@@ -236,7 +236,6 @@ common_common_objects := \
   outlay.o \
   path_utility.o \
   premium_tax.o \
-  product_names.o \
   progress_meter.o \
   sigfpe.o \
   single_cell_document.o \
@@ -661,7 +660,6 @@ input_test$(EXEEXT): \
   null_stream.o \
   path_utility.o \
   premium_tax.o \
-  product_names.o \
   single_cell_document.o \
   timer.o \
   tn_range_types.o \
diff --git a/product_names.cpp b/product_names.cpp
deleted file mode 100644
index 93fb041..0000000
--- a/product_names.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
-// List of available products.
-//
-// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016 Gregory W. Chicares.
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 2 as
-// published by the Free Software Foundation.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software Foundation,
-// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
-//
-// http://savannah.nongnu.org/projects/lmi
-// email: <address@hidden>
-// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
-
-#include "product_names.hpp"
-
-#include "alert.hpp"
-#include "contains.hpp"
-#include "global_settings.hpp"
-#include "miscellany.hpp"
-#include "path_utility.hpp"             // fs::path inserter
-
-#include <boost/filesystem/convenience.hpp>
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-
-#include <algorithm>
-
-namespace
-{
-std::vector<std::string> fetch_product_names()
-{
-    fs::path path(global_settings::instance().data_directory());
-    std::vector<std::string> names;
-    fs::directory_iterator i(path);
-    fs::directory_iterator end_i;
-    for(; i != end_i; ++i)
-        {
-        if(is_directory(*i) || ".policy" != fs::extension(*i))
-            {
-            continue;
-            }
-        std::string name(basename(*i));
-        std::transform
-            (name.begin()
-            ,name.end()
-            ,name.begin()
-            ,lmi_tolower
-            );
-        names.push_back(name);
-        }
-
-    if(names.empty())
-        {
-        fatal_error()
-            << "Data directory '"
-            << path
-            << "' contains no product files."
-            << LMI_FLUSH
-            ;
-        }
-
-    return names;
-}
-} // Unnamed namespace.
-
-std::vector<std::string> const& product_names()
-{
-    static std::vector<std::string> const names(fetch_product_names());
-    return names;
-}
-
-std::string const& default_product_name()
-{
-    static std::string const default_name =
-        contains(product_names(), "sample")
-        ? std::string("sample")
-        : product_names().front()
-        ;
-    return default_name;
-}
-
diff --git a/product_names.hpp b/product_names.hpp
deleted file mode 100644
index 9526e87..0000000
--- a/product_names.hpp
+++ /dev/null
@@ -1,51 +0,0 @@
-// List of available products.
-//
-// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016 Gregory W. Chicares.
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 2 as
-// published by the Free Software Foundation.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software Foundation,
-// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
-//
-// http://savannah.nongnu.org/projects/lmi
-// email: <address@hidden>
-// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
-
-#ifndef product_names_hpp
-#define product_names_hpp
-
-#include "config.hpp"
-
-#include <string>
-#include <vector>
-
-/// Base names of '.policy' files found in data directory.
-/// They are read only once and cached: see documentation for
-/// class ce_product_name.
-
-std::vector<std::string> const& product_names();
-
-/// Default product name is "sample" if that product is available,
-/// else the name of the first product found.
-///
-/// Rationale: It is always possible to specify a different default
-/// product by using a default-input file. If none is used, then the
-/// first product found is not necessarily a better default than
-/// "sample". Defaults hardcoded in the input class are designed to
-/// be generally suitable, but might be inappropriate for some exotic
-/// product. If a user creates an invalid product that appears first
-/// in the list, then the system will still work in default cases
-/// with "sample".
-
-std::string const& default_product_name();
-
-#endif // product_names_hpp
-



reply via email to

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