HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
src
ttauri
fixed_string.hpp
1
// Copyright Take Vos 2021.
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
5
6
7
#pragma once
8
9
namespace
tt {
10
28
template
<
typename
CharT,
int
N>
29
struct
basic_fixed_string
{
30
using
value_type = CharT;
31
32
value_type _str[N + 1];
33
34
constexpr
basic_fixed_string
(
basic_fixed_string
const
&)
noexcept
=
default
;
35
constexpr
basic_fixed_string
(
basic_fixed_string
&&)
noexcept
=
default
;
36
constexpr
basic_fixed_string
&operator=(
basic_fixed_string
const
&)
noexcept
=
default
;
37
constexpr
basic_fixed_string
&operator=(
basic_fixed_string
&&)
noexcept
=
default
;
38
39
[[nodiscard]]
constexpr
basic_fixed_string
() noexcept : _str()
40
{
41
std::memset
(&_str[0], 0,
sizeof
(CharT) * N);
42
}
43
44
[[nodiscard]]
constexpr
basic_fixed_string
(value_type
const
(&str)[N + 1]) noexcept : _str()
45
{
46
std::copy_n
(str, N + 1, _str);
47
}
48
49
[[nodiscard]]
operator
std::basic_string<value_type>
()
const
noexcept
50
{
51
return
std::basic_string<value_type>
{data(), size()};
52
}
53
54
[[nodiscard]]
operator
std::basic_string_view<value_type>()
const
noexcept
55
{
56
return
std::basic_string_view<value_type>{data(), size()};
57
}
58
59
[[nodiscard]]
operator
value_type
const
*()
const
noexcept
60
{
61
return
data();
62
}
63
64
[[nodiscard]]
constexpr
auto
begin()
const
noexcept
65
{
66
return
&_str[0];
67
}
68
69
[[nodiscard]]
constexpr
auto
end()
const
noexcept
70
{
71
return
&_str[N];
72
}
73
74
[[nodiscard]]
constexpr
size_t
size()
const
noexcept
75
{
76
return
N;
77
}
78
79
[[nodiscard]]
constexpr
value_type
const
*data()
const
noexcept
80
{
81
return
&_str[0];
82
}
83
84
template
<
int
M>
85
[[nodiscard]]
constexpr
auto
operator+(
basic_fixed_string<value_type, M>
const
&rhs)
noexcept
86
{
87
auto
r =
basic_fixed_string<value_type, N+M>
{};
88
std::copy_n
(data(), N, &r._str[0]);
89
std::copy_n
(rhs.data(), M + 1, &r._str[N]);
90
return
r;
91
}
92
93
[[nodiscard]]
friend
bool
operator==(
basic_fixed_string
const
&lhs,
basic_fixed_string
const
&rhs)
noexcept
=
default
;
94
};
95
96
template
<
typename
CharT,
size_t
N>
97
basic_fixed_string
(CharT
const
(&)[N]) ->
basic_fixed_string
<CharT, N - 1>;
98
99
100
template
<
size_t
N>
101
using
fixed_string
=
basic_fixed_string<char,N>
;
102
103
template
<
size_t
N>
104
using
fixed_wstring
=
basic_fixed_string<wchar_t,N>
;
105
106
template
<
size_t
N>
107
using
fixed_u8string
=
basic_fixed_string<char8_t,N>
;
108
109
template
<
size_t
N>
110
using
fixed_u16string
=
basic_fixed_string<char16_t,N>
;
111
112
template
<
size_t
N>
113
using
fixed_u32string
=
basic_fixed_string<char32_t,N>
;
114
115
116
}
117
tt::basic_fixed_string
example: ``` template<tt::basic_fixed_string Foo> class A { auto bar() { return std::string{Foo}; } }...
Definition
fixed_string.hpp:29
std::basic_string
std::copy_n
T copy_n(T... args)
std::memset
T memset(T... args)
Generated on Mon Apr 22 2024 12:53:49 for HikoGUI by
1.10.0