1313
1414namespace mstd {
1515#if _HAS_CXX20 && _MSTD_ENABLE_CXX20
16- template <signed_integral _idT>
16+ template <unsigned_integral _idT>
1717#else
18- template <class _idT , std::enable_if_t <std::is_signed_v <_idT>, bool > = true >
18+ template <class _idT = size_t , std::enable_if_t <std::is_unsigned_v <_idT>, bool > = true >
1919#endif
2020 class id_manager {
2121 public:
@@ -27,7 +27,7 @@ namespace mstd {
2727
2828 static constexpr id_type _maxIds = std::numeric_limits<id_type>::max();
2929
30- void _update_removed_ids () {
30+ constexpr void _update_removed_ids () {
3131 if (_removedIds.empty ()) return ;
3232
3333 auto & last = --_removedIds.end ();
@@ -42,25 +42,25 @@ namespace mstd {
4242 }
4343
4444 public:
45- id_manager () = default;
46- virtual ~id_manager () = default ;
45+ id_manager () noexcept = default;
46+ virtual ~id_manager () noexcept = default ;
4747
48- id_type get_next_id () {
48+ [[nodiscard]] constexpr id_type get_next_id () {
4949 if (!_removedIds.empty ()) {
5050 const id_type id = *_removedIds.begin ();
5151 _removedIds.erase (id);
5252 return id;
5353 }
5454
55- if (_nextId == _maxIds) return id_type (- 1 );
55+ if (_nextId == _maxIds) return bad_id ( );
5656
5757 const id_type id = _nextId;
5858 ++_nextId;
5959 return _nextId;
6060 }
6161
62- bool return_id (const id_type& id) {
63- if (id == - 1 || id >= _nextId || _removedIds.find (id) != _removedIds.end ()) {
62+ [[nodiscard]] constexpr bool return_id (const id_type& id) {
63+ if (id == bad_id () || id >= _nextId || _removedIds.find (id) != _removedIds.end ()) {
6464 return false ;
6565 }
6666
@@ -69,22 +69,26 @@ namespace mstd {
6969 return true ;
7070 }
7171
72- void reset () {
72+ void reset () noexcept {
7373 _nextId = 0 ;
7474 _removedIds.clear ();
7575 }
7676
77- static constexpr id_type max_ids () {
77+ static constexpr id_type bad_id () noexcept {
78+ return static_cast <id_type>(-1 );
79+ }
80+
81+ static constexpr id_type max_ids () noexcept {
7882 return _maxIds;
7983 }
8084
81- static constexpr id_type last_id () {
85+ static constexpr id_type last_id () noexcept {
8286 return _maxIds - 1 ;
8387 }
8488 };
8589
86- using id8_manager = id_manager<int8_t >;
87- using id16_manager = id_manager<int16_t >;
88- using id32_manager = id_manager<int32_t >;
89- using id64_manager = id_manager<int64_t >;
90+ using id8_manager = id_manager<uint8_t >;
91+ using id16_manager = id_manager<uint16_t >;
92+ using id32_manager = id_manager<uint32_t >;
93+ using id64_manager = id_manager<uint64_t >;
9094}
0 commit comments