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 /// pushes a new Lunar object on the stack. 00203 /** \return A pointer to the Lunar object 00204 */ 00205 template<class T> 00206 T* push_new() 00207 { 00208 return Lunar<T>::push_new(pLua_); 00209 } 00210 00211 /// Sets the value of a global Lua variable. 00212 /** \param sName The name of this variable 00213 * \note The value taken is taken at the top of the stack, 00214 * and popped. 00215 */ 00216 void set_global(const std::string& sName); 00217 00218 /// Creates a new empty table and pushes it on the stack. 00219 void new_table(); 00220 00221 /// Iterates over the table at the given index. 00222 /** \param iIndex The index of the table to iterate on 00223 * \note Typical loop (with table at index i) :<br> 00224 * for (pLua->push_nil(); pLua->next(i); pLua->pop()) 00225 */ 00226 bool next(int iIndex = -2); 00227 00228 /// Removes the value at the top of the stack. 00229 /** \param uiNumber The number of value to remove 00230 */ 00231 void pop(uint uiNumber = 1); 00232 00233 /// Returns the value at the given index converted to a number. 00234 /** \param iIndex The index at which to search for the value 00235 * \return The value at the given index converted to a number 00236 */ 00237 double get_number(int iIndex = -1); 00238 00239 /// Returns the value at the given index converted to a bool. 00240 /** \param iIndex The index at which to search for the value 00241 * \return The value at the given index converted to a bool 00242 */ 00243 bool get_bool(int iIndex = -1); 00244 00245 /// Returns the value at the given index converted to a string. 00246 /** \param iIndex The index at which to search for the value 00247 * \return The value at the given index converted to a string 00248 */ 00249 std::string get_string(int iIndex = -1); 00250 00251 /// Returns the value at the given index. 00252 /** \param iIndex The index at which to search for the value 00253 * \return The value at the given index 00254 */ 00255 var get_value(int iIndex = -1); 00256 00257 /// Returns the Lunar object at the given index. 00258 /** \param iIndex The index at which to search for the value 00259 * \return The Lunar object at the given index 00260 */ 00261 template<class T> 00262 T* get(int iIndex = -1) const 00263 { 00264 return Lunar<T>::wide_check(pLua_, iIndex); 00265 } 00266 00267 /// Returns the number of value on the stack. 00268 /** \return The number of value on the stack 00269 */ 00270 uint get_top(); 00271 00272 /// Returns the type of the value on the stack. 00273 /** \param iIndex The index of the value to analyse 00274 * \return The type 00275 */ 00276 type get_type(int iIndex = -1); 00277 00278 /// Returns the name of a type. 00279 /** \param mType The type to serialize 00280 * \return The name of the type 00281 */ 00282 std::string get_type_name(type mType); 00283 00284 /// Puts a global variable on the stack. 00285 /** \param sName The name of the global variable 00286 * \note The name can contain tables, for example : 00287 * "MyTable.MyVariable" is a valid input. 00288 */ 00289 void get_global(const std::string& sName); 00290 00291 /// Reads an int from Lua. 00292 /** \param sName The name of the variable (global scope) 00293 * \param bCritical If 'true', an error will be printed if 00294 * the variable is not found. Else, it will 00295 * be assigned its default value 00296 * \param iDefaultValue The default value 00297 * \return The int value 00298 */ 00299 int get_global_int(const std::string& sName, bool bCritical = true, int iDefaultValue = 0); 00300 00301 /// Reads a float from Lua. 00302 /** \param sName The name of the variable (global scope) 00303 * \param bCritical If 'true', an error will be printed if 00304 * the variable is not found. Else, it will 00305 * be assigned its default value 00306 * \param dDefaultValue The default value 00307 * \return The float value 00308 */ 00309 double get_global_double(const std::string& sName, bool bCritical = true, double dDefaultValue = 0.0); 00310 00311 /// Wrapper to read a string from Lua. 00312 /** \param sName The name of the variable (global scope) 00313 * \param bCritical If 'true', an error will be printed if 00314 * the variable is not found. Else, it will 00315 * be assigned its default value 00316 * \param sDefaultValue The default value 00317 * \return The string value 00318 */ 00319 std::string get_global_string(const std::string& sName, bool bCritical = true, const std::string& sDefaultValue = ""); 00320 00321 /// Reads a bool from Lua. 00322 /** \param sName The name of the variable (global scope) 00323 * \param bCritical If 'true', an error will be printed if 00324 * the variable is not found. Else, it will 00325 * be assigned its default value 00326 * \param bDefaultValue The default value 00327 * \return The bool value 00328 */ 00329 bool get_global_bool(const std::string& sName, bool bCritical = true, bool bDefaultValue = false); 00330 00331 /// Puts a value from a Lua table on the stack. 00332 /** \param sName The name of the key associated to the value 00333 * \param iIndex The position of the table in the stack 00334 * \note Puts 'nil' if the key couldn't be found. 00335 */ 00336 void get_field(const std::string& sName, int iIndex = -1); 00337 00338 /// Puts a value from a Lua table on the stack. 00339 /** \param iID The id of the key associated to the value 00340 * \param iIndex The position of the table in the stack 00341 * \note Puts 'nil' if the key couldn't be found. 00342 */ 00343 void get_field(int iID, int iIndex = -1); 00344 00345 /// Reads an int from a Lua table. 00346 /** \param sName The name of the key associated to the value 00347 * \param bCritical If 'true', an error will be printed if 00348 * the variable is not found. Else, it will 00349 * be assigned its default value 00350 * \param iDefaultValue The default value 00351 * \param bSetValue If 'true' and the key wasn't found in the table, 00352 * this function will create that key in the Lua table 00353 * and assign it its default value 00354 * \return The int value 00355 * \note The table that will be used to read the value should be at the top of 00356 * the stack just before you call that function. 00357 */ 00358 int get_field_int(const std::string& sName, bool bCritical = true, int iDefaultValue = 0, bool bSetValue = false); 00359 00360 /// Reads a float from a Lua table. 00361 /** \param sName The name of the key associated to the value 00362 * \param bCritical If 'true', an error will be printed if 00363 * the variable is not found. Else, it will 00364 * be assigned its default value 00365 * \param dDefaultValue The default value 00366 * \param bSetValue If 'true' and the key wasn't found in the table, 00367 * this function will create that key in the Lua table 00368 * and assign it its default value 00369 * \return The float value 00370 * \note The table that will be used to read the value should be at the top of 00371 * the stack just before you call that function. 00372 */ 00373 double get_field_double(const std::string& sName, bool bCritical = true, double dDefaultValue = 0.0, bool bSetValue = false); 00374 00375 /// Reads a string from a Lua table. 00376 /** \param sName The name of the key associated to the value 00377 * \param bCritical If 'true', an error will be printed if 00378 * the variable is not found. Else, it will 00379 * be assigned its default value 00380 * \param sDefaultValue The default value 00381 * \param bSetValue If 'true' and the key wasn't found in the table, 00382 * this function will create that key in the Lua table 00383 * and assign it its default value 00384 * \return The string value 00385 * \note The table that will be used to read the value should be at the top of 00386 * the stack just before you call that function. 00387 */ 00388 std::string get_field_string(const std::string& sName, bool bCritical = true, const std::string& sDefaultValue = "", bool bSetValue = false); 00389 00390 /// Reads a bool from a Lua table. 00391 /** \param sName The name of the key associated to the value 00392 * \param bCritical If 'true', an error will be printed if 00393 * the variable is not found. Else, it will 00394 * be assigned its default value 00395 * \param bDefaultValue The default value 00396 * \param bSetValue If 'true' and the key wasn't found in the table, 00397 * this function will create that key in the Lua table 00398 * and assign it its default value 00399 * \return The bool value 00400 * \note The table that will be used to read the value should be at the top of 00401 * the stack just before you call that function. 00402 */ 00403 bool get_field_bool(const std::string& sName, bool bCritical = true, bool bDefaultValue = false, bool bSetValue = false); 00404 00405 /// Writes a value into a Lua table. 00406 /** \param sName The name of the key associated to the value 00407 * \note The value to put into the table must be at the top of the stack.<br> 00408 * The table must be at the index just before the value.<br> 00409 * pops the value from the stack. 00410 */ 00411 void set_field(const std::string& sName); 00412 00413 /// Writes a value into a Lua table. 00414 /** \param iID The ID of the key associated to the value 00415 * \note The value to put into the table must be at the top of the stack.<br> 00416 * The table must be at the index just before the value.<br> 00417 * pops the value from the stack. 00418 */ 00419 void set_field(int iID); 00420 00421 /// Writes an int into a Lua table. 00422 /** \param sName The name of the key associated to the value 00423 * \param iValue The value to set 00424 * \note The table that will be used to write the value should be at the top of 00425 * the stack just before you call that function. 00426 */ 00427 void set_field_int(const std::string& sName, int iValue); 00428 00429 /// Writes a float into a Lua table. 00430 /** \param sName The name of the key associated to the value 00431 * \param dValue The value to set 00432 * \note The table that will be used to write the value should be at the top of 00433 * the stack just before you call that function. 00434 */ 00435 void set_field_double(const std::string& sName, double dValue); 00436 00437 /// Writes a string into a Lua table. 00438 /** \param sName The name of the key associated to the value 00439 * \param sValue 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_string(const std::string& sName, const std::string& sValue); 00444 00445 /// Writes a bool into a Lua table. 00446 /** \param sName The name of the key associated to the value 00447 * \param bValue 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_bool(const std::string& sName, bool bValue); 00452 00453 /// Writes an int into a Lua table. 00454 /** \param iID The ID of the key associated to the value 00455 * \param iValue 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_int(int iID, int iValue); 00460 00461 /// Writes a float into a Lua table. 00462 /** \param iID The ID of the key associated to the value 00463 * \param dValue 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_double(int iID, double dValue); 00468 00469 /// Writes a string into a Lua table. 00470 /** \param iID The ID of the key associated to the value 00471 * \param sValue 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_string(int iID, const std::string& sValue); 00476 00477 /// Writes a bool into a Lua table. 00478 /** \param iID The ID of the key associated to the value 00479 * \param bValue 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_bool(int iID, bool bValue); 00484 00485 /// Changes the stack size. 00486 /** \param uiSize The new size of the stack 00487 * \note If the stack has more elements, they will be erased.<br> 00488 * If is has less, the stack will be filled with nil. 00489 */ 00490 void set_top(uint uiSize); 00491 00492 std::string sComString; 00493 00494 static state* get_state(lua_State* pLua); 00495 00496 private : 00497 00498 static std::map<lua_State*, state*> lLuaStateMap_; 00499 00500 lua_State* pLua_; 00501 00502 c_function pErrorFunction_; 00503 print_function pPrintFunction_; 00504 }; 00505 } 00506 00507 #endif