A type-safe percentage value in the range [0, 100].
More...
#include <percentage.hpp>
|
| | Percentage (int percentage=0) noexcept |
| | Construct Percentage with a direct value.
|
| | Percentage (std::uint64_t completed_steps, std::uint64_t total_steps) noexcept |
| | Construct Percentage from completed and total steps.
|
| int | Get () const noexcept |
| | Get the percentage value.
|
| void | Set (int percentage) noexcept |
| | Set a new percentage value.
|
| std::string | ToString () const noexcept |
| | Convert percentage to a human-readable string.
|
A type-safe percentage value in the range [0, 100].
Percentage provides a strongly-typed representation of percentage values with automatic clamping to ensure values always remain within valid bounds. This class is particularly useful for progress tracking scenarios.
Key Features
- Automatic Clamping: Values are automatically clamped to [0, 100]
- Step-Based Construction: Calculate percentage from completed/total steps
- String Formatting: Convert to human-readable "X%" format
- noexcept Guarantee: All operations are noexcept
Example
std::println(
"Progress: {}", progress.
ToString());
std::println(
"Complete: {}", progress.
ToString());
A type-safe percentage value in the range [0, 100].
Definition percentage.hpp:81
Percentage(int percentage=0) noexcept
Construct Percentage with a direct value.
Definition percentage.hpp:99
std::string ToString() const noexcept
Convert percentage to a human-readable string.
Definition percentage.hpp:183
void Set(int percentage) noexcept
Set a new percentage value.
Definition percentage.hpp:166
- See also
- ProgressTracker
- Since
- 10.0.0
◆ Percentage() [1/2]
| Recognition::Utility::Percentage::Percentage |
( |
int | percentage = 0 | ) |
|
|
inlineexplicitnoexcept |
Construct Percentage with a direct value.
The value is automatically clamped to the range [0, 100].
- Parameters
-
| [in] | percentage | Value in range [0, 100] (default: 0). Values outside this range are clamped. |
- Since
- 10.0.0
◆ Percentage() [2/2]
| Recognition::Utility::Percentage::Percentage |
( |
std::uint64_t | completed_steps, |
|
|
std::uint64_t | total_steps ) |
|
inlinenoexcept |
Construct Percentage from completed and total steps.
Calculates the percentage as (completed_steps * 100) / total_steps, with protection against division by zero (total_steps of 0 results in 0%).
- Parameters
-
| [in] | completed_steps | Number of steps completed. |
| [in] | total_steps | Total number of steps (0 is treated as 1). |
- Since
- 10.0.0
◆ Get()
| int Recognition::Utility::Percentage::Get |
( |
| ) |
const |
|
inlinenodiscardnoexcept |
Get the percentage value.
- Returns
- The percentage value in the range [0, 100].
int Get() const noexcept
Get the percentage value.
Definition percentage.hpp:146
- Since
- 10.0.0
◆ Set()
| void Recognition::Utility::Percentage::Set |
( |
int | percentage | ) |
|
|
inlinenoexcept |
Set a new percentage value.
The value is automatically clamped to the range [0, 100].
- Parameters
-
| [in] | percentage | New value (clamped to [0, 100]). |
- Since
- 10.0.0
◆ ToString()
| std::string Recognition::Utility::Percentage::ToString |
( |
| ) |
const |
|
inlinenodiscardnoexcept |
Convert percentage to a human-readable string.
- Returns
- String in the format "X%" where X is the percentage value.
- Since
- 10.0.0
◆ m_percentage
| int Recognition::Utility::Percentage::m_percentage |
|
private |
The percentage value, always in range [0, 100].
The documentation for this class was generated from the following file: