aboutsummaryrefslogtreecommitdiff
path: root/src/uniquekey.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-03-20 16:23:54 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2024-03-20 16:23:54 +0100
commit6556cc558f0602cc99b1a8d1c7212b2e91490cdc (patch)
tree1763ed796df57a39432f045eccb8d80b2ec02a93 /src/uniquekey.h
parent0b516e9490b51bdd15c347fcda35b5dbb06b4829 (diff)
downloadlanes-6556cc558f0602cc99b1a8d1c7212b2e91490cdc.tar.gz
lanes-6556cc558f0602cc99b1a8d1c7212b2e91490cdc.tar.bz2
lanes-6556cc558f0602cc99b1a8d1c7212b2e91490cdc.zip
C++ migration: UniqueKey
Diffstat (limited to 'src/uniquekey.h')
-rw-r--r--src/uniquekey.h43
1 files changed, 31 insertions, 12 deletions
diff --git a/src/uniquekey.h b/src/uniquekey.h
index f219ab1..fb98628 100644
--- a/src/uniquekey.h
+++ b/src/uniquekey.h
@@ -2,21 +2,40 @@
2 2
3#include "compat.h" 3#include "compat.h"
4 4
5// Lua light userdata can hold a pointer. 5class UniqueKey
6struct s_UniqueKey
7{ 6{
8 void* value; 7 private:
9}; 8
10typedef struct s_UniqueKey UniqueKey; 9 uintptr_t m_storage;
10
11 public:
11 12
13 constexpr UniqueKey(uintptr_t val_)
12#if LUAJIT_FLAVOR() == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations 14#if LUAJIT_FLAVOR() == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations
13#define MAKE_UNIQUE_KEY( p_) ((void*)((ptrdiff_t)(p_) & 0x7fffffffffffull)) 15 : m_storage{ val_ & 0x7fffffffffffull }
14#else // LUAJIT_FLAVOR() 16#else // LUAJIT_FLAVOR()
15#define MAKE_UNIQUE_KEY( p_) ((void*)(ptrdiff_t)(p_)) 17 : m_storage{ val_ }
16#endif // LUAJIT_FLAVOR() 18#endif // LUAJIT_FLAVOR()
19 {
20 }
21 constexpr UniqueKey(UniqueKey const& rhs_) = default;
22 constexpr bool operator!=(UniqueKey const& rhs_) const
23 {
24 return m_storage != rhs_.m_storage;
25 }
26 constexpr bool operator==(UniqueKey const& rhs_) const
27 {
28 return m_storage == rhs_.m_storage;
29 }
17 30
18#define DECLARE_UNIQUE_KEY( name_) UniqueKey name_ 31 void push(lua_State* const L) const
19#define DECLARE_CONST_UNIQUE_KEY( name_, p_) UniqueKey const name_ = { MAKE_UNIQUE_KEY( p_)} 32 {
20 33 // unfortunately, converting a scalar to a pointer must go through a C cast
21#define push_unique_key( L, key_) lua_pushlightuserdata( L, key_.value) 34 lua_pushlightuserdata(L, (void*) m_storage);
22#define equal_unique_key( L, i, key_) (lua_touserdata( L, i) == key_.value) 35 }
36 bool equals(lua_State* const L, int i) const
37 {
38 // unfortunately, converting a scalar to a pointer must go through a C cast
39 return lua_touserdata(L, i) == (void*) m_storage;
40 }
41};