00001 #ifndef LUAPP_STATE_HPP 00002 #define LUAPP_STATE_HPP 00003 00004 #include "lunar.hpp" 00005 #include <utils.hpp> 00006 #include <vector> 00007 #include <map> 00008 00009 namespace lua 00010 { 00011 class var; 00012 00013 enum type 00014 { 00015 TYPE_NONE, 00016 TYPE_NIL, 00017 TYPE_NUMBER, 00018 TYPE_BOOLEAN, 00019 TYPE_STRING, 00020 TYPE_FUNCTION, 00021 TYPE_TABLE, 00022 TYPE_THREAD, 00023 TYPE_LIGHTUSERDATA, 00024 TYPE_USERDATA 00025 }; 00026 00027 typedef int (*c_function) (lua_State *L); 00028 typedef void (*print_function) (const std::string& s); 00029 00030 /// Lua wrapper 00031 /** Wraps the Lua api into a single class. 00032 */ 00033 class state 00034 { 00035 public : 00036 00037 /// Constructor 00038 /** \note Opens a new Lua state and registers all base functions. 00039 */ 00040 state(); 00041 00042 /// Destructor 00043 /** \note Definately closes the associated Lua state (all data will be lost). 00044 */ 00045 ~state(); 00046 00047 /// Return the associated Lua state. 00048 /** \return The associated Lua state 00049 */ 00050 lua_State* get_state(); 00051 00052 /// Concatenates a Lua table into a string. 00053 /** \param sTable The name of the table in Lua 00054 */ 00055 std::string table_to_string(const std::string& sTable); 00056 00057 /// Copy the content of a table from one Lua state to another. 00058 /** \param mLua The other Lua state into wich the table will be copied 00059 * \param sSrcName The name of the table in the source state 00060 * \param sDestName The name of the table in the destination state 00061 * \note If sDestName is ommited, the table will have the same name in both Lua states. 00062 */ 00063 void copy_table(state& mLua, const std::string& sSrcName, const std::string& sDestName = ""); 00064 00065 /// Executes a Lua file. 00066 /** \param sFile The name of the file 00067 * \note This function wil throw an exception if any error occurs. 00068 * Don't forget to catch them. 00069 */ 00070 void do_file(const std::string& sFile); 00071 00072 /// Executes a string containing Lua instructions. 00073 /** \param sStr The string to execute 00074 * \note This function wil throw an exception if any error occurs. 00075 * Don't forget to catch them. 00076 */ 00077 void do_string(const std::string& sStr); 00078 00079 /// Executes a Lua function. 00080 /** \param sFunctionName The name of the function to execute 00081 * \note This function wil throw an exception if any error occurs. 00082 * Don't forget to catch them. 00083 */ 00084 void call_function(const std::string& sFunctionName); 00085 00086 /// Executes a Lua function with arguments. 00087 /** \param sFunctionName The name of the function to execute 00088 * \param lArgumentStack The agument stack (order matters) 00089 * \note This function wil throw an exception if any error occurs. 00090 * Don't forget to catch them. 00091 */ 00092 void call_function(const std::string& sFunctionName, const std::vector<var>& lArgumentStack); 00093 00094 /// Binds a C++ function to a Lua function. 00095 /** \param sFunctionName The name of the Lua function 00096 * \param mFunction The C++ function to bind 00097 */ 00098 void reg(const std::string& sFunctionName, c_function mFunction); 00099 00100 /// regs a Lunar class to be used on this state. 00101 template<class T> 00102 void reg() 00103 { 00104 Lunar<T>::reg(pLua_); 00105 } 00106 00107 /// Prints an error string in the log file with the Lua tag. 00108 /** \param sError The error string to output 00109 * \note This function will print out the current file and line 00110 */ 00111 void print_error(const std::string& sError); 00112 00113 /// Formats a raw error string. 00114 /** \param sError The raw error string 00115 * \note Calls the custom error function if provided, 00116 * else formats as :<br> 00117 * [source]:line: error 00118 */ 00119 std::string format_error(const std::string& sError); 00120 00121 /// Sets a custom error formatting function. 00122 /** \param pFunc The error function 00123 * \note The string that is passed to this error function only 00124 * contains the raw error message from Lua : no indication 00125 * on the location of the error. 00126 * \note The default implementation of this function returns :<br> 00127 * [source]:line: error<br> 00128 * \note See format_error(). 00129 */ 00130 void set_lua_error_function(c_function pFunc); 00131 00132 /// Returns the current error formatting function. 00133 /** \return The current error formatting function 00134 * \note See set_lua_error_function() for more information. 00135 * \note This method is mostly usefull if you want to save the 00136 * current error function, use another one temporarily, 00137 * and restore the previous one. 00138 */ 00139 c_function get_lua_error_function() const; 00140 00141 /// Sets a custom error printing function. 00142 /** \param pFunc The error printing function 00143 * \note By default, errors are sent through std::cerr. 00144 */ 00145 void set_print_error_function(print_function pFunc); 00146 00147 /// Checks if a variable is serializable. 00148 /** \param iIndex The index on the stack of the variable 00149 * \return 'true' for strings, numbers, booleans and tables. 00150 */ 00151 bool is_serializable(int iIndex = -1); 00152 00153 /// Writes the content of a global variable in a string. 00154 /** \param sName The name of the global variable 00155 * \return The content of the variable 00156 */ 00157 std::string serialize_global(const std::string& sName); 00158 00159 /// Writes the content of a variable in a string. 00160 /** \param sTab Number of space to put in front of each line 00161 * \param iIndex The index on the stack of the variable 00162 * \note Can only serialize strings, numbers, booleans and tables. 00163 * \return The content of the variable 00164 */ 00165 std::string serialize(const std::string& sTab = "", int iIndex = -1); 00166 00167 /// Puts a number on the stack. 00168 /** \param dValue The value to push on the stack (converted to float) 00169 */ 00170 void push_number(const double& dValue); 00171 00172 /// Puts a boolean on the stack. 00173 /** \param bValue The value to push on the stack 00174 */ 00175 void push_bool(bool bValue); 00176 00177 /// Puts a string on the stack. 00178 /** \param sValue The value to push on the stack 00179 */ 00180 void push_string(const std::string& sValue); 00181 00182 /// Puts a value on the stack. 00183 /** \param vValue The value to push on the stack 00184 */ 00185 void push(const var& vValue); 00186 00187 /// pushes a copy of the value at the given index on the stack. 00188 /** \param iIndex The index of the value to push 00189 */ 00190 void push_value(int iIndex); 00191 00192 /// Puts "nil" (null) on the stack. 00193 /** \param uiNumber The number of "nil" to push 00194 */ 00195 void push_nil(uint uiNumber = 1); 00196 00197 /// Puts the value of a global Lua variable on the stack. 00198 /** \param sName The name of this variable 00199 */ 00200 void push_global(const std::string& sName); 00201 00202 /// Puts a user data (C++ pointer) on the stack. 00203 /** \param pData The pointer to put on the stack 00204 */ 00205 template<class T> 00206 void push_userdata(T* pData) 00207 { 00208 lua_pushlightuserdata(pLua_, static_cast<void*>(pData)); 00209 } 00210 00211 /// pushes a new Lunar object on the stack. 00212 /** \return A pointer to the Lunar object 00213 */ 00214 template<class T> 00215 T* push_new() 00216 { 00217 return Lunar<T>::push_new(pLua_); 00218 } 00219 00220 /// Sets the value of a global Lua variable. 00221 /** \param sName The name of this variable 00222 * \note The value taken is taken at the top of the stack, 00223 * and popped. 00224 */ 00225 void set_global(const std::string& sName); 00226 00227 /// Creates a new empty table and pushes it on the stack. 00228 void new_table(); 00229 00230 /// Iterates over the table at the given index. 00231 /** \param iIndex The index of the table to iterate on 00232 * \note Typical loop (with table at index i) :<br> 00233 * for (pLua->push_nil(); pLua->next(i); pLua->pop()) 00234 */ 00235 bool next(int iIndex = -2); 00236 00237 /// Removes the value at the top of the stack. 00238 /** \param uiNumber The number of value to remove 00239 */ 00240 void pop(uint uiNumber = 1); 00241 00242 /// Returns the value at the given index converted to a number. 00243 /** \param iIndex The index at which to search for the value 00244 * \return The value at the given index converted to a number 00245 */ 00246 double get_number(int iIndex = -1); 00247 00248 /// Returns the value at the given index converted to a bool. 00249 /** \param iIndex The index at which to search for the value 00250 * \return The value at the given index converted to a bool 00251 */ 00252 bool get_bool(int iIndex = -1); 00253 00254 /// Returns the value at the given index converted to a string. 00255 /** \param iIndex The index at which to search for the value 00256 * \return The value at the given index converted to a string 00257 */ 00258 std::string get_string(int iIndex = -1); 00259 00260 /// Returns the value at the given index. 00261 /** \param iIndex The index at which to search for the value 00262 * \return The value at the given index 00263 */ 00264 var get_value(int iIndex = -1); 00265 00266 /// Returns the user data (C++ pointer) at the given index. 00267 template<class T> 00268 T* get_userdata(int iIndex = -1) 00269 { 00270 return static_cast<T*>(lua_touserdata(pLua_, iIndex)); 00271 } 00272 00273 /// Returns the Lunar object at the given index. 00274 /** \param iIndex The index at which to search for the value 00275 * \return The Lunar object at the given index 00276 */ 00277 template<class T> 00278 T* get(int iIndex = -1) const 00279 { 00280 return Lunar<T>::wide_check(pLua_, iIndex); 00281 } 00282 00283 /// Returns the number of value on the stack. 00284 /** \return The number of value on the stack 00285 */ 00286 uint get_top(); 00287 00288 /// Returns the type of the value on the stack. 00289 /** \param iIndex The index of the value to analyse 00290 * \return The type 00291 */ 00292 type get_type(int iIndex = -1); 00293 00294 /// Returns the name of a type. 00295 /** \param mType The type to serialize 00296 * \return The name of the type 00297 */ 00298 std::string get_type_name(type mType); 00299 00300 /// Puts a global variable on the stack. 00301 /** \param sName The name of the global variable 00302 * \note The name can contain tables, for example : 00303 * "MyTable.MyVariable" is a valid input. 00304 */ 00305 void get_global(const std::string& sName); 00306 00307 /// Reads an int from Lua. 00308 /** \param sName The name of the variable (global scope) 00309 * \param bCritical If 'true', an error will be printed if 00310 * the variable is not found. Else, it will 00311 * be assigned its default value 00312 * \param iDefaultValue The default value 00313 * \return The int value 00314 */ 00315 int get_global_int(const std::string& sName, bool bCritical = true, int iDefaultValue = 0); 00316 00317 /// Reads a float from Lua. 00318 /** \param sName The name of the variable (global scope) 00319 * \param bCritical If 'true', an error will be printed if 00320 * the variable is not found. Else, it will 00321 * be assigned its default value 00322 * \param dDefaultValue The default value 00323 * \return The float value 00324 */ 00325 double get_global_double(const std::string& sName, bool bCritical = true, double dDefaultValue = 0.0); 00326 00327 /// Wrapper to read a string from Lua. 00328 /** \param sName The name of the variable (global scope) 00329 * \param bCritical If 'true', an error will be printed if 00330 * the variable is not found. Else, it will 00331 * be assigned its default value 00332 * \param sDefaultValue The default value 00333 * \return The string value 00334 */ 00335 std::string get_global_string(const std::string& sName, bool bCritical = true, const std::string& sDefaultValue = ""); 00336 00337 /// Reads a bool from Lua. 00338 /** \param sName The name of the variable (global scope) 00339 * \param bCritical If 'true', an error will be printed if 00340 * the variable is not found. Else, it will 00341 * be assigned its default value 00342 * \param bDefaultValue The default value 00343 * \return The bool value 00344 */ 00345 bool get_global_bool(const std::string& sName, bool bCritical = true, bool bDefaultValue = false); 00346 00347 /// Puts a value from a Lua table on the stack. 00348 /** \param sName The name of the key associated to the value 00349 * \param iIndex The position of the table in the stack 00350 * \note Puts 'nil' if the key couldn't be found. 00351 */ 00352 void get_field(const std::string& sName, int iIndex = -1); 00353 00354 /// Puts a value from a Lua table on the stack. 00355 /** \param iID The id of the key associated to the value 00356 * \param iIndex The position of the table in the stack 00357 * \note Puts 'nil' if the key couldn't be found. 00358 */ 00359 void get_field(int iID, int iIndex = -1); 00360 00361 /// Reads an int from a Lua table. 00362 /** \param sName The name of the key associated to the value 00363 * \param bCritical If 'true', an error will be printed if 00364 * the variable is not found. Else, it will 00365 * be assigned its default value 00366 * \param iDefaultValue The default value 00367 * \param bSetValue If 'true' and the key wasn't found in the table, 00368 * this function will create that key in the Lua table 00369 * and assign it its default value 00370 * \return The int value 00371 * \note The table that will be used to read the value should be at the top of 00372 * the stack just before you call that function. 00373 */ 00374 int get_field_int(const std::string& sName, bool bCritical = true, int iDefaultValue = 0, bool bSetValue = false); 00375 00376 /// Reads a float from a Lua table. 00377 /** \param sName The name of the key associated to the value 00378 * \param bCritical If 'true', an error will be printed if 00379 * the variable is not found. Else, it will 00380 * be assigned its default value 00381 * \param dDefaultValue The default value 00382 * \param bSetValue If 'true' and the key wasn't found in the table, 00383 * this function will create that key in the Lua table 00384 * and assign it its default value 00385 * \return The float value 00386 * \note The table that will be used to read the value should be at the top of 00387 * the stack just before you call that function. 00388 */ 00389 double get_field_double(const std::string& sName, bool bCritical = true, double dDefaultValue = 0.0, bool bSetValue = false); 00390 00391 /// Reads a string from a Lua table. 00392 /** \param sName The name of the key associated to the value 00393 * \param bCritical If 'true', an error will be printed if 00394 * the variable is not found. Else, it will 00395 * be assigned its default value 00396 * \param sDefaultValue The default value 00397 * \param bSetValue If 'true' and the key wasn't found in the table, 00398 * this function will create that key in the Lua table 00399 * and assign it its default value 00400 * \return The string value 00401 * \note The table that will be used to read the value should be at the top of 00402 * the stack just before you call that function. 00403 */ 00404 std::string get_field_string(const std::string& sName, bool bCritical = true, const std::string& sDefaultValue = "", bool bSetValue = false); 00405 00406 /// Reads a bool from a Lua table. 00407 /** \param sName The name of the key associated to the value 00408 * \param bCritical If 'true', an error will be printed if 00409 * the variable is not found. Else, it will 00410 * be assigned its default value 00411 * \param bDefaultValue The default value 00412 * \param bSetValue If 'true' and the key wasn't found in the table, 00413 * this function will create that key in the Lua table 00414 * and assign it its default value 00415 * \return The bool value 00416 * \note The table that will be used to read the value should be at the top of 00417 * the stack just before you call that function. 00418 */ 00419 bool get_field_bool(const std::string& sName, bool bCritical = true, bool bDefaultValue = false, bool bSetValue = false); 00420 00421 /// Writes a value into a Lua table. 00422 /** \param sName The name of the key associated to the value 00423 * \note The value to put into the table must be at the top of the stack.<br> 00424 * The table must be at the index just before the value.<br> 00425 * pops the value from the stack. 00426 */ 00427 void set_field(const std::string& sName); 00428 00429 /// Writes a value into a Lua table. 00430 /** \param iID The ID of the key associated to the value 00431 * \note The value to put into the table must be at the top of the stack.<br> 00432 * The table must be at the index just before the value.<br> 00433 * pops the value from the stack. 00434 */ 00435 void set_field(int iID); 00436 00437 /// Writes an int into a Lua table. 00438 /** \param sName The name of the key associated to the value 00439 * \param iValue The value to set 00440 * \note The table that will be used to write the value should be at the top of 00441 * the stack just before you call that function. 00442 */ 00443 void set_field_int(const std::string& sName, int iValue); 00444 00445 /// Writes a float into a Lua table. 00446 /** \param sName The name of the key associated to the value 00447 * \param dValue The value to set 00448 * \note The table that will be used to write the value should be at the top of 00449 * the stack just before you call that function. 00450 */ 00451 void set_field_double(const std::string& sName, double dValue); 00452 00453 /// Writes a string into a Lua table. 00454 /** \param sName The name of the key associated to the value 00455 * \param sValue The value to set 00456 * \note The table that will be used to write the value should be at the top of 00457 * the stack just before you call that function. 00458 */ 00459 void set_field_string(const std::string& sName, const std::string& sValue); 00460 00461 /// Writes a bool into a Lua table. 00462 /** \param sName The name of the key associated to the value 00463 * \param bValue The value to set 00464 * \note The table that will be used to write the value should be at the top of 00465 * the stack just before you call that function. 00466 */ 00467 void set_field_bool(const std::string& sName, bool bValue); 00468 00469 /// Writes an int into a Lua table. 00470 /** \param iID The ID of the key associated to the value 00471 * \param iValue The value to set 00472 * \note The table that will be used to write the value should be at the top of 00473 * the stack just before you call that function. 00474 */ 00475 void set_field_int(int iID, int iValue); 00476 00477 /// Writes a float into a Lua table. 00478 /** \param iID The ID of the key associated to the value 00479 * \param dValue The value to set 00480 * \note The table that will be used to write the value should be at the top of 00481 * the stack just before you call that function. 00482 */ 00483 void set_field_double(int iID, double dValue); 00484 00485 /// Writes a string into a Lua table. 00486 /** \param iID The ID of the key associated to the value 00487 * \param sValue The value to set 00488 * \note The table that will be used to write the value should be at the top of 00489 * the stack just before you call that function. 00490 */ 00491 void set_field_string(int iID, const std::string& sValue); 00492 00493 /// Writes a bool into a Lua table. 00494 /** \param iID The ID of the key associated to the value 00495 * \param bValue The value to set 00496 * \note The table that will be used to write the value should be at the top of 00497 * the stack just before you call that function. 00498 */ 00499 void set_field_bool(int iID, bool bValue); 00500 00501 /// Changes the stack size. 00502 /** \param uiSize The new size of the stack 00503 * \note If the stack has more elements, they will be erased.<br> 00504 * If is has less, the stack will be filled with nil. 00505 */ 00506 void set_top(uint uiSize); 00507 00508 std::string sComString; 00509 00510 static state* get_state(lua_State* pLua); 00511 00512 private : 00513 00514 state(const state&); 00515 state& operator = (const state&); 00516 00517 static std::map<lua_State*, state*> lLuaStateMap_; 00518 00519 lua_State* pLua_; 00520 00521 c_function pErrorFunction_; 00522 print_function pPrintFunction_; 00523 }; 00524 } 00525 00526 #endif