Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
Recognition::Utility::Percentage Class Reference

A type-safe percentage value in the range [0, 100]. More...

#include <percentage.hpp>

Public Member Functions

 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.

Private Attributes

int m_percentage

Detailed Description

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

// Direct percentage
Percentage progress{75};
std::println("Progress: {}", progress.ToString()); // "75%"
// From steps
Percentage ratio{7, 10}; // 70%
// Update value
progress.Set(100);
std::println("Complete: {}", progress.ToString()); // "100%"
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

Constructor & Destructor Documentation

◆ 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]percentageValue in range [0, 100] (default: 0). Values outside this range are clamped.
Percentage p1; // 0%
Percentage p2{50}; // 50%
Percentage p3{200}; // 100% (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_stepsNumber of steps completed.
[in]total_stepsTotal number of steps (0 is treated as 1).
Percentage p1{50, 100}; // 50%
Percentage p2{1, 4}; // 25%
Percentage p3{10, 0}; // 0% (safe division by zero)
Since
10.0.0

Member Function Documentation

◆ Get()

int Recognition::Utility::Percentage::Get ( ) const
inlinenodiscardnoexcept

Get the percentage value.

Returns
The percentage value in the range [0, 100].
Percentage p{75};
int value = p.Get(); // 75
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]percentageNew value (clamped to [0, 100]).
p.Set(50); // Now 50%
p.Set(-10); // Clamped to 0%
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.
Percentage p{42};
std::string s = p.ToString(); // "42%"
Since
10.0.0

Member Data Documentation

◆ 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: