[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/005 2e73521 5/9: Begin to conditionalize use
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/005 2e73521 5/9: Begin to conditionalize use of currency class |
Date: |
Wed, 20 Jan 2021 00:51:32 -0500 (EST) |
branch: valyuta/005
commit 2e73521e8c98b341303f85999c8726571a2ae8ed
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Begin to conditionalize use of currency class
Motivation: If class currency can conditionally be switched with
using currency = double;
then a piecemeal merge from valyuta/v5 into master becomes tractable.
And a piecemeal merge--TU by TU, and even function by function--is most
devoutly to be desired, because so many details have been changed--some
being clear improvements, while others perhaps are not. A "big bang
integration" would simply trade one set of woes for another. The plan
instead is to make only improvements, integrating and testing them in
manageable chunks--and not one retrograde step.
Currency could now be switched to a typedef except for the pervasive
currency::d(), and that funky raw_cents tag (originally, a cumbersome
constructor from raw cents used that tag to avoid ambiguating converting
ctors, but there are none left to ambiguate). Impending answer to those
issues: free functions.
---
account_value.hpp | 2 ++
currency.hpp | 23 +++++++++++++++++++----
ihs_avdebug.cpp | 2 ++
round_to.hpp | 22 +++++++++++++++++-----
4 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/account_value.hpp b/account_value.hpp
index 69605f0..60868d4 100644
--- a/account_value.hpp
+++ b/account_value.hpp
@@ -304,7 +304,9 @@ class LMI_SO AccountValue final
void SetMonthlyDetail(int enumerator, std::string const&);
void SetMonthlyDetail(int enumerator, double);
+#if defined USE_CURRENCY_CLASS
void SetMonthlyDetail(int enumerator, currency);
+#endif // defined USE_CURRENCY_CLASS
void DebugPrintInit();
void DebugEndBasis();
diff --git a/currency.hpp b/currency.hpp
index bdd5c4b..fb30ed0 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -29,8 +29,21 @@
#include <ostream>
#include <vector>
+#define USE_CURRENCY_CLASS
+
+#if !defined USE_CURRENCY_CLASS
+
+using currency = double;
+
+inline std::vector<double> doubleize(std::vector<currency> const& z)
+{
+ return z;
+}
+
+#else // defined USE_CURRENCY_CLASS
+
// Retain this--it's used elsewhere.
-#define CURRENCY_UNIT_IS_CENTS
+# define CURRENCY_UNIT_IS_CENTS
class raw_cents {}; // Tag class.
@@ -39,13 +52,13 @@ class LMI_SO currency
friend class currency_test;
template<typename> friend class round_to;
friend class round_to_test;
-#if defined CURRENCY_UNIT_IS_CENTS
+# if defined CURRENCY_UNIT_IS_CENTS
static constexpr int cents_digits = 2;
static constexpr double cents_per_dollar = 100.0;
-#else // !defined CURRENCY_UNIT_IS_CENTS
+# else // !defined CURRENCY_UNIT_IS_CENTS
static constexpr int cents_digits = 0;
static constexpr double cents_per_dollar = 1.0;
-#endif // !defined CURRENCY_UNIT_IS_CENTS
+# endif // !defined CURRENCY_UNIT_IS_CENTS
public:
using data_type = double;
@@ -114,6 +127,8 @@ inline std::vector<double> doubleize(std::vector<currency>
const& z)
return r;
}
+# endif // defined USE_CURRENCY_CLASS
+
/// Zero cents--akin to a user-defined literal.
///
/// UDLs seem less convenient because the obvious "0_c" is likely to
diff --git a/ihs_avdebug.cpp b/ihs_avdebug.cpp
index de2a798..3bec972 100644
--- a/ihs_avdebug.cpp
+++ b/ihs_avdebug.cpp
@@ -257,11 +257,13 @@ inline void AccountValue::SetMonthlyDetail(int
enumerator, double d)
}
//============================================================================
+#if defined USE_CURRENCY_CLASS
inline void AccountValue::SetMonthlyDetail(int enumerator, currency c)
{
// CURRENCY !! Probably value_cast<>(currency) should be specialized.
DebugRecord[enumerator] = value_cast<std::string>(c.d());
}
+#endif // defined USE_CURRENCY_CLASS
//============================================================================
void AccountValue::SetDebugFilename(std::string const& s)
diff --git a/round_to.hpp b/round_to.hpp
index 402e635..ecad25f 100644
--- a/round_to.hpp
+++ b/round_to.hpp
@@ -270,8 +270,10 @@ class round_to
currency c(RealType) const;
std::vector<currency> c(std::vector<RealType> const&) const;
+#if defined USE_CURRENCY_CLASS
currency c(currency) const;
std::vector<currency> c(std::vector<currency> const&) const;
+#endif // defined USE_CURRENCY_CLASS
int decimals() const;
rounding_style style() const;
@@ -320,7 +322,11 @@ round_to<RealType>::round_to(int decimals, rounding_style
a_style)
,style_ {a_style}
,scale_fwd_ {detail::perform_pow(max_prec_real(10.0), decimals_)}
,scale_back_ {max_prec_real(1.0) / scale_fwd_}
+#if defined USE_CURRENCY_CLASS
,decimals_cents_ {decimals - currency::cents_digits}
+#else // !defined USE_CURRENCY_CLASS
+ ,decimals_cents_ {decimals - 0}
+#endif // ! defined USE_CURRENCY_CLASS
,scale_fwd_cents_ {detail::perform_pow(max_prec_real(10.0),
decimals_cents_)}
,scale_back_cents_ {max_prec_real(1.0) / scale_fwd_cents_}
,rounding_function_ {select_rounding_function(a_style)}
@@ -389,8 +395,12 @@ inline currency round_to<RealType>::c(RealType r) const
RealType const z = static_cast<RealType>
(rounding_function_(static_cast<RealType>(r * scale_fwd_)) *
scale_back_cents_
);
- // !! static_cast: possible range error
+#if defined USE_CURRENCY_CLASS
+ // CURRENCY !! static_cast: possible range error
return currency(static_cast<currency::data_type>(z), raw_cents {});
+#else // !defined USE_CURRENCY_CLASS
+ return currency(z);
+#endif // ! defined USE_CURRENCY_CLASS
}
template<typename RealType>
@@ -403,15 +413,16 @@ inline std::vector<currency> round_to<RealType>::c
return z;
}
-// !! need unit tests
+#if defined USE_CURRENCY_CLASS
+// CURRENCY !! need unit tests
template<typename RealType>
inline currency round_to<RealType>::c(currency z) const
{
-#if defined CURRENCY_UNIT_IS_CENTS
+# if defined CURRENCY_UNIT_IS_CENTS
return (decimals_ < currency::cents_digits) ? c(z.d()) : z;
-#else // !defined CURRENCY_UNIT_IS_CENTS
+# else // !defined CURRENCY_UNIT_IS_CENTS
return c(z.d());
-#endif // !defined CURRENCY_UNIT_IS_CENTS
+# endif // !defined CURRENCY_UNIT_IS_CENTS
}
template<typename RealType>
@@ -423,6 +434,7 @@ inline std::vector<currency> round_to<RealType>::c
for(auto const& i : v) {z.push_back(c(i));}
return z;
}
+#endif // defined USE_CURRENCY_CLASS
template<typename RealType>
int round_to<RealType>::decimals() const
- [lmi-commits] [lmi] valyuta/005 updated (4401725 -> ca84095), Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 4483b09 1/9: Avoid passing 'currency' by const reference, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 1dd82ec 3/9: Rationalize argument names, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 cb722b2 4/9: Replace a complaint with a solution, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 2e73521 5/9: Begin to conditionalize use of currency class,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/005 b835c6d 2/9: Partially revert the last commit, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 afa4967 8/9: Make lmi build without currency class, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 ca84095 9/9: Record speed measurements, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 df368ca 6/9: Replace currency::d() with d(currency) almost everywhere, Greg Chicares, 2021/01/20
- [lmi-commits] [lmi] valyuta/005 75f8adf 7/9: Add and use free function from_cents(), Greg Chicares, 2021/01/20