VR-Engage  2.2
Loading...
Searching...
No Matches
luaReference.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file luaReference.h
7//! \ingroup vreUtil
8//! \brief Provides a reference counting mechanism for Lua objects
9//!
10//! This file defines the DtLuaReference class which manages references to Lua objects
11//! through the Lua registry. It automatically handles reference counting to ensure
12//! proper cleanup of Lua objects when they are no longer needed.
13
14#pragma once
15
16#include "vreUtil/export.h"
17
18struct lua_State;
19
20namespace makVre
21{
22//! \brief Manages references to Lua objects through the Lua registry
23//!
24//! This class creates and manages references to Lua objects in the Lua registry.
25//! It provides reference counting to ensure objects are properly cleaned up when
26//! they are no longer needed by any C++ code.
28{
29public:
30 //! \brief Constructor that creates a reference to a Lua object
31 //! \param s The Lua state containing the object
32 //! \param index Index of the object on the Lua stack to reference
33 //!
34 //! Creates a new reference to the Lua object at the given stack index
35 //! and stores it in the Lua registry.
36 DtLuaReference(lua_State& s, int index);
37
38 //! \brief Destructor
39 //!
40 //! Removes the reference from the Lua registry if this is the last
41 //! C++ object referring to it.
43
44 //! \brief Gets the Lua registry reference value
45 //! \return The reference value in the Lua registry
46 int value() { return myRef; }
47
48 //! \brief Increments the reference count
49 //!
50 //! Called when another C++ object starts using this reference.
51 void inc() { myRefCount++; }
52
53 //! \brief Decrements the reference count
54 //!
55 //! Called when a C++ object stops using this reference.
56 //! If the reference count reaches zero, the Lua reference is released.
57 void dec();
58
59private:
60 //! \brief Copy constructor (private, not implemented)
61 //!
62 //! References should not be copied directly; instead, increment the reference count.
64
65 //! \brief Assignment operator (private, not implemented)
66 //!
67 //! References should not be assigned directly; instead, use reference counting.
69
70 //! \brief Reference to the Lua state
71 lua_State& myState;
72
73 //! \brief Number of C++ objects using this reference
75
76 //! \brief The actual reference value in the Lua registry
77 int myRef;
78};
79
80
81} // namespace makVre
int myRefCount
Number of C++ objects using this reference.
Definition luaReference.h:74
void inc()
Increments the reference count.
Definition luaReference.h:51
DtLuaReference(lua_State &s, int index)
Constructor that creates a reference to a Lua object.
void dec()
Decrements the reference count.
lua_State & myState
Reference to the Lua state.
Definition luaReference.h:71
const DtLuaReference & operator=(const DtLuaReference &src)
Assignment operator (private, not implemented)
int value()
Gets the Lua registry reference value.
Definition luaReference.h:46
~DtLuaReference()
Destructor.
int myRef
The actual reference value in the Lua registry.
Definition luaReference.h:77
DtLuaReference(const DtLuaReference &src)
Copy constructor (private, not implemented)
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