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
19hi_export_module(hikogui.utility.misc);
20
21hi_warning_push();
22// C26472: Don't use static_cast for arithmetic conversions, Use brace initialization, gsl::narrow_cast or gsl::narrow (type.1).
23// We do not have access to narrow_cast in this file.
24hi_warning_ignore_msvc(26472);
25// C26473: Don't cast between pointer types where the source type and the target type are the same (type.1).
26// hi_forward need to specifically cast a value to a reference using static_cast.
27hi_warning_ignore_msvc(26473);
28
29hi_export namespace hi { inline namespace v1 {
30
34
35#define ssizeof(x) (static_cast<ssize_t>(sizeof(x)))
36
37constexpr std::size_t operator"" _uz(unsigned long long lhs) noexcept
38{
39 return static_cast<std::size_t>(lhs);
40}
41
42constexpr std::size_t operator"" _zu(unsigned long long lhs) noexcept
43{
44 return static_cast<std::size_t>(lhs);
45}
46
47constexpr std::ptrdiff_t operator"" _z(unsigned long long lhs) noexcept
48{
49 return static_cast<std::ptrdiff_t>(lhs);
50}
51
55template<typename T, typename U>
56[[nodiscard]] bool compare_store(T& lhs, U&& rhs) noexcept
57{
58 if (lhs != rhs) {
59 lhs = std::forward<U>(rhs);
60 return true;
61 } else {
62 return false;
63 }
64}
65
71template<typename T, typename U>
72[[nodiscard]] bool compare_store(std::atomic<T>& lhs, U&& rhs) noexcept
73{
74 return lhs.exchange(rhs, std::memory_order::relaxed) != rhs;
75}
76
79struct override_t {};
80
86struct intrinsic_t {};
87constexpr auto intrinsic = intrinsic_t{};
88
91struct unusable_t {
92 unusable_t() = delete;
93 ~unusable_t() = delete;
94 unusable_t(unusable_t const&) = delete;
95 unusable_t(unusable_t&&) = delete;
96 unusable_t& operator=(unusable_t const&) = delete;
97 unusable_t& operator=(unusable_t&&) = delete;
98};
99
100template<class T, class U>
101[[nodiscard]] constexpr auto&& forward_like(U && x) noexcept
102{
103 constexpr bool is_adding_const = std::is_const_v<std::remove_reference_t<T>>;
104 if constexpr (std::is_lvalue_reference_v<T&&>) {
105 if constexpr (is_adding_const) {
106 return std::as_const(x);
107 } else {
108 return static_cast<U&>(x);
109 }
110 } else {
111 if constexpr (is_adding_const) {
112 return std::move(std::as_const(x));
113 } else {
114 return std::move(x);
115 }
116 }
117}
118
126hi_export template<typename CharT, typename Traits = std::char_traits<CharT>>
128{
130
131 while (r.size() < max_size) {
132 auto c = in.get();
133 if (c == Traits::eof()) {
134 break;
135
136 } else if (c == '\r') {
137 c = in.get();
138 if (c != '\n') {
139 in.unget();
140 }
141 break;
142
143 } else if (c == '\n') {
144 break;
145 }
146
147 r += Traits::to_char_type(c);
148 }
149
150 return r;
151}
152
153}} // namespace hi::v1
154
155hi_warning_pop();
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:56
hi_export 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:127
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Tag used for special functions or constructions to do a override compared to another function of the ...
Definition misc.hpp:79
Tag used in constructors to set the intrinsic value of that object.
Definition misc.hpp:86
A type that can not be constructed, copied, moved or destructed.
Definition misc.hpp:91
T move(T... args)