VR-Engage  2.2
Loading...
Searching...
No Matches
initializer.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file initializer.h
7//! \ingroup vreUtil
8//! \brief Provides configuration initialization for VREngage components
9//!
10//! This file defines the DtInitializer and DtInitTable classes that handle loading
11//! and accessing configuration data from Lua scripts. These classes provide a consistent
12//! interface for component initialization and configuration retrieval throughout the
13//! VREngage system.
14
15#pragma once
16
17#include "vreUtil/export.h"
18#include "vreUtil/luaState.h"
20#include "vreUtil/logger.h"
21
22#include <vlutil/vlFilename.h>
23
24#include <string>
25
26struct lua_State;
27
28namespace makVre
29{
30class DtInitializer;
31
32//! \brief Table-based access to Lua state for configuration data
33//!
34//! This class provides table-based access to a Lua state, allowing for
35//! retrieval of configuration values from Lua tables. It is primarily used
36//! by DtInitializer to access the global '_G' table and other configuration tables.
38{
39public:
40 //! \brief Default constructor
41 //!
42 //! Creates an empty initialization table
44
45 //! \brief Constructor with existing Lua state and table
46 //! \param state Reference to the Lua state containing the table
47 //! \param index Index of the table on the Lua stack
48 //! \param throwOnMissingField If true, throws an exception when a requested field is not found
49 DtInitTable(lua_State& state, int index, bool throwOnMissingField = true);
50
51 //! \brief Destructor
52 virtual ~DtInitTable() override;
53
54 template <typename RT>
55 RT findData(const std::string& name, bool resolveDots = true);
56
57 template <typename RT>
58 RT findDataOr(const std::string& name, RT defaultValue, bool silent = false, bool resolveDots = true);
59
60 template <typename RT>
61 RT findData(int index);
62
63 //! \brief Sets whether to throw exceptions for missing fields
64 //! \param set If true, missing fields will cause exceptions; if false, will return default values
65 virtual void setThrowOnMissingField(bool set);
66
67protected:
68 //! \brief Controls exception behavior for missing fields
69 //!
70 //! When true, missing fields will cause exceptions to be thrown
72};
73
74//! \brief Configuration initializer for VREngage components
75//!
76//! This class handles loading and accessing configuration data from Lua scripts.
77//! It provides a consistent interface for component initialization throughout
78//! the VREngage system, supporting command-line arguments and configuration files.
80{
81public:
82 //! \brief Constructor using command-line arguments
83 //! \param argc Number of command-line arguments
84 //! \param argv Array of command-line argument strings
85 //! \param throwOnMissingField If true, throws exceptions for missing configuration fields
86 DtInitializer(int argc, char* argv[], bool throwOnMissingField = true);
87
88 //! \brief Constructor using a configuration file
89 //! \param configFile Path to the Lua configuration file to load
90 //! \param throwOnMissingField If true, throws exceptions for missing configuration fields
91 DtInitializer(DtFilename configFile, bool throwOnMissingField = true);
92
93 //! \brief Virtual destructor
94 virtual ~DtInitializer() override;
95
96 //! \brief Checks if a configuration field exists
97 //! \param name The name of the field to check
98 //! \param resolveDots If true, handles dotted notation for nested tables
99 //! \return True if the field exists, false otherwise
100 virtual bool hasField(const std::string& name, bool resolveDots = true);
101
102 //! \brief Retrieves a configuration value by name
103 //! \tparam RT The type to convert the found data to
104 //! \param name The name of the field to retrieve
105 //! \param resolveDots If true, handles dotted notation for nested tables
106 //! \return The value converted to the requested type
107 //! \throw std::string if the field is not found and throwOnMissingField is true
108 //! \note Supports basic types: bool, int, float, double, string, and tables as DtInitTable*
109 template <typename RT>
110 RT findData(const std::string& name, bool resolveDots = true);
111
112 template <typename RT>
113 RT findDataOr(const std::string& name, RT defaultValue, bool silent = false, bool resolveDots = true);
114
115 template <typename RT>
116 RT findData(int index);
117
118 //! \brief Gets the number of command-line arguments
119 //! \return The number of command-line arguments
120 int argc();
121
122 //! \brief Gets the array of command-line arguments
123 //! \return Pointer to the array of command-line argument strings
124 char** argv();
125
126 //! \brief Adds an additional configuration file
127 //! \param filename Path to the Lua configuration file to add
128 //! \return True if the file was loaded successfully, false otherwise
129 //!
130 //! This method can be called to load additional configuration files after initialization
131 virtual bool addConfigScript(std::string filename);
132
133 //! \brief Adds configuration from a string
134 //! \param str Lua code string containing configuration data
135 //! \return True if the string was executed successfully, false otherwise
136 //!
137 //! This method can be called to add configuration from a string after initialization
138 virtual bool addConfigString(std::string str);
139
140 //! \brief Reloads the configuration script
141 //! \return True if the script was reloaded successfully, false otherwise
142 //!
143 //! This method can be called to reload the configuration script after modifications
144 virtual bool loadConfigScript();
145
146protected:
147 //! \brief Initializes the 'import' function in Lua
148 //!
149 //! Sets up a Lua function to import other Lua scripts into the current state
151
152 //! \brief Creates and initializes the global table
153 //!
154 //! Creates a wrapper around the Lua global '_G' table for configuration access
156
157 //! \brief Path to the main configuration file
159
160 //! \brief Table wrapper for the Lua global environment
162
163 //! \brief Controls exception behavior for missing fields
165
166 //! \brief Number of command-line arguments
168
169 //! \brief Array of command-line argument strings
170 char** myArgv;
171};
172}; // namespace makVre
173
174template <typename RT>
175RT makVre::DtInitTable::findData(const std::string& name, bool resolveDots /* = true */)
176{
177 DtLuaStackCleaner cleaner(*myState);
178 if (getItemOnStack(name, resolveDots) == false)
179 {
180 std::string err = "Cannot find field: " + name;
181 LOG_WARN("Util") << err << std::endl;
182
183 if (myThrowOnMissingField == true)
184 {
185 throw(err);
186 }
187 }
188
190 return state.get<RT>(-1);
191}
192
193template <typename RT>
195{
196 DtASSERT(index > 0, "lua table indexes start at 1");
197
198 DtLuaStackCleaner cleaner(*myState);
199 if (getItemOnStack(index) == false)
200 {
201 std::string err = "Cannot find field: " + index;
202 LOG_WARN("Util") << err << std::endl;
203
204 if (myThrowOnMissingField == true)
205 {
206 throw(err);
207 }
208 }
209
211 return state.get<RT>(-1);
212}
213
214template <typename RT>
215RT makVre::DtInitTable::findDataOr(const std::string& name, RT defaultValue, bool silent, bool resolveDots /* = true */)
216{
217 if (hasField(name, resolveDots) == false)
218 {
219 if (silent == false)
220 {
221 LOG_VERBOSE("Util") << "Did not find requested field \"" << name << "\" using default value" << std::endl;
222 }
223
224 return defaultValue;
225 }
226
227 return findData<RT>(name, resolveDots);
228}
229
230template <typename RT>
232 const std::string& name, RT defaultValue, bool silent, bool resolveDots /* = true */)
233{
234 return myGlobalTable.findDataOr<RT>(name, defaultValue, silent, resolveDots);
235}
236
237template <typename RT>
238RT makVre::DtInitializer::findData(const std::string& name, bool resolveDots /* = true */)
239{
240 return myGlobalTable.findData<RT>(name, resolveDots);
241};
242
243template <typename RT>
245{
246 return myGlobalTable.findData<RT>(index);
247}
#define DtASSERT(exp, description)
Primary assertion macro for runtime validation.
Definition assert.h:53
Table-based access to Lua state for configuration data.
Definition initializer.h:38
bool myThrowOnMissingField
Controls exception behavior for missing fields.
Definition initializer.h:71
virtual void setThrowOnMissingField(bool set)
Sets whether to throw exceptions for missing fields.
virtual ~DtInitTable() override
Destructor.
DtInitTable(lua_State &state, int index, bool throwOnMissingField=true)
Constructor with existing Lua state and table.
DtInitTable()
Default constructor.
RT findDataOr(const std::string &name, RT defaultValue, bool silent=false, bool resolveDots=true)
Definition initializer.h:215
RT findData(const std::string &name, bool resolveDots=true)
Definition initializer.h:175
Configuration initializer for VREngage components.
Definition initializer.h:80
virtual bool addConfigString(std::string str)
Adds configuration from a string.
RT findDataOr(const std::string &name, RT defaultValue, bool silent=false, bool resolveDots=true)
Definition initializer.h:231
char ** argv()
Gets the array of command-line arguments.
int myArgc
Number of command-line arguments.
Definition initializer.h:167
bool myThrowOnMissingField
Controls exception behavior for missing fields.
Definition initializer.h:164
DtInitializer(int argc, char *argv[], bool throwOnMissingField=true)
Constructor using command-line arguments.
virtual bool hasField(const std::string &name, bool resolveDots=true)
Checks if a configuration field exists.
char ** myArgv
Array of command-line argument strings.
Definition initializer.h:170
virtual bool loadConfigScript()
Reloads the configuration script.
DtInitializer(DtFilename configFile, bool throwOnMissingField=true)
Constructor using a configuration file.
virtual bool addConfigScript(std::string filename)
Adds an additional configuration file.
void initImportFunction()
Initializes the 'import' function in Lua.
void createGlobalTable()
Creates and initializes the global table.
DtFilename myConfigFilename
Path to the main configuration file.
Definition initializer.h:158
DtInitTable myGlobalTable
Table wrapper for the Lua global environment.
Definition initializer.h:161
int argc()
Gets the number of command-line arguments.
virtual ~DtInitializer() override
Virtual destructor.
RT findData(const std::string &name, bool resolveDots=true)
Retrieves a configuration value by name.
Definition initializer.h:238
virtual lua_State * state()
Gets the Lua state associated with this object.
virtual bool hasField(const std::string &name, bool resolveDots=true)
Checks if the table has a field with the given name.
virtual bool getItemOnStack(const std::string &name, bool resolveDots)
Retrieves a named item from the table and pushes it onto the stack.
DtLuaObject()
Default constructor.
lua_State * myState
Pointer to the Lua state.
Definition luaObject.h:163
A utility class for automatically cleaning the Lua stack.
Definition luaStackCleaner.h:28
Class for managing a Lua state and providing high-level operations.
Definition luaState.h:43
DtLuaState(const DtLuaState &orig)
Copy constructor (not implemented)
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Provides logging functionality with various severity levels and channels.
#define LOG_WARN(channel)
Macro to log a warning message to log files.
Definition logger.h:69
#define LOG_VERBOSE(channel)
Macro to log a verbose message to log files.
Definition logger.h:79
Provides a RAII mechanism for automatically cleaning the Lua stack.
Provides an interface for interacting with a Lua state in VREngage.
Include export definitions for this library.
Definition glsVreMessageUtil.h:49