-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Do not bother caching previously used Ids, just a simple increment will do
- Loading branch information
Showing
17 changed files
with
95 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#define DYN_DEFINE_ID_TYPE(T) using T = struct T##_t * | ||
|
||
static_assert(sizeof(void *) == sizeof(uintptr_t), "Pointer and its integer mode must be same size."); | ||
|
||
namespace Dynamo { | ||
/** | ||
* @brief Generate typesafe handles. | ||
* | ||
* @tparam Id | ||
*/ | ||
template <typename Id> | ||
class IdGenerator { | ||
static inline uintptr_t _counter = 0; | ||
|
||
public: | ||
/** | ||
* @brief Generate an id. | ||
* | ||
* @return Id | ||
*/ | ||
static inline Id generate() { return reinterpret_cast<Id>(_counter++); } | ||
|
||
/** | ||
* @brief Get the key of the id. | ||
* | ||
* @param id | ||
* @return uintptr_t | ||
*/ | ||
static inline uintptr_t key(Id id) { return reinterpret_cast<uintptr_t>(id); } | ||
}; | ||
} // namespace Dynamo |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <Dynamo.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
DYN_DEFINE_ID_TYPE(Id_0); | ||
DYN_DEFINE_ID_TYPE(Id_1); | ||
|
||
TEST_CASE("IdGenerator generate", "[IdGenerator]") { | ||
Id_0 a0 = Dynamo::IdGenerator<Id_0>::generate(); | ||
Id_0 b0 = Dynamo::IdGenerator<Id_0>::generate(); | ||
|
||
Id_1 a1 = Dynamo::IdGenerator<Id_1>::generate(); | ||
Id_1 b1 = Dynamo::IdGenerator<Id_1>::generate(); | ||
|
||
REQUIRE(Dynamo::IdGenerator<Id_0>::key(a0) == 0); | ||
REQUIRE(Dynamo::IdGenerator<Id_0>::key(b0) == 1); | ||
|
||
REQUIRE(Dynamo::IdGenerator<Id_1>::key(a1) == 0); | ||
REQUIRE(Dynamo::IdGenerator<Id_1>::key(b1) == 1); | ||
} |
Oops, something went wrong.