lua++
C:/Documents and Settings/Kalith/My Documents/Programmation/luapp/include/luapp_state.hpp
Go to the documentation of this file.
00001 #ifndef LUAPP_STATE_H
00002 #define LUAPP_STATE_H
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   conc_table(const std::string& sTable);
00056 
00057     /// Copy the content of a table from a 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.<br>
00126     *         The default implementation of this function returns :<br>
00127     *         [source]:line: error<br>
00128     *         See format_error().
00129     */
00130     void    set_lua_error_function(c_function pFunc);
00131 
00132     /// Sets a custom error printing function.
00133     /** \param pFunc The error printing function
00134     */
00135     void    set_print_error_function(print_function pFunc);
00136 
00137     /// Checks if a variable is serializable.
00138     /** \param iIndex The index on the stack of the variable
00139     *   \return 'true' for strings, numbers, booleans and tables.
00140     */
00141     bool  is_serializable(int iIndex = -1);
00142 
00143     /// Writes the content of a global variable in a string.
00144     /** \param sName The name of the global variable
00145     *   \return The content of the variable
00146     */
00147     std::string   serialize_global(const std::string& sName);
00148 
00149     /// Writes the content of a variable in a string.
00150     /** \param sTab   Number of space to put in front of each line
00151     *   \param iIndex The index on the stack of the variable
00152     *   \note Can only serialize strings, numbers, booleans and tables.
00153     *   \return The content of the variable
00154     */
00155     std::string   serialize(const std::string& sTab = "", int iIndex = -1);
00156 
00157     /// Puts a number on the stack.
00158     /** \param dValue The value to push on the stack (converted to float)
00159     */
00160     void    push_number(const double& dValue);
00161 
00162     /// Puts a boolean on the stack.
00163     /** \param bValue The value to push on the stack
00164     */
00165     void    push_bool(bool bValue);
00166 
00167     /// Puts a string on the stack.
00168     /** \param sValue The value to push on the stack
00169     */
00170     void    push_string(const std::string& sValue);
00171 
00172     /// Puts a value on the stack.
00173     /** \param vValue The value to push on the stack
00174     */
00175     void    push_var(const var& vValue);
00176 
00177     /// pushes a copy of the value at the given index on the stack.
00178     /** \param iIndex The index of the value to push
00179     */
00180     void    pushValue(int iIndex);
00181 
00182     /// Puts "nil" (null) on the stack.
00183     /** \param uiNumber The number of "nil" to push
00184     */
00185     void    push_nil(uint uiNumber = 1);
00186 
00187     /// Puts the value of a global Lua variable on the stack.
00188     /** \param sName The name of this variable
00189     */
00190     void    push_global(const std::string& sName);
00191 
00192     /// pushes a new Lunar object on the stack.
00193     /** \return A pointer to the Lunar object
00194     */
00195     template<class T>
00196     T* push_new()
00197     {
00198         return Lunar<T>::push_new(pLua_);
00199     }
00200 
00201     /// Sets the value of a global Lua variable.
00202     /** \param sName The name of this variable
00203     *   \note The value taken is taken at the top of the stack,
00204     *         and popped.
00205     */
00206     void    set_global(const std::string& sName);
00207 
00208     /// Creates a new empty table and pushes it on the stack.
00209     void    new_table();
00210 
00211     /// Iterates over the table at the given index.
00212     /** \param iIndex The index of the table to iterate on
00213     *   \note Typical loop (with table at index i) :<br>
00214     *         for (pLua->push_nil(); pLua->next(i); pLua->pop())
00215     */
00216     bool    next(int iIndex = -2);
00217 
00218     /// Removes the value at the top of the stack.
00219     /** \param uiNumber The number of value to remove
00220     */
00221     void    pop(uint uiNumber = 1);
00222 
00223     /// Returns the value at the given index converted to a number.
00224     /** \param iIndex The index at which to search for the value
00225     *   \return The value at the given index converted to a number
00226     */
00227     double get_number(int iIndex = -1);
00228 
00229     /// Returns the value at the given index converted to a bool.
00230     /** \param iIndex The index at which to search for the value
00231     *   \return The value at the given index converted to a bool
00232     */
00233     bool  get_bool(int iIndex = -1);
00234 
00235     /// Returns the value at the given index converted to a string.
00236     /** \param iIndex The index at which to search for the value
00237     *   \return The value at the given index converted to a string
00238     */
00239     std::string   get_string(int iIndex = -1);
00240 
00241     /// Returns the value at the given index.
00242     /** \param iIndex The index at which to search for the value
00243     *   \return The value at the given index
00244     */
00245     var   get_value(int iIndex = -1);
00246 
00247     /// Returns the Lunar object at the given index.
00248     /** \param iIndex The index at which to search for the value
00249     *   \return The Lunar object at the given index
00250     */
00251     template<class T>
00252     T* get(int iIndex = -1) const
00253     {
00254         return Lunar<T>::wide_check(pLua_, iIndex);
00255     }
00256 
00257     /// Returns the number of value on the stack.
00258     /** \return The number of value on the stack
00259     */
00260     uint  get_top();
00261 
00262     /// Returns the type of the value on the stack.
00263     /** \param iIndex The index of the value to analyse
00264     *   \return The type
00265     */
00266     type    get_type(int iIndex = -1);
00267 
00268     /// Returns the name of a type.
00269     /** \param mType The type to serialize
00270     *   \return The name of the type
00271     */
00272     std::string   get_type_name(type mType);
00273 
00274     /// Puts a global variable on the stack.
00275     /** \param sName The name of the global variable
00276     *   \note The name can contain tables, for example :
00277     *         "MyTable.MyVariable" is a valid input.
00278     */
00279     void    get_global(const std::string& sName);
00280 
00281     /// Reads an int from Lua.
00282     /** \param sName         The name of the variable (global scope)
00283     *   \param bCritical     If 'true', an error will be printed if
00284     *                        the variable is not found. Else, it will
00285     *                        be assigned its default value
00286     *   \param iDefaultValue The default value
00287     *   \return The int value
00288     */
00289     int   get_global_int(const std::string& sName, bool bCritical = true, int iDefaultValue = 0);
00290 
00291     /// Reads a float 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 dDefaultValue The default value
00297     *   \return The float value
00298     */
00299     double get_global_double(const std::string& sName, bool bCritical = true, double dDefaultValue = 0.0);
00300 
00301     /// Wrapper to read a string 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 sDefaultValue The default value
00307     *   \return The string value
00308     */
00309     std::string   get_global_string(const std::string& sName, bool bCritical = true, const std::string& sDefaultValue = "");
00310 
00311     /// Reads a bool 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 bDefaultValue The default value
00317     *   \return The bool value
00318     */
00319     bool  get_global_bool(const std::string& sName, bool bCritical = true, bool bDefaultValue = false);
00320 
00321     /// Puts a value from a Lua table on the stack.
00322     /** \param sName  The name of the key associated to the value
00323     *   \param iIndex The position of the table in the stack
00324     *   \note Puts 'nil' if the key couldn't be found.
00325     */
00326     void    get_field(const std::string& sName, int iIndex = -1);
00327 
00328     /// Puts a value from a Lua table on the stack.
00329     /** \param iID    The id of the key associated to the value
00330     *   \param iIndex The position of the table in the stack
00331     *   \note Puts 'nil' if the key couldn't be found.
00332     */
00333     void    get_field(int iID, int iIndex = -1);
00334 
00335     /// Reads an int from a Lua table.
00336     /** \param sName         The name of the key associated to the value
00337     *   \param bCritical     If 'true', an error will be printed if
00338     *                        the variable is not found. Else, it will
00339     *                        be assigned its default value
00340     *   \param iDefaultValue The default value
00341     *   \param bSetValue     If 'true' and the key wasn't found in the table,
00342     *                        this function will create that key in the Lua table
00343     *                        and assign it its default value
00344     *   \return The int value
00345     *   \note The table that will be used to read the value should be at the top of
00346     *         the stack just before you call that function.
00347     */
00348     int   get_field_int(const std::string& sName, bool bCritical = true, int iDefaultValue = 0, bool bSetValue = false);
00349 
00350     /// Reads a float from a Lua table.
00351     /** \param sName         The name of the key associated to the value
00352     *   \param bCritical     If 'true', an error will be printed if
00353     *                        the variable is not found. Else, it will
00354     *                        be assigned its default value
00355     *   \param dDefaultValue The default value
00356     *   \param bSetValue     If 'true' and the key wasn't found in the table,
00357     *                        this function will create that key in the Lua table
00358     *                        and assign it its default value
00359     *   \return The float value
00360     *   \note The table that will be used to read the value should be at the top of
00361     *         the stack just before you call that function.
00362     */
00363     double get_field_double(const std::string& sName, bool bCritical = true, double dDefaultValue = 0.0, bool bSetValue = false);
00364 
00365     /// Reads a string from a Lua table.
00366     /** \param sName         The name of the key associated to the value
00367     *   \param bCritical     If 'true', an error will be printed if
00368     *                        the variable is not found. Else, it will
00369     *                        be assigned its default value
00370     *   \param sDefaultValue The default value
00371     *   \param bSetValue     If 'true' and the key wasn't found in the table,
00372     *                        this function will create that key in the Lua table
00373     *                        and assign it its default value
00374     *   \return The string value
00375     *   \note The table that will be used to read the value should be at the top of
00376     *         the stack just before you call that function.
00377     */
00378     std::string   get_field_string(const std::string& sName, bool bCritical = true, const std::string& sDefaultValue = "", bool bSetValue = false);
00379 
00380     /// Reads a bool from a Lua table.
00381     /** \param sName         The name of the key associated to the value
00382     *   \param bCritical     If 'true', an error will be printed if
00383     *                        the variable is not found. Else, it will
00384     *                        be assigned its default value
00385     *   \param bDefaultValue The default value
00386     *   \param bSetValue     If 'true' and the key wasn't found in the table,
00387     *                        this function will create that key in the Lua table
00388     *                        and assign it its default value
00389     *   \return The bool value
00390     *   \note The table that will be used to read the value should be at the top of
00391     *         the stack just before you call that function.
00392     */
00393     bool  get_field_bool(const std::string& sName, bool bCritical = true, bool bDefaultValue = false, bool bSetValue = false);
00394 
00395     /// Writes a value into a Lua table.
00396     /** \param sName The name of the key associated to the value
00397     *   \note The value to put into the table must be at the top of the stack.<br>
00398     *         The table must be at the index just before the value.<br>
00399     *         pops the value from the stack.
00400     */
00401     void    set_field(const std::string& sName);
00402 
00403     /// Writes a value into a Lua table.
00404     /** \param iID The ID of the key associated to the value
00405     *   \note The value to put into the table must be at the top of the stack.<br>
00406     *         The table must be at the index just before the value.<br>
00407     *         pops the value from the stack.
00408     */
00409     void    set_field(int iID);
00410 
00411     /// Writes an int into a Lua table.
00412     /** \param sName  The name of the key associated to the value
00413     *   \param iValue The value to set
00414     *   \note The table that will be used to write the value should be at the top of
00415     *         the stack just before you call that function.
00416     */
00417     void    set_field_int(const std::string& sName, int iValue);
00418 
00419     /// Writes a float into a Lua table.
00420     /** \param sName  The name of the key associated to the value
00421     *   \param dValue The value to set
00422     *   \note The table that will be used to write the value should be at the top of
00423     *         the stack just before you call that function.
00424     */
00425     void    set_field_double(const std::string& sName, double dValue);
00426 
00427     /// Writes a string into a Lua table.
00428     /** \param sName  The name of the key associated to the value
00429     *   \param sValue The value to set
00430     *   \note The table that will be used to write the value should be at the top of
00431     *         the stack just before you call that function.
00432     */
00433     void    set_field_string(const std::string& sName, const std::string& sValue);
00434 
00435     /// Writes a bool into a Lua table.
00436     /** \param sName  The name of the key associated to the value
00437     *   \param bValue The value to set
00438     *   \note The table that will be used to write the value should be at the top of
00439     *         the stack just before you call that function.
00440     */
00441     void    set_field_bool(const std::string& sName, bool bValue);
00442 
00443     /// Writes an int into a Lua table.
00444     /** \param iID    The ID of the key associated to the value
00445     *   \param iValue The value to set
00446     *   \note The table that will be used to write the value should be at the top of
00447     *         the stack just before you call that function.
00448     */
00449     void    set_field_int(int iID, int iValue);
00450 
00451     /// Writes a float into a Lua table.
00452     /** \param iID    The ID of the key associated to the value
00453     *   \param dValue The value to set
00454     *   \note The table that will be used to write the value should be at the top of
00455     *         the stack just before you call that function.
00456     */
00457     void    set_field_double(int iID, double dValue);
00458 
00459     /// Writes a string into a Lua table.
00460     /** \param iID    The ID of the key associated to the value
00461     *   \param sValue The value to set
00462     *   \note The table that will be used to write the value should be at the top of
00463     *         the stack just before you call that function.
00464     */
00465     void    set_field_string(int iID, const std::string& sValue);
00466 
00467     /// Writes a bool into a Lua table.
00468     /** \param iID    The ID of the key associated to the value
00469     *   \param bValue The value to set
00470     *   \note The table that will be used to write the value should be at the top of
00471     *         the stack just before you call that function.
00472     */
00473     void    set_field_bool(int iID, bool bValue);
00474 
00475     /// Changes the stack size.
00476     /** \param uiSize The new size of the stack
00477     *   \note If the stack has more elements, they will be erased.<br>
00478     *         If is has less, the stack will be filled with nil.
00479     */
00480     void    set_top(uint uiSize);
00481 
00482     std::string sComString;
00483 
00484     static state* get_state(lua_State* pLua);
00485 
00486 private :
00487 
00488     static std::map<lua_State*, state*> lLuaStateMap_;
00489 
00490     lua_State* pLua_;
00491 
00492     c_function pErrorFunction_;
00493     print_function pPrintFunction_;
00494 };
00495 }
00496 
00497 #endif