Overview
TheUnitConverter class is the main implementation of unit conversion functionality in Windows Calculator. It manages conversion categories, units, conversion ratios, and user input handling. The class supports both standard unit conversions (length, weight, temperature, etc.) and currency conversions through specialized data loaders.
Class Definition
~/workspace/source/src/CalcManager/UnitConverter.h
Constructors
UnitConverter(dataLoader)
Constructs aUnitConverter with a single data loader for standard unit conversions.
Data loader instance for reading category/unit names and conversion data
UnitConverter(dataLoader, currencyDataLoader)
Constructs aUnitConverter with separate data loaders for standard units and currencies.
Data loader for standard unit conversion data
Specialized data loader for currency conversion data (can be
nullptr)Core Methods
Initialize
Initializes the converter by loading data from the configured data loaders.UnitConverter.cpp:72
GetCategories
Returns the list of available conversion categories.Category structs representing all available conversion categories (e.g., Length, Weight, Temperature, Currency)
See UnitConverter.cpp:89
SetCurrentCategory
Sets the active conversion category and returns initialization data for the category.The category to set as current
CategorySelectionInitializer (tuple containing: vector of units, from unit, to unit)
See UnitConverter.cpp:100
GetCurrentCategory
Retrieves the currently active conversion category.Category struct
See UnitConverter.cpp:134
SetCurrentUnitTypes
Sets the source and target units for conversion and triggers a calculation.The unit being converted from (source unit)
The unit being converted to (target unit)
UnitConverter.cpp:145
Calculate
Performs the conversion calculation based on current input value and selected units.UnitConverter.cpp:867
SwitchActive
Swaps the source and target units, switching which field the user is editing.The value from the newly activated field (handles cases where the UI may have trimmed digits)
UnitConverter.cpp:176
IsSwitchedActive
Checks whether the user has switched to editing the target field.true if the user is editing the target field, false otherwise
See UnitConverter.cpp:199
SendCommand
Processes input commands from the user interface (number keys, decimal, backspace, etc.).The command enum representing the user action (e.g., Zero, One, Decimal, Backspace, Negate, Clear)
UnitConverter.cpp:393
RefreshCurrencyRatios
Asynchronously refreshes currency conversion rates from the web.UnitConverter.cpp:558
Persistence Methods
SaveUserPreferences
Serializes the current converter state to a string for persistence.UnitConverter.cpp:314
RestoreUserPreferences
Deserializes and restores the converter state from a saved string.Serialized state string from a previous
SaveUserPreferences() callUnitConverter.cpp:278
Callback Methods
SetViewModelCallback
Registers a callback interface for display updates.Callback interface that receives conversion results and suggested values
UnitConverter.cpp:538
SetViewModelCurrencyCallback
Registers a callback interface for currency-specific updates.Callback interface for currency symbols, ratios, timestamps, and network status
UnitConverter.cpp:547
Utility Methods
StringToVector
Parses a delimited string into a vector of strings.The string to parse
The delimiter to split on
Whether to include any remaining text after the last delimiter
UnitConverter.cpp:214
Quote
Escapes special delimiter characters in a string for serialization.The string to escape
UnitConverter.cpp:330
Unquote
Unescapes a serialized string, restoring original delimiter characters.The escaped string to restore
UnitConverter.cpp:354
ResetCategoriesAndRatios
Reloads all categories and conversion ratios from the data loaders.UnitConverter.cpp:724
Data Structures
Unit Struct
Represents a single unit of measurement within a category.Unique identifier for the unit
Display name of the unit (e.g., “Meters”, “Pounds”)
Accessibility-friendly name for screen readers
Short abbreviation (e.g., “m”, “lb”)
Whether this unit is set as the source (from) unit
Whether this unit is set as the target (to) unit
Whether this is a whimsical/novelty unit (e.g., “Football fields”)
UnitConverter.h:16
EMPTY_UNIT
A sentinel value representing an uninitialized or invalid unit.UnitConverter.h:80
Category Struct
Represents a conversion category (e.g., Length, Weight, Currency).Unique identifier for the category
Display name of the category (e.g., “Length”, “Currency”)
Whether negative values are allowed in this category
UnitConverter.h:82
ConversionData Struct
Defines the conversion formula between two units.Multiplication factor for the conversion
Additive offset for the conversion (used for temperature, etc.)
If
If
true, apply offset before ratio: (value + offset) * ratioIf
false, apply ratio before offset: (value * ratio) + offsetUnitConverter.h:126
Constants
MAXIMUMDIGITSALLOWED = 15- Maximum significant digits allowed in inputOPTIMALDIGITSALLOWED = 7- Optimal number of significant digits for displayOPTIMALDECIMALALLOWED = 1e-6- Threshold for optimal decimal precisionMINIMUMDECIMALALLOWED = 1e-14- Minimum decimal value before switching to scientific notation
UnitConverter.cpp:21
Type Aliases
UnitConverter.h:159
Usage Example
Related
- Currency Converter - Currency-specific conversion details
- IConverterDataLoader Interface - Data loader interface for categories and units
- ICurrencyConverterDataLoader Interface - Currency-specific data loader interface