OGLplus  (0.59.0) a C++ wrapper for rendering APIs

identifier.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_IDENTIFIER_HPP
10 #define EAGINE_IDENTIFIER_HPP
11 
12 #include "biteset.hpp"
13 #include "fixed_size_str.hpp"
14 #include "identifier_t.hpp"
15 #include "selector.hpp"
16 #include <iosfwd>
17 
18 namespace eagine {
19 //------------------------------------------------------------------------------
24 template <typename CharSet>
26 public:
28  static constexpr auto encode(const char c) noexcept -> std::uint8_t {
29  return _do_encode(c, 0, CharSet::values);
30  }
31 
33  static constexpr auto decode(const std::uint8_t i) noexcept -> char {
34  return _do_decode(i, CharSet::values);
35  }
36 
38  static constexpr auto invalid() noexcept {
39  return _get_invalid(CharSet::values);
40  }
41 
43  static constexpr auto invalid(const std::uint8_t c) noexcept {
44  return c >= invalid();
45  }
46 
48  static inline auto chars() -> string_view {
49  return {CharSet::values};
50  }
51 
52 private:
53  template <std::size_t L>
54  static constexpr auto _get_invalid(const char (&)[L]) noexcept {
55  return std::uint8_t(L);
56  }
57 
58  template <std::size_t L>
59  static constexpr auto _do_encode(
60  const char c,
61  const std::uint8_t i,
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))
65  : invalid();
66  }
67 
68  template <std::size_t L>
69  static constexpr auto
70  _do_decode(const std::uint8_t i, const char (&enc)[L]) noexcept -> char {
71  return (i < invalid()) ? enc[i] : '\0';
72  }
73 };
74 //------------------------------------------------------------------------------
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'};
87 };
88 //------------------------------------------------------------------------------
93 template <std::size_t M>
95 public:
96  template <typename... C>
97  identifier_name(span_size_t len, C... c) noexcept
98  : _len{std::uint8_t(len)}
99  , _str{c...} {}
100 
103 
105  using value_type = char;
106 
108  using iterator = const char*;
109 
112 
114  auto data() const noexcept {
115  return _str.data();
116  }
117 
119  auto size() const noexcept {
120  return size_type(_len);
121  }
122 
124  auto begin() const -> const_iterator {
125  return _str.data();
126  }
127 
129  auto end() const -> const_iterator {
130  return _str.data() + size();
131  }
132 
134  auto view() const -> string_view {
135  return {data(), size()};
136  }
137 
139  auto str() const -> std::string {
140  return {_str.data(), _len};
141  }
142 
145  auto str(std::string& s) const -> std::string& {
146  s.assign(_str.data(), _len);
147  return s;
148  }
149 
150 private:
151  std::uint8_t _len{0};
152  fixed_size_string<M> _str{};
153 };
154 //------------------------------------------------------------------------------
158 template <std::size_t M>
159 static inline auto operator<<(std::ostream& out, const identifier_name<M>& n)
160  -> std::ostream& {
161  return out.write(n.data(), std::streamsize(n.size()));
162 }
163 //------------------------------------------------------------------------------
177 template <std::size_t M, std::size_t B, typename CharSet, typename UIntT>
180 
181 public:
182  static_assert(
183  (1U << B) > sizeof(CharSet::values),
184  "B-bits are not sufficient to represent CharSet");
185 
188 
191 
193  using value_type = char;
194 
197 
199  constexpr basic_identifier() noexcept = default;
200 
202  template <std::size_t L, typename = std::enable_if_t<(L <= M + 1)>>
203  explicit constexpr basic_identifier(const char (&init)[L]) noexcept
204  : _bites{_make_bites(
205  static_cast<const char*>(init),
206  L,
207  std::make_index_sequence<M>{})} {}
208 
210  explicit constexpr basic_identifier(span<const char> init) noexcept
211  : _bites{_make_bites(
212  init.data(),
213  init.size(),
214  std::make_index_sequence<M>{})} {}
215 
216  explicit constexpr basic_identifier(UIntT init) noexcept
217  : _bites{_bites_t::from_value(init)} {}
218 
219  explicit constexpr basic_identifier(_bites_t init) noexcept
220  : _bites{std::move(init)} {}
221 
223  static constexpr auto max_size() noexcept -> size_type {
224  return M;
225  }
226 
229  constexpr auto is_empty() const noexcept -> bool {
230  return value() == 0;
231  }
232 
233  constexpr explicit operator bool() const noexcept {
234  return !is_empty();
235  }
236 
239  constexpr auto size() const noexcept -> size_type {
240  return is_empty() ? 0 : _get_size(0);
241  }
242 
244  constexpr auto operator[](size_type idx) const noexcept -> value_type {
245  return encoding::decode(_bites[idx]);
246  }
247 
248  constexpr auto value() const noexcept -> UIntT {
249  return _bites.bytes().template as<UIntT>();
250  }
251 
252  constexpr auto matches(UIntT what) const noexcept {
253  return value() == what;
254  }
255 
258  constexpr auto name() const noexcept -> name_type {
259  return _get_name(std::make_index_sequence<M>{});
260  }
261 
264  inline auto str() const -> std::string {
265  return name().str();
266  }
267 
269  friend constexpr auto
270  operator==(const basic_identifier& a, const basic_identifier& b) noexcept {
271  return a._bites == b._bites;
272  }
273 
275  friend constexpr auto
276  operator!=(const basic_identifier& a, const basic_identifier& b) noexcept {
277  return a._bites != b._bites;
278  }
279 
282  friend constexpr auto
283  operator<(const basic_identifier& a, const basic_identifier& b) noexcept {
284  return a._bites < b._bites;
285  }
286 
289  friend constexpr auto
290  operator<=(const basic_identifier& a, const basic_identifier& b) noexcept {
291  return a._bites <= b._bites;
292  }
293 
296  friend constexpr auto
297  operator>(const basic_identifier& a, const basic_identifier& b) noexcept {
298  return a._bites > b._bites;
299  }
300 
303  friend constexpr auto
304  operator>=(const basic_identifier& a, const basic_identifier& b) noexcept {
305  return a._bites >= b._bites;
306  }
307 
308 private:
309  _bites_t _bites;
310 
311  template <std::size_t... I>
312  static constexpr auto _make_bites(
313  const char* init,
314  std::size_t l,
315  std::index_sequence<I...>) noexcept {
317  encoding::encode((I < l) ? init[I] : '\0')...};
318  }
319 
320  template <std::size_t... I>
321  constexpr auto _get_name(std::index_sequence<I...>) const noexcept
322  -> name_type {
323  return name_type{size(), (*this)[size_type(I)]...};
324  }
325 
326  constexpr auto _get_size(std::size_t s) const noexcept -> std::size_t {
327  return (s < M) ? encoding::invalid(_bites[size_type(s)])
328  ? s
329  : _get_size(s + 1)
330  : M;
331  }
332 };
333 //------------------------------------------------------------------------------
345 using identifier =
347 //------------------------------------------------------------------------------
353 #define EAGINE_ID(NAME) ::eagine::identifier(#NAME)
354 
359 #define EAGINE_ID_V(NAME) ::eagine::identifier(#NAME).value()
360 
367 #define EAGINE_TAG_TYPE(NAME) ::eagine::selector<EAGINE_ID_V(NAME)>
368 
375 #define EAGINE_TAG(NAME) \
376  EAGINE_TAG_TYPE(NAME) {}
377 //------------------------------------------------------------------------------
378 #if EAGINE_HAS_LONG_ID
379 using long_identifier =
389 //------------------------------------------------------------------------------
393 #define EAGINE_LONG_ID(NAME) ::eagine::long_identifier(#NAME)
394 #define EAGINE_LONG_ID_V(NAME) ::eagine::long_identifier(#NAME).value()
395 #endif
396 //------------------------------------------------------------------------------
397 } // namespace eagine
398 
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

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).