lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master c798b11 1/3: Add and test another "query" ove


From: Greg Chicares
Subject: [lmi-commits] [lmi] master c798b11 1/3: Add and test another "query" overload
Date: Tue, 6 Nov 2018 08:48:53 -0500 (EST)

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

    Add and test another "query" overload
    
    See:
      https://lists.nongnu.org/archive/html/lmi/2018-11/msg00013.html
---
 database.hpp   | 27 ++++++++++++++++++++++-----
 input_test.cpp | 13 +++++++++----
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/database.hpp b/database.hpp
index e843242..902d105 100644
--- a/database.hpp
+++ b/database.hpp
@@ -77,6 +77,9 @@ class LMI_SO product_database final
     double Query(e_database_key) const;
 
     template<typename T>
+    T query(e_database_key) const;
+
+    template<typename T>
     void query_into(T&, e_database_key) const;
 
     bool are_equivalent(e_database_key, e_database_key) const;
@@ -97,27 +100,41 @@ class LMI_SO product_database final
     std::shared_ptr<DBDictionary> db_;
 };
 
-/// Query database, using default index; write result into scalar argument.
+/// Query database, using default index; return a scalar.
 ///
-/// Casts result to type T, preserving value by using bourn_cast.
+/// Cast result to type T, preserving value by using bourn_cast.
 ///
 /// Throw if the database entity is not scalar, or if casting fails
 /// (because T is neither enumerative nor arithmetic, or because the
 /// result cannot be represented exactly in type T).
 
 template<typename T>
-void product_database::query_into(T& dst, e_database_key k) const
+T product_database::query(e_database_key k) const
 {
     double d = Query(k, index_);
     if constexpr(std::is_enum_v<T>)
         {
-        dst = static_cast<T>(bourn_cast<std::underlying_type_t<T>>(d));
+        return static_cast<T>(bourn_cast<std::underlying_type_t<T>>(d));
         }
     else
         {
-        dst = bourn_cast<T>(d);
+        return bourn_cast<T>(d);
         }
 }
 
+/// Query database, using default index; write result into scalar argument.
+///
+/// Cast result to type T, preserving value by using bourn_cast.
+///
+/// Throw if the database entity is not scalar, or if casting fails
+/// (because T is neither enumerative nor arithmetic, or because the
+/// result cannot be represented exactly in type T).
+
+template<typename T>
+void product_database::query_into(T& dst, e_database_key k) const
+{
+    dst = query<T>(k);
+}
+
 #endif // database_hpp
 
diff --git a/input_test.cpp b/input_test.cpp
index 33d6c6b..24e09cb 100644
--- a/input_test.cpp
+++ b/input_test.cpp
@@ -36,6 +36,7 @@
 #include "dbnames.hpp"
 #include "global_settings.hpp"
 #include "miscellany.hpp"
+#include "oecumenic_enumerations.hpp"
 #include "path_utility.hpp"             // initialize_filesystem()
 #include "test_tools.hpp"
 #include "timer.hpp"
@@ -155,6 +156,8 @@ void input_test::test_product_database()
     // This value corresponds to no enumerator, but C++ allows that.
     db.query_into(a, DB_MaturityAge);
     BOOST_TEST_EQUAL(100, a);
+    auto const b {db.query<oenum_alb_or_anb>(DB_MaturityAge)};
+    BOOST_TEST_EQUAL(100, b);
 
     // This value is not integral, so bourn_cast rejects it.
     BOOST_TEST_THROW
@@ -166,15 +169,17 @@ void input_test::test_product_database()
     auto f0 = [&db]     {db.initialize("sample");};
     auto f1 = [&db, &v] {db.Query(v, DB_MaturityAge);};
     auto f2 = [&db]     {db.Query(DB_MaturityAge);};
-    auto f3 = [&db, &a] {db.query_into(a, DB_AgeLastOrNearest);};
-    auto f4 = [&db]     {db.entity_from_key(DB_MaturityAge);};
+    auto f3 = [&db]     {db.query<oenum_alb_or_anb>(DB_AgeLastOrNearest);};
+    auto f4 = [&db, &a] {db.query_into(a, DB_AgeLastOrNearest);};
+    auto f5 = [&db]     {db.entity_from_key(DB_MaturityAge);};
     std::cout
         << "\n  Database speed tests..."
         << "\n  initialize()      : " << TimeAnAliquot(f0)
         << "\n  Query(vector)     : " << TimeAnAliquot(f1)
         << "\n  Query(scalar)     : " << TimeAnAliquot(f2)
-        << "\n  query_into(scalar): " << TimeAnAliquot(f3)
-        << "\n  entity_from_key() : " << TimeAnAliquot(f4)
+        << "\n  query<T>(scalar)  : " << TimeAnAliquot(f3)
+        << "\n  query_into(scalar): " << TimeAnAliquot(f4)
+        << "\n  entity_from_key() : " << TimeAnAliquot(f5)
         << '\n'
         ;
 



reply via email to

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