lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master c5204be 3/6: Sequester hazardous ctors


From: Greg Chicares
Subject: [lmi-commits] [lmi] master c5204be 3/6: Sequester hazardous ctors
Date: Sun, 26 Feb 2017 19:04:53 -0500 (EST)

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

    Sequester hazardous ctors
    
    Instead of just documenting why these ctors shouldn't be used directly,
    simply forbid it. Incidentally use the recently-added c14n function in
    preference to the obsolescent mathematical_representation().
---
 custom_io_0.cpp         |  2 +-
 input_harmonization.cpp |  2 +-
 input_sequence.cpp      |  9 ++++++++-
 input_sequence.hpp      | 15 ++++++++++++---
 input_sequence_test.cpp |  7 +++++++
 5 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/custom_io_0.cpp b/custom_io_0.cpp
index a6b2dea..fb50b14 100644
--- a/custom_io_0.cpp
+++ b/custom_io_0.cpp
@@ -137,7 +137,7 @@ std::string adjust_interest_rates
         {
         ; // Do nothing.
         }
-    return InputSequence(general_account_rate).mathematical_representation();
+    return canonicalized_input_sequence(general_account_rate);
 }
 
 #if 0
diff --git a/input_harmonization.cpp b/input_harmonization.cpp
index 672b1b8..b8428af 100644
--- a/input_harmonization.cpp
+++ b/input_harmonization.cpp
@@ -45,7 +45,7 @@ namespace
         {
         std::vector<double> z;
         database.Query(z, DB_MaxGenAcctRate);
-        return InputSequence(z).mathematical_representation();
+        return canonicalized_input_sequence(z);
         }
 } // Unnamed namespace.
 
diff --git a/input_sequence.cpp b/input_sequence.cpp
index f30f92e..17739d3 100644
--- a/input_sequence.cpp
+++ b/input_sequence.cpp
@@ -131,6 +131,9 @@ InputSequence::InputSequence
 
 /// Construct from vector: e.g, 1 1 1 2 2 --> 1[0,3); 2[3,4).
 ///
+/// Accessible only by unit test or through free function template
+/// canonicalized_input_sequence().
+///
 /// This is used, e.g., when interest rates obtained from an external
 /// source vary from one year to the next, and it is desired to use
 /// them as lmi input. It might seem that inserting semicolons between
@@ -151,6 +154,9 @@ InputSequence::InputSequence(std::vector<double> const& v)
 
 /// Construct from vector: e.g, a a a b b --> a[0,3); b[3,4).
 ///
+/// Accessible only by unit test or through free function template
+/// canonicalized_input_sequence().
+///
 /// No actual need for this particular ctor has yet been found, but
 /// one might be, someday.
 
@@ -199,7 +205,8 @@ void set_value(ValueInterval& v, std::string const& s)
 ///
 /// As these comments suggest, the ctors that use this function
 /// template are suitable only for certain specialized purposes where
-/// the argument is known to be valid.
+/// the argument is known to be valid, and therefore they are private
+/// and accessible only through canonicalized_input_sequence().
 
 template<typename T>
 void InputSequence::initialize_from_vector(std::vector<T> const& v)
diff --git a/input_sequence.hpp b/input_sequence.hpp
index 19d0801..1fad852 100644
--- a/input_sequence.hpp
+++ b/input_sequence.hpp
@@ -143,6 +143,9 @@ class LMI_SO InputSequence
     :        private lmi::uncopyable <InputSequence>
     ,virtual private obstruct_slicing<InputSequence>
 {
+    template<typename T>
+    friend std::string canonicalized_input_sequence(std::vector<T> const&);
+
     friend class input_sequence_test;
 
   public:
@@ -158,9 +161,6 @@ class LMI_SO InputSequence
         ,std::string const&              a_default_keyword  = std::string()
         );
 
-    explicit InputSequence(std::vector<double> const&);
-    explicit InputSequence(std::vector<std::string> const&);
-
     ~InputSequence();
 
     std::string canonical_form() const;
@@ -173,6 +173,9 @@ class LMI_SO InputSequence
     std::vector<ValueInterval> const& interval_representation() const;
 
   private:
+    explicit InputSequence(std::vector<double> const&);
+    explicit InputSequence(std::vector<std::string> const&);
+
     template<typename T>
     void initialize_from_vector(std::vector<T> const&);
 
@@ -184,5 +187,11 @@ class LMI_SO InputSequence
     std::vector<std::string> keyword_result_;
 };
 
+template<typename T>
+std::string canonicalized_input_sequence(std::vector<T> const& z)
+{
+    return InputSequence(z).canonical_form();
+}
+
 #endif // input_sequence_hpp
 
diff --git a/input_sequence_test.cpp b/input_sequence_test.cpp
index 58cfa05..b6e7ce6 100644
--- a/input_sequence_test.cpp
+++ b/input_sequence_test.cpp
@@ -395,6 +395,7 @@ void input_sequence_test::test()
     std::vector<double> const v{1, 1, 1, 2, 2};
     InputSequence const seq(v);
     BOOST_TEST(v == seq.linear_number_representation());
+    BOOST_TEST_EQUAL("1 3; 2", canonicalized_input_sequence(v));
     BOOST_TEST_EQUAL("1 3; 2", seq.canonical_form());
     BOOST_TEST_EQUAL
         ("1 [0, 3); 2 [3, maturity)"
@@ -407,6 +408,10 @@ void input_sequence_test::test()
     std::vector<std::string> const v{"alpha", "beta", "beta", "gamma", "eta"};
     InputSequence const seq(v);
     BOOST_TEST(v == seq.linear_keyword_representation());
+    BOOST_TEST_EQUAL
+        ("alpha 1; beta 3; gamma 4; eta"
+        ,canonicalized_input_sequence(v)
+        );
     BOOST_TEST_EQUAL("alpha 1; beta 3; gamma 4; eta", seq.canonical_form());
     BOOST_TEST_EQUAL
         ("alpha [0, 1); beta [1, 3); gamma [3, 4); eta [4, maturity)"
@@ -419,6 +424,7 @@ void input_sequence_test::test()
     std::vector<double> const v{3};
     InputSequence const seq(v);
     BOOST_TEST(v == seq.linear_number_representation());
+    BOOST_TEST_EQUAL("3", canonicalized_input_sequence(v));
     BOOST_TEST_EQUAL("3", seq.canonical_form());
     BOOST_TEST_EQUAL("3", seq.mathematical_representation());
     }
@@ -428,6 +434,7 @@ void input_sequence_test::test()
     std::vector<double> const v;
     InputSequence const seq(v);
     BOOST_TEST(v == seq.linear_number_representation());
+    BOOST_TEST_EQUAL("0", canonicalized_input_sequence(v));
     BOOST_TEST_EQUAL("0", seq.canonical_form());
     BOOST_TEST_EQUAL("0", seq.mathematical_representation());
     }



reply via email to

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