[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 6084765 2/3: Revert "Serve cached file reads
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 6084765 2/3: Revert "Serve cached file reads as non-const [...] shared_ptr" |
Date: |
Tue, 2 Feb 2021 09:48:25 -0500 (EST) |
branch: master
commit 6084765925d2503cfbbae1c289672d94f44fbd98
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Revert "Serve cached file reads as non-const [...] shared_ptr"
This reverts most of commit a202f8754d3b9bf728078203e87c469190753ce4,
which was misbegotten: it removed 'const' just to make some unit tests
work without 'const_cast'. Cf.:
https://lists.nongnu.org/archive/html/lmi/2016-08/msg00006.html
| I think the way I'd do this would be to provide a way to test such
| what-if scenario in product_database itself by allowing to replace
| its "db_" member.
---
cache_file_reads.hpp | 10 +++++-----
database.cpp | 4 ++--
database.hpp | 2 +-
input_test.cpp | 2 +-
premium_tax_test.cpp | 2 +-
5 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/cache_file_reads.hpp b/cache_file_reads.hpp
index 5591730..0229420 100644
--- a/cache_file_reads.hpp
+++ b/cache_file_reads.hpp
@@ -47,11 +47,11 @@ namespace detail
/// For each filename, the cache stores one instance, which is
/// replaced by reloading the file if its write time has changed.
///
-/// Instances are retrieved as shared_ptr<T> so that they remain
+/// Instances are retrieved as shared_ptr<T const> so that they remain
/// valid even when the file changes. The client is responsible for
-/// updating any stale pointers it holds. An earlier version returned
-/// shared_ptr<T const> instead, but legitimate non-const use cases
-/// exist, so managing constness is better left to each client.
+/// updating any stale pointers it holds, if it chooses to refresh the
+/// data; if it doesn't, then it uses older data, which remain valid
+/// as long as it holds a pointer to them.
///
/// Implemented as a simple Meyers singleton, with the expected
/// dead-reference and threading issues.
@@ -60,7 +60,7 @@ template<typename T>
class file_cache
{
public:
- using retrieved_type = std::shared_ptr<T>;
+ using retrieved_type = std::shared_ptr<T const>;
static file_cache<T>& instance()
{
diff --git a/database.cpp b/database.cpp
index 6cecf32..91e99b9 100644
--- a/database.cpp
+++ b/database.cpp
@@ -170,7 +170,7 @@ namespace
///
/// Postcondition: returned pointer is not null; throws otherwise.
-std::shared_ptr<DBDictionary> antediluvian_db()
+std::shared_ptr<DBDictionary const> antediluvian_db()
{
std::shared_ptr<DBDictionary> z(new DBDictionary);
z->InitAntediluvian();
@@ -187,7 +187,7 @@ void product_database::initialize(std::string const&
product_name)
{
if(is_antediluvian_fork())
{
- static std::shared_ptr<DBDictionary> z(antediluvian_db());
+ static std::shared_ptr<DBDictionary const> z(antediluvian_db());
db_ = z;
}
else
diff --git a/database.hpp b/database.hpp
index daca9d8..236d308 100644
--- a/database.hpp
+++ b/database.hpp
@@ -101,7 +101,7 @@ class LMI_SO product_database final
int length_;
int maturity_age_;
- std::shared_ptr<DBDictionary> db_;
+ std::shared_ptr<DBDictionary const> db_;
};
/// Query database, using default index; return a scalar.
diff --git a/input_test.cpp b/input_test.cpp
index 0fe0673..464b30e 100644
--- a/input_test.cpp
+++ b/input_test.cpp
@@ -101,7 +101,7 @@ void input_test::test_product_database()
Input input;
yare_input yi(input);
product_database db(yi);
- DBDictionary& dictionary = *db.db_;
+ DBDictionary& dictionary = const_cast<DBDictionary&>(*db.db_);
std::vector<double> v;
std::vector<double> w;
diff --git a/premium_tax_test.cpp b/premium_tax_test.cpp
index 442dacf..288fd1f 100644
--- a/premium_tax_test.cpp
+++ b/premium_tax_test.cpp
@@ -99,7 +99,7 @@ void premium_tax_test::test_rates()
// A uniform but nonzero load would elicit a runtime error,
// because the tiered load is not zero.
{
- DBDictionary& dictionary = *db.db_;
+ DBDictionary& dictionary = const_cast<DBDictionary&>(*db.db_);
database_entity const original = dictionary.datum("PremTaxLoad");
database_entity const scalar(DB_PremTaxLoad, 0.0000);