HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
misc.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
10#pragma once
11
12#include "../macros.hpp"
13#include <utility>
14#include <cstddef>
15#include <string>
16#include <chrono>
17#include <atomic>
18#include <string_view>
19
20hi_export_module(hikogui.utility.misc);
21
22hi_warning_push();
23// C26472: Don't use static_cast for arithmetic conversions, Use brace initialization, gsl::narrow_cast or gsl::narrow (type.1).
24// We do not have access to narrow_cast in this file.
25hi_warning_ignore_msvc(26472);
26
27hi_export namespace hi {
28inline namespace v1 {
29
33
34constexpr std::size_t operator""_uz(unsigned long long lhs) noexcept
35{
36 return static_cast<std::size_t>(lhs);
37}
38
39constexpr std::size_t operator""_zu(unsigned long long lhs) noexcept
40{
41 return static_cast<std::size_t>(lhs);
42}
43
44constexpr std::ptrdiff_t operator""_z(unsigned long long lhs) noexcept
45{
46 return static_cast<std::ptrdiff_t>(lhs);
47}
48
52template<typename T, typename U>
53[[nodiscard]] bool compare_store(T& lhs, U&& rhs) noexcept
54{
55 if (lhs != rhs) {
56 lhs = std::forward<U>(rhs);
57 return true;
58 } else {
59 return false;
60 }
61}
62
68template<typename T, typename U>
69[[nodiscard]] bool compare_store(std::atomic<T>& lhs, U&& rhs) noexcept
70{
71 return lhs.exchange(rhs, std::memory_order::relaxed) != rhs;
72}
73
76struct unusable_t {
77 unusable_t() = delete;
78 ~unusable_t() = delete;
79 unusable_t(unusable_t const&) = delete;
80 unusable_t(unusable_t&&) = delete;
81 unusable_t& operator=(unusable_t const&) = delete;
82 unusable_t& operator=(unusable_t&&) = delete;
83};
84
85template<class T, class U>
86[[nodiscard]] constexpr auto&& forward_like(U&& x) noexcept
87{
88 constexpr bool is_adding_const = std::is_const_v<std::remove_reference_t<T>>;
89 if constexpr (std::is_lvalue_reference_v<T&&>) {
90 if constexpr (is_adding_const) {
91 return std::as_const(x);
92 } else {
93 return static_cast<U&>(x);
94 }
95 } else {
96 if constexpr (is_adding_const) {
97 return std::move(std::as_const(x));
98 } else {
99 return std::move(x);
100 }
101 }
102}
103
111template<typename CharT, typename Traits = std::char_traits<CharT>>
112[[nodiscard]] inline std::basic_string<CharT, Traits> getline(std::basic_istream<CharT, Traits>& in, size_t max_size) noexcept
113{
115
116 while (r.size() < max_size) {
117 auto c = in.get();
118 if (c == Traits::eof()) {
119 break;
120
121 } else if (c == '\r') {
122 c = in.get();
123 if (c != '\n') {
124 in.unget();
125 }
126 break;
127
128 } else if (c == '\n') {
129 break;
130 }
131
132 r += Traits::to_char_type(c);
133 }
134
135 return r;
136}
137
138[[nodiscard]] constexpr size_t count(std::string_view haystack, std::string_view needle) noexcept
139{
140 auto count = 0_uz;
141 auto pos = 0_uz;
142
143 while (true) {
144 auto i = haystack.find(needle, pos);
145 if (i == haystack.npos) {
146 return count;
147 }
148
149 ++count;
150 pos = i + needle.size();
151 }
152}
153
154[[nodiscard]] constexpr std::string replace(std::string haystack, std::string_view needle, std::string_view replace) noexcept
155{
156 auto pos = 0_uz;
157
158 while (true) {
159 auto i = haystack.find(needle, pos);
160 if (i == haystack.npos) {
161 return haystack;
162 }
163
164 haystack.replace(i, needle.size(), replace);
165 pos = i + replace.size();
166 }
167}
168
169} // namespace v1
170} // namespace hi::v1
171
172hi_warning_pop();
The HikoGUI namespace.
Definition array_generic.hpp:20
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
std::basic_string< CharT, Traits > getline(std::basic_istream< CharT, Traits > &in, size_t max_size) noexcept
Get a line from an input string, upto a maximum size.
Definition misc.hpp:112
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A type that can not be constructed, copied, moved or destructed.
Definition misc.hpp:76
T count(T... args)
T move(T... args)
T replace(T... args)