lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 8252fb3 2/6: Establish an invariant that will


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 8252fb3 2/6: Establish an invariant that will become useful ere long
Date: Fri, 22 Jan 2021 21:55:54 -0500 (EST)

branch: master
commit 8252fb3261f04c88432004b267f184073c57f3ad
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Establish an invariant that will become useful ere long
    
    Invariant: the class for each product-file type is constructible from
    a filesystem::path specifying the file's name, by means of a ctor marked
    as 'explicit'.
---
 dbdict.cpp             |  5 ++---
 dbdict.hpp             |  4 +++-
 fund_data.cpp          |  5 ++---
 fund_data.hpp          |  4 +++-
 lingo.cpp              |  5 ++---
 lingo.hpp              |  4 +++-
 product_data.cpp       | 55 +++++++++++++++++++++++++++++++++-----------------
 product_data.hpp       |  5 +++++
 rounding_rules.cpp     |  3 +--
 rounding_rules.hpp     |  4 +++-
 stratified_charges.cpp |  2 +-
 stratified_charges.hpp |  4 +++-
 12 files changed, 64 insertions(+), 36 deletions(-)

diff --git a/dbdict.cpp b/dbdict.cpp
index 664f3ac..6ce5cd4 100644
--- a/dbdict.cpp
+++ b/dbdict.cpp
@@ -45,7 +45,6 @@
 #include <boost/filesystem/convenience.hpp>
 #include <boost/filesystem/fstream.hpp>
 #include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
 
 #include <vector>
 
@@ -99,11 +98,11 @@ DBDictionary::DBDictionary()
 /// seem unnecessary, in case the file read by Init() lacks any member
 /// entity (because it's an older version or has been edited, e.g.).
 
-DBDictionary::DBDictionary(std::string const& filename)
+DBDictionary::DBDictionary(fs::path const& filename)
 {
     ascribe_members();
     InitDB();
-    Init(filename);
+    Init(filename.string());
 }
 
 database_entity const& DBDictionary::datum(std::string const& name) const
diff --git a/dbdict.hpp b/dbdict.hpp
index a09c590..699fbdf 100644
--- a/dbdict.hpp
+++ b/dbdict.hpp
@@ -30,6 +30,8 @@
 #include "so_attributes.hpp"
 #include "xml_serializable.hpp"
 
+#include <boost/filesystem/path.hpp>
+
 #include <string>
 
 /// Cached product database.
@@ -45,7 +47,7 @@ class LMI_SO DBDictionary
 
   public:
     DBDictionary();
-    DBDictionary(std::string const& filename);
+    explicit DBDictionary(fs::path const& filename);
 
     ~DBDictionary() override = default;
 
diff --git a/fund_data.cpp b/fund_data.cpp
index ab424ab..702cd65 100644
--- a/fund_data.cpp
+++ b/fund_data.cpp
@@ -33,7 +33,6 @@
 #include "xml_serialize.hpp"
 
 #include <boost/filesystem/convenience.hpp> // basename()
-#include <boost/filesystem/path.hpp>
 
 //============================================================================
 FundInfo::FundInfo
@@ -77,9 +76,9 @@ template<> struct xml_io<FundInfo>
 } // namespace xml_serialize
 
 //============================================================================
-FundData::FundData(std::string const& a_Filename)
+FundData::FundData(fs::path const& a_Filename)
 {
-    Read(a_Filename);
+    Read(a_Filename.string());
 }
 
 int FundData::GetNumberOfFunds() const
diff --git a/fund_data.hpp b/fund_data.hpp
index 75e4757..a54ab21 100644
--- a/fund_data.hpp
+++ b/fund_data.hpp
@@ -26,6 +26,8 @@
 
 #include "so_attributes.hpp"
 
+#include <boost/filesystem/path.hpp>
+
 #include <string>
 #include <vector>
 
@@ -62,7 +64,7 @@ class LMI_SO FundInfo final
 class LMI_SO FundData final
 {
   public:
-    FundData(std::string const& a_Filename);
+    explicit FundData(fs::path const& a_Filename);
     ~FundData() = default;
 
     static void write_funds_files();
diff --git a/lingo.cpp b/lingo.cpp
index 5629008..79f9056 100644
--- a/lingo.cpp
+++ b/lingo.cpp
@@ -32,13 +32,12 @@
 #include "xml_serialize.hpp"
 
 #include <boost/filesystem/convenience.hpp>
-#include <boost/filesystem/path.hpp>
 
 /// Construct from filename.
 
-lingo::lingo(std::string const& filename)
+lingo::lingo(fs::path const& filename)
 {
-    xml_lmi::dom_parser parser(filename);
+    xml_lmi::dom_parser parser(filename.string());
     xml::element const& root = parser.root_node(xml_root_name());
     int file_version = 0;
     if(!xml_lmi::get_attr(root, "version", file_version))
diff --git a/lingo.hpp b/lingo.hpp
index 4ac2875..4a2e405 100644
--- a/lingo.hpp
+++ b/lingo.hpp
@@ -28,6 +28,8 @@
 #include "so_attributes.hpp"
 #include "xml_lmi_fwd.hpp"
 
+#include <boost/filesystem/path.hpp>
+
 #include <string>
 #include <unordered_map>
 
@@ -37,7 +39,7 @@ class LMI_SO lingo final
     :public cache_file_reads<lingo>
 {
   public:
-    explicit lingo(std::string const& filename);
+    explicit lingo(fs::path const& filename);
 
     std::string const& lookup(int) const;
 
diff --git a/product_data.cpp b/product_data.cpp
index 4798322..056ff5e 100644
--- a/product_data.cpp
+++ b/product_data.cpp
@@ -33,7 +33,6 @@
 #include "xml_serialize.hpp"
 
 #include <boost/filesystem/convenience.hpp>
-#include <boost/filesystem/path.hpp>
 
 #include <vector>
 
@@ -124,27 +123,15 @@ product_data::product_data()
     ascribe_members();
 }
 
-/// Construct from product name.
-///
-/// The argument is a string (typically Input::ProductName) such as
-/// 'sample'. The appropriate extension and path are added here to
-/// produce a filepath.
-///
-/// Somewhat arbitrarily, forbid '.' in product names. There's no real
-/// need to allow that, and it would complicate the code. A product
-/// name like "ul.with.variable.funds" could too easily be mistaken
-/// for a '.funds' file. The boost filesystem portability guidelines
-/// suggest "Do not use more that [sic] one period in a file name",
-/// and extensions are added to product names to create file names.
-
-product_data::product_data(std::string const& product_name)
+product_data::product_data(fs::path const& product_filename)
 {
     ascribe_members();
+    load(product_filename);
+}
 
-    fs::path path(product_name);
-    LMI_ASSERT(product_name == fs::basename(path));
-    path = fs::change_extension(path, ".policy");
-    load(AddDataDir(path.string()));
+product_data::product_data(std::string const& product_name)
+    :product_data(fs::path(filename_from_product_name(product_name)))
+{
 }
 
 product_data::product_data(product_data const& z)
@@ -938,6 +925,36 @@ void product_data::write_policy_files()
     sample2xyz  ().save(AddDataDir("sample2xyz.policy"));
 }
 
+/// Convert a product name to the name of its '.product' file.
+///
+/// For example: 'sample' --> '/opt/lmi/data/sample.product'.
+///
+/// The argument is a string (typically Input::ProductName) such as
+/// 'sample'. The appropriate extension and path are added here to
+/// produce a filepath.
+///
+/// Somewhat arbitrarily, forbid '.' in product names. There's no real
+/// need to allow that, and it would complicate the code. A product
+/// name like "ul.with.variable.funds" could too easily be mistaken
+/// for a '.funds' file. The boost filesystem portability guidelines
+/// suggest "Do not use more that [sic] one period in a file name",
+/// and extensions are added to product names to create file names.
+///
+/// Rejected alternative: take a 'ce_product_name' argument instead.
+/// That would constrain the argument in a natural way, but would
+/// force coupling between class ce_product_name and client code
+/// that has no other need to know about it; furthermore, the range
+/// of 'ce_product_name' values is determined only at run time, and
+/// it would be strange to propagate a run-time dependency.
+
+std::string filename_from_product_name(std::string const& product_name)
+{
+    fs::path path(product_name);
+    LMI_ASSERT(product_name == fs::basename(path));
+    path = fs::change_extension(path, ".policy");
+    return AddDataDir(path.string());
+}
+
 /// Load from file. This free function can be invoked across dll
 /// boundaries, even though xml_serializable<> is instantiated only
 /// in the present TU.
diff --git a/product_data.hpp b/product_data.hpp
index 09249bb..fc8cb6e 100644
--- a/product_data.hpp
+++ b/product_data.hpp
@@ -28,6 +28,8 @@
 #include "so_attributes.hpp"
 #include "xml_serializable.hpp"
 
+#include <boost/filesystem/path.hpp>
+
 #include <string>
 
 /// A single product datum: a string with an optional gloss.
@@ -87,6 +89,7 @@ class LMI_SO product_data
     typedef deserialized<product_data>::value_type value_type;
 
   public:
+    explicit product_data(fs::path const& product_filename);
     explicit product_data(std::string const& product_name);
     ~product_data() override;
 
@@ -367,6 +370,8 @@ class LMI_SO product_data
     glossed_string DefnSpecAmt;
 };
 
+LMI_SO std::string filename_from_product_name(std::string const&);
+
 LMI_SO void load(product_data      &, fs::path const&);
 LMI_SO void save(product_data const&, fs::path const&);
 
diff --git a/rounding_rules.cpp b/rounding_rules.cpp
index fa78497..a0dd519 100644
--- a/rounding_rules.cpp
+++ b/rounding_rules.cpp
@@ -31,7 +31,6 @@
 #include "xml_serialize.hpp"
 
 #include <boost/filesystem/convenience.hpp>
-#include <boost/filesystem/path.hpp>
 
 template class xml_serializable<rounding_rules>;
 
@@ -167,7 +166,7 @@ rounding_rules::rounding_rules()
 /// that violate this precondition, but this ctor prevents them from
 /// being used to produce illustrations.
 
-rounding_rules::rounding_rules(std::string const& filename)
+rounding_rules::rounding_rules(fs::path const& filename)
 {
     ascribe_members();
 
diff --git a/rounding_rules.hpp b/rounding_rules.hpp
index 7b6328c..37872d5 100644
--- a/rounding_rules.hpp
+++ b/rounding_rules.hpp
@@ -30,6 +30,8 @@
 #include "so_attributes.hpp"
 #include "xml_serializable.hpp"
 
+#include <boost/filesystem/path.hpp>
+
 #include <string>
 
 /// Parameters of a rounding rule.
@@ -95,7 +97,7 @@ class LMI_SO rounding_rules final
     friend class RoundingDocument;
 
   public:
-    explicit rounding_rules(std::string const& filename);
+    explicit rounding_rules(fs::path const& filename);
     ~rounding_rules() override = default;
 
     rounding_parameters const& datum(std::string const& name) const;
diff --git a/stratified_charges.cpp b/stratified_charges.cpp
index 34ef1fc..ad23a1d 100644
--- a/stratified_charges.cpp
+++ b/stratified_charges.cpp
@@ -168,7 +168,7 @@ stratified_charges::stratified_charges()
     ascribe_members();
 }
 
-stratified_charges::stratified_charges(std::string const& filename)
+stratified_charges::stratified_charges(fs::path const& filename)
 {
     ascribe_members();
     load(filename);
diff --git a/stratified_charges.hpp b/stratified_charges.hpp
index 4cdde13..f703c49 100644
--- a/stratified_charges.hpp
+++ b/stratified_charges.hpp
@@ -29,6 +29,8 @@
 #include "so_attributes.hpp"
 #include "xml_serializable.hpp"
 
+#include <boost/filesystem/path.hpp>
+
 #include <string>
 #include <vector>
 
@@ -103,7 +105,7 @@ class LMI_SO stratified_charges final
     friend class TierDocument;
 
   public:
-    stratified_charges(std::string const& filename);
+    explicit stratified_charges(fs::path const& filename);
     stratified_charges(stratified_charges const&);
     ~stratified_charges() override = default;
 



reply via email to

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