Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

LuaFunctions.h

Go to the documentation of this file.
00001 #ifndef LUA_FUNCTIONS_H 00002 #define LUA_FUNCTIONS_H 00003 00004 #include "LuaManager.h" 00005 #include "RageUtil.h" /* for ssprintf */ 00006 00007 extern "C" 00008 { 00009 #include <lua.h> 00010 #include <lualib.h> 00011 } 00012 00013 /* Argument helpers: */ 00014 #define LUA_ASSERT( expr, err ) if( !(expr) ) { LUA->Fail( err ); } 00015 00016 /* Require exactly "need" arguments. */ 00017 #define REQ_ARGS(func, need) { \ 00018 const int args = lua_gettop(L); \ 00019 LUA_ASSERT( args == need, ssprintf( func " requires exactly %i argument%s, got %i", need, need == 1? "":"s", args) ); \ 00020 } 00021 00022 /* Require between minargs and maxargs arguments. */ 00023 #define REQ_ARGS_BETWEEN(func, minargs, maxargs) { \ 00024 const int args = lua_gettop(L); \ 00025 LUA_ASSERT( args < minargs, ssprintf( func " requires between %i and %i argument%s, got %i", \ 00026 minargs, maxargs, maxargs == 1? "":"s", args) ); \ 00027 } 00028 00029 /* argument n must be of type "type" */ 00030 #define REQ_ARG(func, n, type) { \ 00031 LUA_ASSERT( lua_is##type(L, n), ssprintf("Argument %i to " func " must be %s", n, #type) ); } 00032 /* argument n must be a number between minimum...maximum */ 00033 #define REQ_ARG_NUMBER_RANGE(func, n, minimum, maximum) { \ 00034 REQ_ARG(func, n, number); \ 00035 const int val = (int) lua_tonumber( L, n ); \ 00036 LUA_ASSERT( val >= minimum && val <= maximum, ssprintf("Argument %i to " func " must be an integer between %i and %i (got %i)", n, minimum, maximum, val) ); \ 00037 } 00038 #define LUA_RETURN( expr ) { LUA->PushStack( expr ); return 1; } 00039 00040 /* Helpers to create common functions: */ 00041 /* Functions that take no arguments: */ 00042 #define LuaFunction_NoArgs( func, call ) \ 00043 int LuaFunc_##func( lua_State *L ) { \ 00044 REQ_ARGS( #func, 0 ); \ 00045 LUA_RETURN( call ); \ 00046 } \ 00047 LuaFunction( func ); /* register it */ 00048 00049 #define LuaFunction_Int( func, call ) \ 00050 int LuaFunc_##func( lua_State *L ) { \ 00051 REQ_ARGS( #func, 1 ); \ 00052 REQ_ARG( #func, 1, number ); \ 00053 const int a1 = int(lua_tonumber( L, 1 )); \ 00054 LUA_RETURN( call ); \ 00055 } \ 00056 LuaFunction( func ); /* register it */ 00057 00058 #define LuaFunction_IntInt( func, call ) \ 00059 int LuaFunc_##func( lua_State *L ) { \ 00060 REQ_ARGS( #func, 2 ); \ 00061 REQ_ARG( #func, 1, number ); \ 00062 REQ_ARG( #func, 2, number ); \ 00063 const int a1 = int(lua_tonumber( L, 1 )); \ 00064 const int a2 = int(lua_tonumber( L, 2 )); \ 00065 LUA_RETURN( call ); \ 00066 } \ 00067 LuaFunction( func ); /* register it */ 00068 00069 #define LuaFunction_Str( func, call ) \ 00070 int LuaFunc_##func( lua_State *L ) { \ 00071 REQ_ARGS( #func, 1 ); \ 00072 REQ_ARG( #func, 1, string ); \ 00073 CString str; \ 00074 LUA->PopStack( str ); \ 00075 LUA_RETURN( call ); \ 00076 } \ 00077 LuaFunction( func ); /* register it */ 00078 00079 #define LuaFunction_StrStr( func, call ) \ 00080 int LuaFunc_##func( lua_State *L ) { \ 00081 REQ_ARGS( #func, 2 ); \ 00082 REQ_ARG( #func, 1, string ); \ 00083 REQ_ARG( #func, 2, string ); \ 00084 CString str1; \ 00085 CString str2; \ 00086 LUA->PopStack( str2 ); \ 00087 LUA->PopStack( str1 ); \ 00088 LUA_RETURN( call ); \ 00089 } \ 00090 LuaFunction( func ); /* register it */ 00091 00092 /* Functions that take a single PlayerNumber argument: */ 00093 #define LuaFunction_PlayerNumber( func, call ) \ 00094 int LuaFunc_##func( lua_State *L ) { \ 00095 REQ_ARGS( #func, 1 ); \ 00096 REQ_ARG_NUMBER_RANGE( #func, 1, 1, NUM_PLAYERS ); \ 00097 const PlayerNumber pn = (PlayerNumber) (int(lua_tonumber( L, -1 ))-1); \ 00098 LUA_RETURN( call ); \ 00099 } \ 00100 LuaFunction( func ); /* register it */ 00101 00102 /* Functions that take a single PlayerNumber argument and an optional integer: */ 00103 #define LuaFunction_PlayerNumber_OptInt( func, def, call ) \ 00104 int LuaFunc_##func( lua_State *L ) { \ 00105 REQ_ARGS_BETWEEN( #func, 1, 2 ); \ 00106 REQ_ARG_NUMBER_RANGE( #func, 1, 1, NUM_PLAYERS ); \ 00107 const PlayerNumber pn = (PlayerNumber) (int(lua_tonumber( L, 1 ))-1); \ 00108 int a1 = def; \ 00109 LUA->GetStack( L, 2, a1 ); \ 00110 } 00111 00112 /* Linked list of functions we make available to Lua. */ 00113 struct LuaFunctionList 00114 { 00115 LuaFunctionList( CString name, lua_CFunction func ); 00116 CString name; 00117 lua_CFunction func; 00118 LuaFunctionList *next; 00119 }; 00120 extern LuaFunctionList *g_LuaFunctionList; 00121 #define LuaFunction( func ) static LuaFunctionList g_##func( #func, LuaFunc_##func ) 00122 00123 #endif 00124 00125 /* 00126 * (c) 2004 Glenn Maynard 00127 * All rights reserved. 00128 * 00129 * Permission is hereby granted, free of charge, to any person obtaining a 00130 * copy of this software and associated documentation files (the 00131 * "Software"), to deal in the Software without restriction, including 00132 * without limitation the rights to use, copy, modify, merge, publish, 00133 * distribute, and/or sell copies of the Software, and to permit persons to 00134 * whom the Software is furnished to do so, provided that the above 00135 * copyright notice(s) and this permission notice appear in all copies of 00136 * the Software and that both the above copyright notice(s) and this 00137 * permission notice appear in supporting documentation. 00138 * 00139 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00140 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00141 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00142 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00143 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00144 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00145 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00146 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00147 * PERFORMANCE OF THIS SOFTWARE. 00148 */

Generated on Thu Jan 27 20:57:23 2005 for StepMania by doxygen 1.3.7