VR-Engage  2.2
Loading...
Searching...
No Matches
luaBinder.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file luaBinder.h
7//! \brief Provides functionality for binding C++ classes to Lua
8//!
9//! Adapted from Game Programming Gems 6: Section 4.2
10
11#pragma once
12
13#include "vreUtil/export.h"
14
15#include <string>
16
17// forward defs
18struct lua_State;
19struct luaL_Reg;
20
21namespace makVre
22{
23class DtLuaValue;
24class DtLuaObject;
25
26//! \brief Class for binding C++ classes to Lua
27//!
28//! This class provides utilities for exposing C++ classes to Lua scripts,
29//! allowing Lua code to create and interact with C++ objects. It manages
30//! the registration of classes, object lifetime, and inheritance relationships.
32{
33public:
34 //! \brief Constructor
35 //!
36 //! \param L The Lua state to bind to
37 DtLuaBinder(lua_State& L);
38
39 //! \brief Destructor
41
42 //! \brief Registers a C++ class with Lua
43 //!
44 //! This method exposes a C++ class to Lua, defining its name, inheritance,
45 //! and available methods. It creates the necessary metatables and registration
46 //! structures for the class.
47 //!
48 //! \param className The name to use for this class in Lua
49 //! \param baseClass The name of the parent class (empty string if none)
50 //! \param memberFuncList Array of member functions that instances can call
51 //! \param publicFuncList Array of static functions accessible through the class table
52 void registerClass(const std::string& className, const std::string& baseClass, const luaL_Reg* memberFuncList,
53 const luaL_Reg* publicFuncList);
54
55 //! \brief Creates and pushes a Lua representation of a C++ object onto the stack
56 //!
57 //! This method takes a C++ object pointer and pushes a Lua userdata
58 //! representing that object onto the Lua stack. The userdata will have
59 //! the appropriate metatable for the object's class.
60 //!
61 //! \note This should only be used in binding/glue functions
62 //!
63 //! \param ptr Pointer to the C++ object
64 //! \param className The class name of the object
65 void pushClassInstance(void* ptr, const std::string& className);
66
67 //! \brief Validates and extracts a C++ object pointer from a Lua value
68 //!
69 //! Checks if the Lua value at the specified stack index is a userdata
70 //! with a metatable matching the given class name (or a derived class).
71 //! If it is, returns the pointer to the C++ object.
72 //!
73 //! \note This should only be used in binding/glue functions
74 //!
75 //! \param index The stack index of the Lua value to check
76 //! \param className The expected class name of the object
77 //! \return Pointer to the C++ object or NULL if validation fails
78 void* checkClassInstance(int index, const std::string& className);
79
80 //! \brief Validates and extracts a C++ object pointer from a Lua object
81 //!
82 //! \note This should only be used in binding/glue functions
83 //!
84 //! \param obj The Lua object to check
85 //! \param className The expected class name of the object
86 //! \return Pointer to the C++ object or NULL if validation fails
87 void* checkClassInstance(DtLuaObject& obj, const std::string& className);
88
89 //! \brief Stores a C++ object as a global Lua variable
90 //!
91 //! Creates a Lua userdata for the specified C++ object and
92 //! assigns it to a global variable with the given name.
93 //!
94 //! \param ptr Pointer to the C++ object
95 //! \param className The class name of the object
96 //! \param variableName The name of the global variable to create
97 void storeClassInstance(void* ptr, const std::string& className, const std::string& variableName);
98
99 //! \brief Converts a relative stack index to an absolute index
100 //!
101 //! \param index The stack index to convert (positive or negative)
102 //! \return The absolute (positive) stack index
103 int absIndex(int index);
104
105protected:
106 //! \brief The Lua state this binder is associated with
107 lua_State* myState;
108};
109
110} // namespace makVre
void storeClassInstance(void *ptr, const std::string &className, const std::string &variableName)
Stores a C++ object as a global Lua variable.
void * checkClassInstance(int index, const std::string &className)
Validates and extracts a C++ object pointer from a Lua value.
lua_State * myState
The Lua state this binder is associated with.
Definition luaBinder.h:107
DtLuaBinder(lua_State &L)
Constructor.
void registerClass(const std::string &className, const std::string &baseClass, const luaL_Reg *memberFuncList, const luaL_Reg *publicFuncList)
Registers a C++ class with Lua.
int absIndex(int index)
Converts a relative stack index to an absolute index.
void * checkClassInstance(DtLuaObject &obj, const std::string &className)
Validates and extracts a C++ object pointer from a Lua object.
~DtLuaBinder()
Destructor.
void pushClassInstance(void *ptr, const std::string &className)
Creates and pushes a Lua representation of a C++ object onto the stack.
Represents a Lua object (typically a table) in C++.
Definition luaObject.h:37
Represents a Lua value in C++.
Definition luaValue.h:29
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Include export definitions for this library.
Definition glsVreMessageUtil.h:49