Lua: struct method calls work again.
[fail.git] / lua / method.h
blobd110f9662d7c3553d83329f9a471f188393b7943
1 #ifndef AWFUL_LUA_METHOD_H
2 #define AWFUL_LUA_METHOD_H
4 namespace awful { namespace LuaImpl
6 template< class C, typename obj_category, typename PointerT > struct CallContext
8 CallContext( lua_State* pLS, bool bStatic = false, int SkipParams = 0 ) :
9 m_pLS( pLS ),
10 m_paramindex( SkipParams + 1 ),
11 m_bReturnValue( false ),
12 m_bStatic( bStatic ),
13 m_SkipParams( SkipParams )
17 int numParams() const
19 int numparams = lua_gettop( m_pLS );
21 if( !m_bStatic )
22 --numparams;
24 numparams -= m_SkipParams;
26 return numparams;
29 void numExpectedParams( int Num_ ) const
31 // Check if we have the proper number of parameters
32 int numargs = numParams();
33 if( numargs != Num_ )
34 throw Error::NotEnoughParams( numargs, Num_ );
37 C& getObject()
39 PointerT Ptr;
41 try
43 TypeConverter< obj_category, PointerT > tc( m_pLS );
44 tc.ConvertFromLua( m_paramindex, Ptr );
46 catch( const Error::TypeMismatch& error )
48 throw Error::BadMethodCall();
51 ++m_paramindex;
52 return *Ptr;
55 template< typename category, typename T > bool getParam( const char* pName_, T& Dest_ )
57 TypeConverter< category, T > tc( m_pLS );
59 if( !tc.CheckType_nothrow( m_paramindex ) )
61 m_paramindex = m_SkipParams + 1;
62 return false;
65 tc.ConvertFromLua_nocheck( m_paramindex, Dest_ );
66 ++m_paramindex;
67 return true;
70 template< typename category, typename T > void setReturnValue( const T& Value_ )
72 TypeConverter< category, T > tc( m_pLS );
73 tc.ConvertToLua( Value_ );
74 m_bReturnValue = true;
77 lua_State* m_pLS;
78 int m_paramindex;
79 bool m_bReturnValue;
80 bool m_bStatic;
81 int m_SkipParams;
84 template< class C, typename MtdTag, class CC > struct LuaStaticMethodWrapper
86 static int LuaCFunc( lua_State* pLS )
88 try
90 CC context( pLS, true );
91 if( !method_traits< C, MtdTag >::template CallWrapper< CC >::Call( context ) )
92 throw Error::NoOverloadFound();
93 return context.m_bReturnValue ? 1 : 0;
95 catch( const Error::Base& error )
97 error.ConvertToLua( pLS );
98 return 0;
103 // Same as static method wrapper, but tell the callcontext to skip
104 // the first param.
105 template< class C, class CC > struct LuaCtorWrapper
107 static int LuaCFunc( lua_State* pLS )
111 CC context( pLS, true, 1 );
112 if( !method_traits< C, C >::template CallWrapper< CC >::Call( context ) )
113 throw Error::NoOverloadFound();
114 return context.m_bReturnValue ? 1 : 0;
116 catch( const Error::Base& error )
118 error.ConvertToLua( pLS );
119 return 0;
124 template< class C, typename MtdTag, class CC > struct LuaMethodWrapper
126 static int LuaCFunc( lua_State* pLS )
130 CC context( pLS, false );
131 if( !method_traits< C, MtdTag >::template CallWrapper< CC >::Call( context ) )
132 throw Error::NoOverloadFound();
133 return context.m_bReturnValue ? 1 : 0;
135 catch( Error::Base& error )
137 error.ConvertToLua( pLS );
138 return 0;
144 #endif