Skip to content

Commit ce764e9

Browse files
committed
some small updates
1 parent 7761e70 commit ce764e9

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

include/events/mstd/event_handler.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace mstd {
1414

1515
template<typename... Args>
1616
class event_handler {
17-
using id_type = int64_t;
17+
using id_type = size_t;
1818
using event_action_type = action<Args...>;
1919
using events_type = std::unordered_map<id_type, event_action_type>;
2020
using id_manager_type = id_manager<id_type>;
@@ -24,23 +24,23 @@ namespace mstd {
2424
id_manager_type _ids = {};
2525

2626
public:
27-
event_handler() = default;
28-
virtual ~event_handler() = default;
27+
event_handler() noexcept = default;
28+
virtual ~event_handler() noexcept = default;
2929

3030
#if _HAS_CXX20 && _MSTD_ENABLE_CXX20
3131
template<event_action_func<event_action_type> F>
3232
#else
3333
template<class F, std::enable_if_t<mstd::is_same_function_v<F, event_action_type>, bool> = true>
3434
#endif
35-
constexpr id_type add_callback(const F& callback) {
35+
[[nodiscard]] constexpr id_type add_callback(const F& callback) {
3636
id_type id = _ids.get_next_id();
37-
if (id == -1) return id;
37+
if (id == id_manager_type::bad_id()) return id;
3838

3939
_events[id] = callback;
4040
return id;
4141
}
4242

43-
bool remove_callback(const id_type& callbackId) {
43+
[[nodiscard]] constexpr bool remove_callback(const id_type& callbackId) {
4444
auto& itr = _events.find(callbackId);
4545
if (itr == _events.end()) {
4646
return false;
@@ -62,15 +62,15 @@ namespace mstd {
6262
}
6363

6464
#if _HAS_CXX20 && _MSTD_ENABLE_CXX20
65-
template<event_action_func F>
65+
template<eevent_action_func<event_action_type> F>
6666
#else
6767
template<class F, std::enable_if_t<mstd::is_same_function_v<F, event_action_type>, bool> = true>
6868
#endif
69-
constexpr id_type operator+=(const F& callback) {
69+
[[nodiscard]] constexpr id_type operator+=(const F& callback) {
7070
return add_callback(callback);
7171
}
7272

73-
constexpr bool operator-=(id_type callbackId) {
73+
[[nodiscard]] constexpr bool operator-=(id_type callbackId) {
7474
return remove_callback(callbackId);
7575
}
7676

include/main/mstd/mstd_config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#pragma region VERSION
1212
#define MSTD_VERSION_MAJOR 1
1313
#define MSTD_VERSION_MINOR 2
14-
#define MSTD_VERSION_PATCH 0
14+
#define MSTD_VERSION_PATCH 1
1515

1616
#define _MSTD_STRINGIFY_HELPER(x) #x
1717

@@ -30,7 +30,7 @@
3030
#pragma endregion
3131

3232
#pragma region LAST_UPDATE
33-
#define MSTD_LAST_UPDATE_DAY 4
33+
#define MSTD_LAST_UPDATE_DAY 15
3434
#define MSTD_LAST_UPDATE_MONTH 10
3535
#define MSTD_LAST_UPDATE_YEAR 2025
3636

include/utils/mstd/id_manager.hpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace 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

Comments
 (0)