Libmagicxx v9.0.2
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 {
12
13namespace utility {
14
21public:
27 explicit percentage(int percentage = 0) noexcept
28 : m_percentage{std::clamp(percentage, 0, 100)}
29 { }
30
38 std::uint64_t completed_steps,
39 std::uint64_t total_steps
40 ) noexcept
41 : m_percentage{std::clamp(
42 static_cast<int>(
43 (completed_steps * 100)
44 / std::max<std::uint64_t>(total_steps, 1u)
45 ),
46 0,
47 100
48 )}
49 { }
50
54 [[nodiscard]] int get() const noexcept
55 {
56 return m_percentage;
57 }
58
64 void set(int percentage) noexcept
65 {
66 m_percentage = std::clamp(percentage, 0, 100);
67 }
68
74 [[nodiscard]] std::string to_string() const noexcept
75 {
76 return std::to_string(m_percentage) + "%";
77 }
78
79private:
81};
82
83} /* namespace utility */
84
85} /* namespace recognition */
86
87#endif /* PERCENTAGE_HPP */
Represents a value in the range [0, 100].
Definition percentage.hpp:20
percentage(int percentage=0) noexcept
Construct percentage with a given value.
Definition percentage.hpp:27
void set(int percentage) noexcept
Set the percentage value.
Definition percentage.hpp:64
percentage(std::uint64_t completed_steps, std::uint64_t total_steps) noexcept
Construct percentage from completed and total steps.
Definition percentage.hpp:37
std::string to_string() const noexcept
Convert percentage to string.
Definition percentage.hpp:74
int get() const noexcept
Definition percentage.hpp:54
int m_percentage
Definition percentage.hpp:80
Definition magic.hpp:19