aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/je_lua_reference.h
blob: 28c3bc384eee279b52dfda7c1fdb7a22e57866a6 (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
#ifndef __JIN_COMMON_REFERENCE_H
#define __JIN_COMMON_REFERENCE_H

#include "../luax.h"

namespace JinEngine
{
    namespace Lua
    {

        ///
        /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua 
        /// variables.
        ///
        class Reference
        {
        public: 
            Reference(lua_State* L, unsigned int i)
                : mL(L)
            {
                luax_pushvalue(mL, i);
                mIndex = luax_ref(mL, LUA_REGISTRYINDEX);
            }

            ~Reference()
            {
                unref();
            }

            void unref()
            {
                luax_unref(mL, LUA_REGISTRYINDEX, mIndex);
            }

            ///
            /// Push value onto the stack.
            ///
            void push()
            {
                luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex);
            }

        private:
            lua_State* const mL;
            int mIndex;

        };

    } // namespace Lua
} // namespace JinEngine

#endif // __JIN_COMMON_REFERENCE_H