VR-Engage  2.2
Loading...
Searching...
No Matches
luaValue.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file luaValue.h
7//! \ingroup vreUtil
8//! \brief Provides a C++ wrapper for Lua values
9//!
10//! This file defines the DtLuaValue class which represents Lua values in C++.
11//! It supports various types like numbers, strings, booleans, and provides
12//! type checking and conversion facilities for safely working with Lua values.
13
14#pragma once
15
16#include "vreUtil/export.h"
17
18#include <string>
19
20namespace makVre
21{
22
23//! \brief Represents a Lua value in C++
24//!
25//! This class provides a C++ representation of Lua values with type information
26//! and conversion facilities. It is used for passing values between C++ and Lua,
27//! and serves as a base class for more complex Lua object wrappers.
29{
30public:
31 //! \brief Enumeration of supported Lua value types
32 //!
33 //! This enumeration defines the types of Lua values that can be represented
34 //! by the DtLuaValue class.
36 {
37 SCRIPT_NIL, //!< Nil value (null/undefined)
38 SCRIPT_BOOL, //!< Boolean value (true/false)
39 SCRIPT_INT, //!< Integer value
40 SCRIPT_FLOAT, //!< Single-precision floating point value
41 SCRIPT_DOUBLE, //!< Double-precision floating point value
42 SCRIPT_STRING, //!< String value
43 SCRIPT_POINTER, //!< Pointer value (for light userdata)
44 SCRIPT_FUNCTION, //!< Function reference
45 SCRIPT_TABLE //!< Table reference
46 };
47
48 //! \name Constructors and Destructor
49 //! @{
50
51 //! \brief Default constructor
52 //!
53 //! Creates a nil value
55
56 //! \brief Constructor for boolean values
57 //! \param v Boolean value to store
58 DtLuaValue(bool v);
59
60 //! \brief Constructor for integer values
61 //! \param v Integer value to store
62 DtLuaValue(int v);
63
64 //! \brief Constructor for float values
65 //! \param v Float value to store
66 DtLuaValue(float v);
67
68 //! \brief Constructor for double values
69 //! \param v Double value to store
70 DtLuaValue(double v);
71
72 //! \brief Constructor for string values
73 //! \param v String value to store
74 DtLuaValue(const std::string& v);
75
76 //! \brief Constructor for C-string values
77 //! \param v C-string value to store
78 DtLuaValue(const char* v);
79
80 //! \brief Constructor for pointer values
81 //! \param v Pointer value to store
82 DtLuaValue(void* v);
83
84 //! \brief Virtual destructor
85 virtual ~DtLuaValue();
86 //! @}
87
88 //! \name Copy and Assignment
89 //! @{
90
91 //! \brief Copy constructor
92 //! \param orig Source object to copy from
93 DtLuaValue(const DtLuaValue& orig);
94
95 //! \brief Assignment operator
96 //! \param orig Source object to assign from
97 //! \return Reference to this object
99 //! @}
100
101 //! \brief Gets the type of this Lua value
102 //! \return Enumeration value indicating the type
103 virtual ScriptDataTypeEnum type() const;
104
105 //! \name Type Checking Methods
106 //! \brief Methods for checking the type of the value
107 //! @{
108
109 //! \brief Checks if this value is nil
110 //! \return True if the value is nil, false otherwise
111 virtual bool isNil() const;
112
113 //! \brief Checks if this value is a boolean
114 //! \return True if the value is a boolean, false otherwise
115 virtual bool isBool() const;
116
117 //! \brief Checks if this value is an integer
118 //! \return True if the value is an integer, false otherwise
119 virtual bool isInt() const;
120
121 //! \brief Checks if this value is a float
122 //! \return True if the value is a float, false otherwise
123 virtual bool isFloat() const;
124
125 //! \brief Checks if this value is a double
126 //! \return True if the value is a double, false otherwise
127 virtual bool isDouble() const;
128
129 //! \brief Checks if this value is a string
130 //! \return True if the value is a string, false otherwise
131 virtual bool isString() const;
132
133 //! \brief Checks if this value is a pointer
134 //! \return True if the value is a pointer, false otherwise
135 virtual bool isPointer() const;
136
137 //! \brief Checks if this value is a table
138 //! \return True if the value is a table, false otherwise
139 virtual bool isTable() const;
140
141 //! \brief Checks if this value is a function
142 //! \return True if the value is a function, false otherwise
143 virtual bool isFunction() const;
144 //! @}
145
146 //! \name Value Retrieval Methods
147 //! \brief Methods for retrieving the value with the appropriate type
148 //! @{
149
150 //! \brief Gets the value as a boolean
151 //! \return The boolean value
152 //! \note May perform type conversion if the underlying type is not boolean
153 virtual bool boolValue() const;
154
155 //! \brief Gets the value as an integer
156 //! \return The integer value
157 //! \note May perform type conversion if the underlying type is not integer
158 virtual int intValue() const;
159
160 //! \brief Gets the value as a float
161 //! \return The float value
162 //! \note May perform type conversion if the underlying type is not float
163 virtual float floatValue() const;
164
165 //! \brief Gets the value as a double
166 //! \return The double value
167 //! \note May perform type conversion if the underlying type is not double
168 virtual double doubleValue() const;
169
170 //! \brief Gets the value as a string
171 //! \return The string value
172 //! \note May perform type conversion if the underlying type is not string
173 virtual std::string stringValue() const;
174
175 //! \brief Gets the value as a pointer
176 //! \return The pointer value
177 //! \note Returns nullptr if the underlying type is not a pointer
178 virtual void* pointerValue() const;
179 //! @}
180
181 //! \brief Gets the name of the value's type as a string
182 //! \return String representation of the value's type
183 virtual std::string typeName() const;
184
185protected:
186 //! \brief The type of the stored value
188
189 //! \brief Union storing the actual value
190 //!
191 //! This union stores the value in its native type to avoid unnecessary conversions.
192 //! The active member is determined by the myType field.
193 union
194 {
195 bool myBool; //!< Storage for boolean values
196 int myInt; //!< Storage for integer values
197 float myFloat; //!< Storage for float values
198 double myDouble; //!< Storage for double values
199 const char* myString; //!< Storage for string values
200 void* myPtr; //!< Storage for pointer values
201 };
202};
203
204} // namespace makVre
void * myPtr
Storage for pointer values.
Definition luaValue.h:200
virtual ~DtLuaValue()
Virtual destructor.
virtual std::string typeName() const
Gets the name of the value's type as a string.
bool myBool
Storage for boolean values.
Definition luaValue.h:195
DtLuaValue(bool v)
Constructor for boolean values.
virtual double doubleValue() const
Gets the value as a double.
double myDouble
Storage for double values.
Definition luaValue.h:198
DtLuaValue(const DtLuaValue &orig)
Copy constructor.
DtLuaValue & operator=(const DtLuaValue &orig)
Assignment operator.
virtual bool isTable() const
Checks if this value is a table.
virtual bool isInt() const
Checks if this value is an integer.
float myFloat
Storage for float values.
Definition luaValue.h:197
virtual bool isBool() const
Checks if this value is a boolean.
virtual ScriptDataTypeEnum type() const
Gets the type of this Lua value.
DtLuaValue(const std::string &v)
Constructor for string values.
DtLuaValue(void *v)
Constructor for pointer values.
virtual std::string stringValue() const
Gets the value as a string.
virtual bool boolValue() const
Gets the value as a boolean.
virtual float floatValue() const
Gets the value as a float.
ScriptDataTypeEnum
Enumeration of supported Lua value types.
Definition luaValue.h:36
@ SCRIPT_POINTER
Pointer value (for light userdata)
Definition luaValue.h:43
@ SCRIPT_STRING
String value.
Definition luaValue.h:42
@ SCRIPT_INT
Integer value.
Definition luaValue.h:39
@ SCRIPT_FLOAT
Single-precision floating point value.
Definition luaValue.h:40
@ SCRIPT_FUNCTION
Function reference.
Definition luaValue.h:44
@ SCRIPT_DOUBLE
Double-precision floating point value.
Definition luaValue.h:41
@ SCRIPT_NIL
Nil value (null/undefined)
Definition luaValue.h:37
@ SCRIPT_TABLE
Table reference.
Definition luaValue.h:45
@ SCRIPT_BOOL
Boolean value (true/false)
Definition luaValue.h:38
virtual bool isFloat() const
Checks if this value is a float.
DtLuaValue()
Default constructor.
DtLuaValue(const char *v)
Constructor for C-string values.
ScriptDataTypeEnum myType
The type of the stored value.
Definition luaValue.h:187
virtual bool isNil() const
Checks if this value is nil.
DtLuaValue(int v)
Constructor for integer values.
virtual bool isFunction() const
Checks if this value is a function.
int myInt
Storage for integer values.
Definition luaValue.h:196
virtual int intValue() const
Gets the value as an integer.
virtual bool isDouble() const
Checks if this value is a double.
const char * myString
Storage for string values.
Definition luaValue.h:199
DtLuaValue(double v)
Constructor for double values.
DtLuaValue(float v)
Constructor for float values.
virtual void * pointerValue() const
Gets the value as a pointer.
virtual bool isPointer() const
Checks if this value is a pointer.
virtual bool isString() const
Checks if this value is a string.
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