Provides functionality for currency conversion operations.
More...
#include <CurrencyConverter.hpp>
|
| | CurrencyConverter (CurrencySource &source) |
| | Full initialisation constructor.
|
| ConversionMap | fetchAll (const std::set< pecunia::Currency > ¤cies) const |
| | Retrieves conversion rates for a set of currencies.
|
| pecunia::FloatingPoint | lookUp (const pecunia::Currency &from, const pecunia::Currency &to) const |
| | Retrieves the conversion rate for the specified pair of currencies.
|
|
| static const ConversionMap & | cache () noexcept |
| | Retrieves the collection of available currency conversion mappings.
|
| static void | initialiseCache (ConversionMap &&conversions) |
| | Initializes the currency conversion cache used in the converter.
|
| static void | updateCache (const ConversionMap &conversions) |
| | Updates the currency conversion cache with new mappings.
|
Provides functionality for currency conversion operations.
It is designed to facilitate the conversion of monetary values between different currencies using predefined or dynamically fetched conversion rates. It supports both static configuration and runtime retrieval of conversion mappings.
◆ CurrencyConverter()
| drn::conversion::CurrencyConverter::CurrencyConverter |
( |
CurrencySource & | source | ) |
|
|
explicit |
Full initialisation constructor.
It initializes the instance with a source of currency data for use in conversion calculations. The source provides the necessary exchange rates, either preloaded or fetched at runtime. It can then be used to perform lookups and fetch conversions for specified currencies.
- Parameters
-
| source | The source of currency data, used to fetch exchange rates during conversion operations. |
◆ cache()
| const ConversionMap & drn::conversion::CurrencyConverter::cache |
( |
| ) |
|
|
staticnodiscardnoexcept |
Retrieves the collection of available currency conversion mappings.
This method provides access to the cache of conversion mappings, which associate currency pairs with their respective conversion rates. These mappings are essential for accurate currency conversion operations throughout the system.
- Returns
- The collection of available conversion mappings.
◆ fetchAll()
| ConversionMap drn::conversion::CurrencyConverter::fetchAll |
( |
const std::set< pecunia::Currency > & | currencies | ) |
const |
|
nodiscard |
Retrieves conversion rates for a set of currencies.
It fetches all potential currency conversion rates for a given set of currencies, dynamically sourcing data for pairs that are not already available in the cache. It uses an external data source to acquire missing conversion rates.
- Parameters
-
| currencies | A set of currencies for which to fetch and update conversion rates. Currency pairs will be generated from all possible pair combinations within this set, including pairs where the source and target are the same. |
- Returns
- A conversion map containing the updated conversion rates for all currency pairs created from the provided set of currencies, including rates for self-conversions.
◆ initialiseCache()
| void drn::conversion::CurrencyConverter::initialiseCache |
( |
ConversionMap && | conversions | ) |
|
|
static |
Initializes the currency conversion cache used in the converter.
It is used to load or update the internal mapping of currency conversions, enabling the system to perform accurate and efficient conversion operations.
- Parameters
-
| conversions | A mapping of currency pairs to their respective conversion rates to be used in future conversions. |
- Postcondition
- Replaces the current currency conversion cache with the provided one. The updated mapping will serve as the basis for any subsequent currency conversion operations.
◆ lookUp()
| pecunia::FloatingPoint drn::conversion::CurrencyConverter::lookUp |
( |
const pecunia::Currency & | from, |
|
|
const pecunia::Currency & | to ) const |
|
nodiscard |
Retrieves the conversion rate for the specified pair of currencies.
Ensures that a conversion rate exists for the provided currency pair. If a rate is not already available in the cache, it attempts to fetch it using a data source and updates the cache. This ensures that subsequent lookups for the same pair do not require additional fetching.
- Parameters
-
| from | The source currency for the conversion. |
| to | The target currency for the conversion. |
- Postcondition
- If the conversion does not exist in the cache, it is fetched and stored for future lookups.
- Returns
- The conversion rate between the two specified currencies.
◆ updateCache()
| void drn::conversion::CurrencyConverter::updateCache |
( |
const ConversionMap & | conversions | ) |
|
|
static |
Updates the currency conversion cache with new mappings.
- Parameters
-
| conversions | A mapping of source currencies to a collection of target currencies and their corresponding conversion rates. These mappings will be added to the current cache. |
- Postcondition
- The internal cache of currency conversions is updated to reflect the new mappings. Existing mappings for any currency pairs present in the input data are overwritten with the provided conversion rates.
-
The rate fetch timestamp is updated.