Libmagicxx v9.1.1
A C++ wrapper library over the Magic Number Recognition Library.
Loading...
Searching...
No Matches
percentage.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2025 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
4#ifndef PERCENTAGE_HPP
5#define PERCENTAGE_HPP
6
7#include <algorithm>
8#include <cstdint>
9#include <string>
10
11namespace recognition {
12namespace utility {
19public:
25 explicit percentage(int percentage = 0) noexcept
26 : m_percentage{std::clamp(percentage, 0, 100)}
27 { }
28
36 std::uint64_t completed_steps,
37 std::uint64_t total_steps
38 ) noexcept
39 : m_percentage{std::clamp(
40 static_cast<int>(
41 (completed_steps * 100)
42 / std::max<std::uint64_t>(total_steps, 1u)
43 ),
44 0,
45 100
46 )}
47 { }
48
52 [[nodiscard]] int get() const noexcept
53 {
54 return m_percentage;
55 }
56
62 void set(int percentage) noexcept
63 {
64 m_percentage = std::clamp(percentage, 0, 100);
65 }
66
72 [[nodiscard]] std::string to_string() const noexcept
73 {
74 return std::to_string(m_percentage) + "%";
75 }
76
77private:
79};
80} /* namespace utility */
81} /* namespace recognition */
82
83#endif /* PERCENTAGE_HPP */
percentage(int percentage=0) noexcept
Construct percentage with a given value.
Definition percentage.hpp:25
void set(int percentage) noexcept
Set the percentage value.
Definition percentage.hpp:62
percentage(std::uint64_t completed_steps, std::uint64_t total_steps) noexcept
Construct percentage from completed and total steps.
Definition percentage.hpp:35
std::string to_string() const noexcept
Convert percentage to string.
Definition percentage.hpp:72
int get() const noexcept
Definition percentage.hpp:52
int m_percentage
Definition percentage.hpp:78
Definition percentage.hpp:12
Definition magic.hpp:19