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
, cp_NoThrow
> tc( m_pLS
);
59 /* if( !tc.CheckType( m_paramindex ) )
61 m_paramindex = m_SkipParams + 1;
65 if( !tc
.ConvertFromLua( m_paramindex
, Dest_
) )
67 m_paramindex
= m_SkipParams
+ 1;
75 template< typename category
, typename T
> void setReturnValue( const T
& Value_
)
77 TypeConverter
< category
, T
> tc( m_pLS
);
78 tc
.ConvertToLua( Value_
);
79 m_bReturnValue
= true;
89 template< class C
, typename MtdTag
, class CC
> struct StaticMethodWrapper
91 static int LuaCFunc( lua_State
* pLS
)
95 CC
context( pLS
, true );
96 if( !method_traits
< C
, MtdTag
>::template CallWrapper
< CC
>::Call( context
) )
97 throw Error::NoOverloadFound();
98 return context
.m_bReturnValue
? 1 : 0;
100 catch( const Error::Base
& error
)
102 error
.ConvertToLua( pLS
);
108 template< class C
, class CC
> struct CtorWrapper
110 static int LuaCFunc( lua_State
* pLS
)
114 CC
context( pLS
, true, 1 );
115 if( !method_traits
< C
, typename class_traits
< C
>::ctor_tag
>::
116 template CallWrapper
< CC
>::Call( context
) )
117 throw Error::NoOverloadFound();
120 catch( const Error::Base
& error
)
122 error
.ConvertToLua( pLS
);
128 template< class C
, typename MtdTag
, class CC
> struct MethodWrapper
130 static int LuaCFunc( lua_State
* pLS
)
134 CC
context( pLS
, false );
135 if( !method_traits
< C
, MtdTag
>::template CallWrapper
< CC
>::Call( context
) )
136 throw Error::NoOverloadFound();
137 return context
.m_bReturnValue
? 1 : 0;
139 catch( Error::Base
& error
)
141 error
.ConvertToLua( pLS
);