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 ) :
10 m_paramindex( SkipParams
+ 1 ),
11 m_bReturnValue( false ),
13 m_SkipParams( SkipParams
)
19 int numparams
= lua_gettop( m_pLS
);
24 numparams
-= m_SkipParams
;
29 void numExpectedParams( int Num_
) const
31 // Check if we have the proper number of parameters
32 int numargs
= numParams();
34 throw Error::NotEnoughParams( numargs
, Num_
);
43 TypeConverter
< obj_category
, PointerT
> tc( m_pLS
);
44 tc
.ConvertFromLua( m_paramindex
, Ptr
);
46 catch( const Error::TypeMismatch
& error
)
48 throw Error::BadMethodCall();
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;
65 tc
.ConvertFromLua_nocheck( m_paramindex
, Dest_
);
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;
84 template< class C
, typename MtdTag
, class CC
> struct LuaStaticMethodWrapper
86 static int LuaCFunc( lua_State
* pLS
)
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
);
103 // Same as static method wrapper, but tell the callcontext to skip
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
);
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
);