aboutsummaryrefslogtreecommitdiff
path: root/src/uniquekey.h
blob: 738cb518947259a29735cbbaadb9c20578c2adc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once

#include "compat.h"
#include "macros_and_utils.h"

#include <bit>

// #################################################################################################

class UniqueKey
{
    protected:

    uintptr_t const m_storage{ 0 };

    public:

    char const* m_debugName{ nullptr };

    // ---------------------------------------------------------------------------------------------
    constexpr explicit UniqueKey(uint64_t val_, char const* debugName_ = nullptr)
#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
    : m_storage{ static_cast<uintptr_t>(val_ & 0x7FFFFFFFFFFFull) }
#else // LUAJIT_FLAVOR()
    : m_storage{ static_cast<uintptr_t>(val_) }
#endif // LUAJIT_FLAVOR()
    , m_debugName{ debugName_ }
    {
    }
    // ---------------------------------------------------------------------------------------------
    constexpr UniqueKey(UniqueKey const& rhs_) = default;
    // ---------------------------------------------------------------------------------------------
    constexpr std::strong_ordering operator<=>(UniqueKey const& rhs_) const = default;
    // ---------------------------------------------------------------------------------------------
    bool equals(lua_State* const L_, int i_) const
    {
        return lua_touserdata(L_, i_) == std::bit_cast<void*>(m_storage);
    }
    // ---------------------------------------------------------------------------------------------
    void pushKey(lua_State* const L_) const
    {
        lua_pushlightuserdata(L_, std::bit_cast<void*>(m_storage));
    }
};

// #################################################################################################

class RegistryUniqueKey : public UniqueKey
{
    public:

    using UniqueKey::UniqueKey;

    // ---------------------------------------------------------------------------------------------
    void pushValue(lua_State* const L_) const
    {
        STACK_CHECK_START_REL(L_, 0);
        pushKey(L_);
        lua_rawget(L_, LUA_REGISTRYINDEX);
        STACK_CHECK(L_, 1);
    }
    // ---------------------------------------------------------------------------------------------
    template <typename OP>
    void setValue(lua_State* L_, OP operation_) const
    {
        // Note we can't check stack consistency because operation is not always a push (could be insert, replace, whatever)
        pushKey(L_);                           // ... key
        operation_(L_);                        // ... key value
        lua_rawset(L_, LUA_REGISTRYINDEX);     // ...
    }
    // ---------------------------------------------------------------------------------------------
    template <typename T>
    T* readLightUserDataValue(lua_State* const L_) const
    {
        STACK_GROW(L_, 1);
        STACK_CHECK_START_REL(L_, 0);
        pushValue(L_);
        T* const value{ lua_tolightuserdata<T>(L_, -1) }; // lightuserdata/nil
        lua_pop(L_, 1);
        STACK_CHECK(L_, 0);
        return value;
    }
    // ---------------------------------------------------------------------------------------------
    bool readBoolValue(lua_State* const L_) const
    {
        STACK_GROW(L_, 1);
        STACK_CHECK_START_REL(L_, 0);
        pushValue(L_);
        bool const value{ lua_toboolean(L_, -1) ? true : false}; // bool/nil
        lua_pop(L_, 1);
        STACK_CHECK(L_, 0);
        return value;
    }
};

// #################################################################################################