[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 8862ffc 7/9: Improve const correctness
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 8862ffc 7/9: Improve const correctness |
Date: |
Wed, 12 Jun 2019 12:53:00 -0400 (EDT) |
branch: master
commit 8862ffce33cd76fb09a5a5850d058a3807be9a17
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Improve const correctness
Replaced this idiom:
https://isocpp.org/wiki/faq/ctors#named-parameter-idiom
with a const variant that suffices for all real-world use cases.
---
dbindex.hpp | 35 +++++++++++++++++++----------------
input_test.cpp | 28 +++++++++++++++++++---------
2 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/dbindex.hpp b/dbindex.hpp
index 7f25bb8..fde8c39 100644
--- a/dbindex.hpp
+++ b/dbindex.hpp
@@ -77,14 +77,9 @@ class database_index
,mcenum_uw_basis uw_basis
,mcenum_state state
)
- :idx_(number_of_indices)
+ :idx_ {gender, uw_class, smoking, issue_age, uw_basis, state}
{
- idx_[0] = gender ;
- idx_[1] = uw_class ;
- idx_[2] = smoking ;
- idx_[3] = issue_age;
- idx_[4] = uw_basis ;
- idx_[5] = state ;
+ check_issue_age();
}
std::vector<int> const& index_vector() const {return idx_;}
@@ -96,18 +91,26 @@ class database_index
mcenum_uw_basis uw_basis () const {return
static_cast<mcenum_uw_basis>(idx_[4]);}
mcenum_state state () const {return static_cast<mcenum_state
>(idx_[5]);}
- database_index& gender (mcenum_gender z) {idx_[0] = z; return *this;}
- database_index& uw_class (mcenum_class z) {idx_[1] = z; return *this;}
- database_index& smoking (mcenum_smoking z) {idx_[2] = z; return *this;}
- database_index& issue_age(int z) {check_issue_age(z);
- idx_[3] = z; return *this;}
- database_index& uw_basis (mcenum_uw_basis z) {idx_[4] = z; return *this;}
- database_index& state (mcenum_state z) {idx_[5] = z; return *this;}
+ database_index gender (mcenum_gender z) const {auto i = idx_; i[0] =
z; return {i};}
+ database_index uw_class (mcenum_class z) const {auto i = idx_; i[1] =
z; return {i};}
+ database_index smoking (mcenum_smoking z) const {auto i = idx_; i[2] =
z; return {i};}
+ database_index issue_age(int z) const {auto i = idx_; i[3] =
z; return {i};}
+ database_index uw_basis (mcenum_uw_basis z) const {auto i = idx_; i[4] =
z; return {i};}
+ database_index state (mcenum_state z) const {auto i = idx_; i[5] =
z; return {i};}
private:
- void check_issue_age(int z) {LMI_ASSERT(0 <= z && z <
e_max_dim_issue_age);}
+ database_index(std::vector<int> idx)
+ :idx_ {idx}
+ {
+ check_issue_age();
+ }
+
+ void check_issue_age()
+ {
+ LMI_ASSERT(0 <= issue_age() && issue_age() < e_max_dim_issue_age);
+ }
- std::vector<int> idx_;
+ std::vector<int> const idx_;
};
#endif // dbindex_hpp
diff --git a/input_test.cpp b/input_test.cpp
index 02242c3..2debd45 100644
--- a/input_test.cpp
+++ b/input_test.cpp
@@ -226,36 +226,46 @@ void input_test::test_product_database()
// Test presumptive issue-age bounds in class database_index.
BOOST_TEST_THROW
+ (database_index({mce_male, mce_rated, mce_smoker, 100, mce_medical,
mce_s_XX})
+ ,std::runtime_error
+ ,"Assertion '0 <= issue_age() && issue_age() < e_max_dim_issue_age'
failed."
+ );
+ BOOST_TEST_THROW
+ (database_index({mce_male, mce_rated, mce_smoker, -1, mce_medical,
mce_s_XX})
+ ,std::runtime_error
+ ,"Assertion '0 <= issue_age() && issue_age() < e_max_dim_issue_age'
failed."
+ );
+ BOOST_TEST_THROW
(index.issue_age(100)
,std::runtime_error
- ,"Assertion '0 <= z && z < e_max_dim_issue_age' failed."
+ ,"Assertion '0 <= issue_age() && issue_age() < e_max_dim_issue_age'
failed."
);
BOOST_TEST_THROW
(index.issue_age(-1)
,std::runtime_error
- ,"Assertion '0 <= z && z < e_max_dim_issue_age' failed."
+ ,"Assertion '0 <= issue_age() && issue_age() < e_max_dim_issue_age'
failed."
);
- index.issue_age(99);
- db.query_into(DB_SnflQ, v, index);
+ database_index idx99 = index.issue_age(99);
+ db.query_into(DB_SnflQ, v, idx99);
BOOST_TEST_EQUAL( 1, v.size());
// Force the product to mature at 98.
db.maturity_age_ = 98;
- index.issue_age(98);
- db.query(DB_MaturityAge, index); // Accepted because maturity age is
scalar.
+ database_index idx98 = index.issue_age(98);
+ db.query(DB_MaturityAge, idx98); // Accepted because maturity age is
scalar.
BOOST_TEST_THROW
- (db.query_into(DB_SnflQ, v, index)
+ (db.query_into(DB_SnflQ, v, idx98)
,std::runtime_error
,"Assertion '0 < local_length && local_length <= methuselah' failed."
);
index.issue_age(97);
- db.query_into(DB_SnflQ, v, index);
+ db.query_into(DB_SnflQ, v, index.issue_age(97));
BOOST_TEST_EQUAL( 1, v.size());
index.issue_age(0);
- db.query_into(DB_SnflQ, v, index);
+ db.query_into(DB_SnflQ, v, index.issue_age(0));
BOOST_TEST_EQUAL(98, v.size());
}
- [lmi-commits] [lmi] master updated (a9bcf09 -> d30e9b1), Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master b6c5d13 6/9: Improve const correctness, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master 1f1adcf 8/9: Prefer std::array to std::vector when size is fixed, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master 9ffdf78 4/9: Add const accessors to class database_index, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master 7b5da91 5/9: Use database_index const accessors, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master fad3d61 1/9: Use forward declarations to improve physical design, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master 89c8cc5 2/9: Use forward declarations to improve physical design, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master d30e9b1 9/9: Make {{ContractNameCap}} available for all formats, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master a8298c9 3/9: Improve list of handy commands, Greg Chicares, 2019/06/12
- [lmi-commits] [lmi] master 8862ffc 7/9: Improve const correctness,
Greg Chicares <=