lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 2cf0e24 1/7: Overload query_into() for curren


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 2cf0e24 1/7: Overload query_into() for currency
Date: Thu, 4 Feb 2021 16:44:32 -0500 (EST)

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

    Overload query_into() for currency
    
    Class Loads uses a free-function variant of this; class BasicValues
    does the same sort of thing "by hand", in too many cases.
---
 database.cpp   | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 database.hpp   |  4 ++++
 loads_test.cpp |  1 +
 3 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/database.cpp b/database.cpp
index 91e99b9..4a21079 100644
--- a/database.cpp
+++ b/database.cpp
@@ -23,13 +23,17 @@
 
 #include "database.hpp"
 
+#include "alert.hpp"
 #include "assert_lmi.hpp"
+#include "currency.hpp"
 #include "data_directory.hpp"
 #include "dbdict.hpp"
 #include "dbvalue.hpp"
 #include "lmi.hpp"                      // is_antediluvian_fork()
 #include "oecumenic_enumerations.hpp"   // methuselah
 #include "product_data.hpp"
+#include "round_to.hpp"
+#include "ssize_lmi.hpp"
 #include "yare_input.hpp"
 
 #include <algorithm>                    // min()
@@ -115,13 +119,63 @@ void product_database::query_into
         }
 }
 
-/// Query database, using default index; write result into vector argument.
+/// Query database, using default index; write to vector<double> argument.
 
 void product_database::query_into(e_database_key k, std::vector<double>& dst) 
const
 {
     return query_into(k, dst, index_);
 }
 
+/// Query database, using default index; write to currency& argument.
+///
+/// Throws if conversion from double to currency (nearest cent)
+/// does not preserve value.
+
+void product_database::query_into(e_database_key k, currency& dst) const
+{
+    static round_to<double> const round_to_cents(2, r_to_nearest);
+    double z;
+    query_into(k, z);
+    dst = round_to_cents.c(z);
+    if(dblize(dst) != z)
+        {
+        alarum()
+            << "Database key '" << db_name_from_key(k)
+            << ": value " << z
+            << " not preserved in conversion"
+            << " to " << dst.cents() << " cents."
+            << LMI_FLUSH
+            ;
+        }
+}
+
+/// Query database, using default index; write to vector<currency> argument.
+///
+/// Throws if conversion from double to currency (nearest cent)
+/// does not preserve value for all elements.
+
+void product_database::query_into(e_database_key k, std::vector<currency>& 
dst) const
+{
+    static round_to<double> const round_to_cents(2, r_to_nearest);
+    std::vector<double> z;
+    query_into(k, z);
+    dst = round_to_cents.c(z);
+    for(int j = 0; j < lmi::ssize(z); ++j)
+        {
+        if(dblize(dst[j]) != z[j])
+            {
+            alarum()
+                << "Database key '" << db_name_from_key(k)
+                << "', duration " << j
+                << ": value " << z[j]
+                << " not preserved in conversion"
+                << " to " << dst[j].cents() << " cents."
+                << LMI_FLUSH
+                ;
+            }
+        }
+}
+
 /// Query database; return a scalar.
 ///
 /// Throw if the database entity is not scalar.
diff --git a/database.hpp b/database.hpp
index 236d308..42b76ff 100644
--- a/database.hpp
+++ b/database.hpp
@@ -35,6 +35,7 @@
 #include <type_traits>                  // is_integral_v, underlying_type_t
 #include <vector>
 
+class currency;
 class database_entity;
 class DBDictionary;
 class yare_input;
@@ -72,6 +73,9 @@ class LMI_SO product_database final
         ) const;
     void query_into(e_database_key, std::vector<double>&) const;
 
+    void query_into(e_database_key, currency&) const;
+    void query_into(e_database_key, std::vector<currency>&) const;
+
     double query(e_database_key, database_index const&) const;
 
     template<typename T>
diff --git a/loads_test.cpp b/loads_test.cpp
index 3fa44ed..2e6c2aa 100644
--- a/loads_test.cpp
+++ b/loads_test.cpp
@@ -50,6 +50,7 @@ std::vector<double> BasicValues::GetGuarSpecAmtLoadTable() 
const {return dummy_v
 #include "database.hpp"
 int product_database::length() const {return length_;}
 void product_database::query_into(e_database_key, std::vector<double>& v) 
const {v.resize(length_);}
+void product_database::query_into(e_database_key, std::vector<currency>& v) 
const {v.resize(length_);}
 double product_database::query(e_database_key, database_index const&) const 
{return 0.0;}
 
 #include "premium_tax.hpp"



reply via email to

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