Go to the documentation of this file.
9 #ifndef EAGINE_IDENTIFIER_HPP
10 #define EAGINE_IDENTIFIER_HPP
24 template <
typename CharSet>
28 static constexpr
auto encode(
const char c) noexcept -> std::uint8_t {
29 return _do_encode(c, 0, CharSet::values);
33 static constexpr
auto decode(
const std::uint8_t i) noexcept ->
char {
34 return _do_decode(i, CharSet::values);
38 static constexpr
auto invalid() noexcept {
39 return _get_invalid(CharSet::values);
43 static constexpr
auto invalid(
const std::uint8_t c) noexcept {
49 return {CharSet::values};
53 template <std::
size_t L>
54 static constexpr
auto _get_invalid(
const char (&)[L]) noexcept {
55 return std::uint8_t(L);
58 template <std::
size_t L>
59 static constexpr
auto _do_encode(
62 const char (&enc)[L]) noexcept -> std::uint8_t {
63 return ((i < L) && (c !=
'\0'))
64 ? ((c == enc[i]) ? i : _do_encode(c, i + 1, enc))
68 template <std::
size_t L>
70 _do_decode(
const std::uint8_t i,
const char (&enc)[L]) noexcept ->
char {
71 return (i <
invalid()) ? enc[i] :
'\0';
81 static constexpr
const char values[63] = {
82 '_',
'e',
't',
'a',
'o',
'i',
'n',
's',
'r',
'h',
'l',
'd',
'c',
83 'u',
'm',
'f',
'p',
'g',
'w',
'y',
'b',
'v',
'k',
'x',
'j',
'q',
84 'z',
'T',
'A',
'I',
'S',
'O',
'W',
'H',
'B',
'C',
'M',
'F',
'P',
85 'D',
'R',
'L',
'E',
'G',
'N',
'Y',
'U',
'K',
'V',
'J',
'Q',
'X',
86 'Z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
93 template <std::
size_t M>
96 template <
typename... C>
98 : _len{std::uint8_t(len)}
139 auto str() const -> std::
string {
140 return {_str.
data(), _len};
145 auto str(std::string& s)
const -> std::string& {
146 s.assign(_str.
data(), _len);
151 std::uint8_t _len{0};
152 fixed_size_string<M> _str{};
158 template <std::
size_t M>
161 return out.write(n.data(), std::streamsize(n.size()));
177 template <std::
size_t M, std::
size_t B,
typename CharSet,
typename UIntT>
183 (1U << B) >
sizeof(CharSet::values),
184 "B-bits are not sufficient to represent CharSet");
202 template <std::
size_t L, typename = std::enable_if_t<(L <= M + 1)>>
204 : _bites{_make_bites(
205 static_cast<const char*
>(init),
207 std::make_index_sequence<M>{})} {}
211 : _bites{_make_bites(
214 std::make_index_sequence<M>{})} {}
220 : _bites{std::move(init)} {}
233 constexpr
explicit operator bool() const noexcept {
240 return is_empty() ? 0 : _get_size(0);
248 constexpr
auto value() const noexcept -> UIntT {
249 return _bites.
bytes().template as<UIntT>();
252 constexpr
auto matches(UIntT what)
const noexcept {
253 return value() == what;
259 return _get_name(std::make_index_sequence<M>{});
264 inline auto str() const -> std::
string {
269 friend constexpr
auto
271 return a._bites == b._bites;
275 friend constexpr
auto
277 return a._bites != b._bites;
282 friend constexpr
auto
284 return a._bites < b._bites;
289 friend constexpr
auto
291 return a._bites <= b._bites;
296 friend constexpr
auto
298 return a._bites > b._bites;
303 friend constexpr
auto
305 return a._bites >= b._bites;
311 template <std::size_t... I>
312 static constexpr
auto _make_bites(
315 std::index_sequence<I...>) noexcept {
320 template <std::size_t... I>
321 constexpr
auto _get_name(std::index_sequence<I...>)
const noexcept
326 constexpr
auto _get_size(std::size_t s)
const noexcept -> std::size_t {
353 #define EAGINE_ID(NAME) ::eagine::identifier(#NAME)
359 #define EAGINE_ID_V(NAME) ::eagine::identifier(#NAME).value()
367 #define EAGINE_TAG_TYPE(NAME) ::eagine::selector<EAGINE_ID_V(NAME)>
375 #define EAGINE_TAG(NAME) \
376 EAGINE_TAG_TYPE(NAME) {}
378 #if EAGINE_HAS_LONG_ID
393 #define EAGINE_LONG_ID(NAME) ::eagine::long_identifier(#NAME)
394 #define EAGINE_LONG_ID_V(NAME) ::eagine::long_identifier(#NAME).value()
399 #endif // EAGINE_IDENTIFIER_HPP
static constexpr auto encode(const char c) noexcept -> std::uint8_t
Encoded the specified character as N-bit byte.
Definition: identifier.hpp:28
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static auto chars() -> string_view
Returns the valid character set as a string_view.
Definition: identifier.hpp:48
auto str() const -> std::string
Returns the unpacked identifier name as a standard string.
Definition: identifier.hpp:139
Common code is placed in this namespace.
Definition: eagine.hpp:21
constexpr friend auto operator>(const basic_identifier &a, const basic_identifier &b) noexcept
Greater-than comparison.
Definition: identifier.hpp:297
basic_identifier< 20, 6, default_identifier_char_set, long_identifier_t > long_identifier
Long identifier type used throughout the project.
Definition: identifier.hpp:388
identifier_name< M > name_type
Alias for the unpacked identifier_name type.
Definition: identifier.hpp:196
auto end() const -> const_iterator
Returns an iterator past the end of the unpacked identifier name.
Definition: identifier.hpp:129
constexpr auto operator[](size_type idx) const noexcept -> value_type
Subscript operator. Allows to access individual characters.
Definition: identifier.hpp:244
static constexpr auto from_value(UIntT init) noexcept
Constructs a biteset from the specified values splitting bit groups of size=B.
Definition: biteset.hpp:465
static constexpr auto max_size() noexcept -> size_type
Returns the maximum length of this identifier type.
Definition: identifier.hpp:223
constexpr auto bytes() const noexcept -> const byteset< store_size > &
Converts this biteset into a byteset.
Definition: biteset.hpp:556
iterator const_iterator
Alias for the const iterator type.
Definition: identifier.hpp:111
static constexpr auto invalid() noexcept
Returns the invalid value from the encoding.
Definition: identifier.hpp:38
Characted encoding table for identifier.
Definition: identifier.hpp:78
char value_type
Alias for the string character type.
Definition: identifier.hpp:105
constexpr basic_identifier() noexcept=default
Default constructor. Constructs an empty identifier.
span_size_t size_type
Alias for the length type.
Definition: identifier.hpp:190
static constexpr auto invalid(const std::uint8_t c) noexcept
Indicates if the encoded value is invalid.
Definition: identifier.hpp:43
constexpr friend auto operator==(const basic_identifier &a, const basic_identifier &b) noexcept
Equality comparison.
Definition: identifier.hpp:270
constexpr friend auto operator!=(const basic_identifier &a, const basic_identifier &b) noexcept
Nonequality comparison.
Definition: identifier.hpp:276
constexpr friend auto operator>=(const basic_identifier &a, const basic_identifier &b) noexcept
Greater-equal comparison.
Definition: identifier.hpp:304
auto str() const -> std::string
Returns this identifier as unpacked standard string.
Definition: identifier.hpp:264
constexpr auto name() const noexcept -> name_type
Returns this identifier as unpacked identifier_name.
Definition: identifier.hpp:258
constexpr friend auto operator<=(const basic_identifier &a, const basic_identifier &b) noexcept
Less-equal comparison.
Definition: identifier.hpp:290
const char * iterator
Alias for the iterator type.
Definition: identifier.hpp:108
Basic template for limited length, packed string identifiers.
Definition: identifier.hpp:178
constexpr auto size() const noexcept -> size_type
Returns the size of this identifier.
Definition: identifier.hpp:239
Helper class implementing identifier encoding functionality.
Definition: identifier.hpp:25
static constexpr auto decode(const std::uint8_t i) noexcept -> char
Encoded the specified N-bit byte as character.
Definition: identifier.hpp:33
auto data() const noexcept
Returns pointer to the internally-stored character data.
Definition: fixed_size_str.hpp:73
auto size() const noexcept
Returns the length of the unpacked character string.
Definition: identifier.hpp:119
auto begin() const -> const_iterator
Returns an iterator to the start of the unpacked identifier name.
Definition: identifier.hpp:124
char value_type
Alias for the element type.
Definition: identifier.hpp:193
constexpr basic_identifier(span< const char > init) noexcept
Construction from a const span of characters.
Definition: identifier.hpp:210
constexpr friend auto operator<(const basic_identifier &a, const basic_identifier &b) noexcept
Less-than comparison.
Definition: identifier.hpp:283
auto data() const noexcept
Returns the pointer to the start of the unpacked identifier name.
Definition: identifier.hpp:114
auto str(std::string &s) const -> std::string &
Assigns the unpacked identifier name into a standard string.
Definition: identifier.hpp:145
constexpr auto is_empty() const noexcept -> bool
Returns the size of this identifier.
Definition: identifier.hpp:229
static auto operator<<(std::ostream &out, const identifier_name< M > &n) -> std::ostream &
Operator for writing identifier_name into output streams.
Definition: identifier.hpp:159
span_size_t size_type
Alias for the string lenght type.
Definition: identifier.hpp:102
auto view() const -> string_view
Returns a string view covering the unpacked identifier name.
Definition: identifier.hpp:134
Helper template for unpacking of identifier into a character string.
Definition: identifier.hpp:94
basic_identifier< 10, 6, default_identifier_char_set, identifier_t > identifier
Default identifier type used throughout the project.
Definition: identifier.hpp:346
static constexpr const char values[63]
The default identifier character set.
Definition: identifier.hpp:81