VR-Engage  2.2
Loading...
Searching...
No Matches
luaFunction.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file luaFunction.h
7//! \ingroup vreUtil
8//! \brief Provides a C++ wrapper for Lua functions
9//!
10//! This file defines the DtLuaFunction class which allows C++ code to easily
11//! invoke Lua functions with type-safe parameter passing and return value handling.
12//! It supports calling Lua functions with various numbers of parameters and
13//! retrieving the result with proper type conversion.
14
15#pragma once
16
17#include "vreUtil/export.h"
18
19#include "vreUtil/luaObject.h"
20
21struct lua_State;
22
23namespace makVre
24{
25//! \brief C++ wrapper for Lua functions
26//!
27//! This class provides a C++ interface to Lua functions, allowing them to be
28//! called with type-safe parameter passing and return value handling.
29//! It supports invoking Lua functions with up to five parameters and retrieving
30//! the results with proper type conversion.
32{
33public:
34 //! \brief Default constructor
35 //!
36 //! Creates an empty, invalid function reference
38
39 //! \brief Constructor from Lua stack value
40 //! \param state The Lua state containing the function
41 //! \param index Index of the function on the Lua stack
42 DtLuaFunction(lua_State& state, int index);
43
44 //! \brief Virtual destructor
45 virtual ~DtLuaFunction() override;
46
47 //! \brief Construct from a Lua object
48 //! \param funcObj Lua object referencing a function
49 //!
50 //! Constructs a function reference from a generic Lua object
51 DtLuaFunction(const DtLuaObject& funcObj);
52
53 //! \brief Copy constructor
54 //! \param funcObj Source function to copy from
56
57 //! \brief Assignment operator from Lua object
58 //! \param funcObj Lua object referencing a function
59 //! \return Reference to this function object
61
62 //! \brief Assignment operator
63 //! \param func Source function to assign from
64 //! \return Reference to this function object
66
67 //! \brief Calls the Lua function with no parameters
68 //! \tparam RT Return type to convert the Lua result to
69 //! \return The result of the function call converted to type RT
70 //! \throw std::string if the function call fails or type conversion fails
71 template <typename RT>
72 RT call();
73
74 //! \brief Calls the Lua function with one parameter
75 //! \tparam RT Return type to convert the Lua result to
76 //! \tparam P0 Type of the first parameter
77 //! \param arg1 The first parameter to pass to the function
78 //! \return The result of the function call converted to type RT
79 //! \throw std::string if the function call fails or type conversion fails
80 template <typename RT, typename P0>
81 RT call(const P0& arg1);
82
83 //! \brief Calls the Lua function with two parameters
84 //! \tparam RT Return type to convert the Lua result to
85 //! \tparam P0 Type of the first parameter
86 //! \tparam P1 Type of the second parameter
87 //! \param arg1 The first parameter to pass to the function
88 //! \param arg2 The second parameter to pass to the function
89 //! \return The result of the function call converted to type RT
90 //! \throw std::string if the function call fails or type conversion fails
91 template <typename RT, typename P0, typename P1>
92 RT call(const P0& arg1, const P1& arg2);
93
94 //! \brief Calls the Lua function with three parameters
95 //! \tparam RT Return type to convert the Lua result to
96 //! \tparam P0 Type of the first parameter
97 //! \tparam P1 Type of the second parameter
98 //! \tparam P2 Type of the third parameter
99 //! \param arg1 The first parameter to pass to the function
100 //! \param arg2 The second parameter to pass to the function
101 //! \param arg3 The third parameter to pass to the function
102 //! \return The result of the function call converted to type RT
103 //! \throw std::string if the function call fails or type conversion fails
104 template <typename RT, typename P0, typename P1, typename P2>
105 RT call(const P0& arg1, const P1& arg2, const P2& arg3);
106
107 //! \brief Calls the Lua function with four parameters
108 //! \tparam RT Return type to convert the Lua result to
109 //! \tparam P0 Type of the first parameter
110 //! \tparam P1 Type of the second parameter
111 //! \tparam P2 Type of the third parameter
112 //! \tparam P3 Type of the fourth parameter
113 //! \param arg1 The first parameter to pass to the function
114 //! \param arg2 The second parameter to pass to the function
115 //! \param arg3 The third parameter to pass to the function
116 //! \param arg4 The fourth parameter to pass to the function
117 //! \return The result of the function call converted to type RT
118 //! \throw std::string if the function call fails or type conversion fails
119 template <typename RT, typename P0, typename P1, typename P2, typename P3>
120 RT call(const P0& arg1, const P1& arg2, const P2& arg3, const P3& arg4);
121
122 //! \brief Calls the Lua function with five parameters
123 //! \tparam RT Return type to convert the Lua result to
124 //! \tparam P0 Type of the first parameter
125 //! \tparam P1 Type of the second parameter
126 //! \tparam P2 Type of the third parameter
127 //! \tparam P3 Type of the fourth parameter
128 //! \tparam P4 Type of the fifth parameter
129 //! \param arg1 The first parameter to pass to the function
130 //! \param arg2 The second parameter to pass to the function
131 //! \param arg3 The third parameter to pass to the function
132 //! \param arg4 The fourth parameter to pass to the function
133 //! \param arg5 The fifth parameter to pass to the function
134 //! \return The result of the function call converted to type RT
135 //! \throw std::string if the function call fails or type conversion fails
136 template <typename RT, typename P0, typename P1, typename P2, typename P3, typename P4>
137 RT call(const P0& arg1, const P1& arg2, const P2& arg3, const P3& arg4, const P4& arg5);
138
139protected:
140 //! \brief Prepares the stack for a function call
141 //!
142 //! Sets up error handling and pushes the function onto the stack
143 virtual void preFunction();
144
145 //! \brief Cleans up after a function call
146 //! \param argc Number of arguments that were passed to the function
147 //!
148 //! Removes the error handler and adjusts the stack as needed
149 virtual void postFunction(int argc);
150
151 //! \brief Stack index for the error handler function
153};
154}; // namespace makVre
155
156#include "vreUtil/luaFunction.inl"
virtual ~DtLuaFunction() override
Virtual destructor.
DtLuaFunction()
Default constructor.
RT call(const P0 &arg1)
Calls the Lua function with one parameter.
RT call(const P0 &arg1, const P1 &arg2, const P2 &arg3)
Calls the Lua function with three parameters.
DtLuaFunction(lua_State &state, int index)
Constructor from Lua stack value.
DtLuaFunction(const DtLuaFunction &funcObj)
Copy constructor.
RT call(const P0 &arg1, const P1 &arg2, const P2 &arg3, const P3 &arg4)
Calls the Lua function with four parameters.
RT call()
Calls the Lua function with no parameters.
virtual void preFunction()
Prepares the stack for a function call.
int myErrorIndex
Stack index for the error handler function.
Definition luaFunction.h:152
DtLuaFunction & operator=(const DtLuaFunction &func)
Assignment operator.
RT call(const P0 &arg1, const P1 &arg2, const P2 &arg3, const P3 &arg4, const P4 &arg5)
Calls the Lua function with five parameters.
DtLuaFunction(const DtLuaObject &funcObj)
Construct from a Lua object.
virtual void postFunction(int argc)
Cleans up after a function call.
DtLuaFunction & operator=(const DtLuaObject &funcObj)
Assignment operator from Lua object.
RT call(const P0 &arg1, const P1 &arg2)
Calls the Lua function with two parameters.
virtual lua_State * state()
Gets the Lua state associated with this object.
DtLuaObject()
Default constructor.
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Provides access to Lua tables and objects with C++ wrappers.
Include export definitions for this library.
Definition glsVreMessageUtil.h:49