Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
percentage.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2026 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
30
31#ifndef PERCENTAGE_HPP
32#define PERCENTAGE_HPP
33
34#include <algorithm>
35#include <cstdint>
36#include <string>
37
38namespace Recognition {
39namespace Utility {
44
82public:
99 explicit Percentage(int percentage = 0) noexcept
100 : m_percentage{std::clamp(percentage, 0, 100)}
101 { }
102
121 std::uint64_t completed_steps,
122 std::uint64_t total_steps
123 ) noexcept
124 : m_percentage{std::clamp(
125 static_cast<int>(
126 (completed_steps * 100)
127 / std::max<std::uint64_t>(total_steps, 1u)
128 ),
129 0,
130 100
131 )}
132 { }
133
146 [[nodiscard]] int Get() const noexcept
147 {
148 return m_percentage;
149 }
150
166 void Set(int percentage) noexcept
167 {
168 m_percentage = std::clamp(percentage, 0, 100);
169 }
170
183 [[nodiscard]] std::string ToString() const noexcept
184 {
185 return std::to_string(m_percentage) + "%";
186 }
187
188private:
190};
191
193} /* namespace Utility */
194} /* namespace Recognition */
195
196#endif /* PERCENTAGE_HPP */
Percentage(std::uint64_t completed_steps, std::uint64_t total_steps) noexcept
Construct Percentage from completed and total steps.
Definition percentage.hpp:120
int m_percentage
Definition percentage.hpp:189
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
int Get() const noexcept
Get the percentage value.
Definition percentage.hpp:146
void Set(int percentage) noexcept
Set a new percentage value.
Definition percentage.hpp:166
Utility components for the libmagicxx library.
Definition percentage.hpp:39
Root namespace for the libmagicxx library.