Lua: partial implementation of struct reference wrapping, which has led into a shitlo...
[fail.git] / lua / method.h
blob8953fa904f1ae5dd15f650f95ba66f943083007c
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, cp_NoThrow > tc( m_pLS );
59 /* if( !tc.CheckType( m_paramindex ) )
61 m_paramindex = m_SkipParams + 1;
62 return false;
63 }*/
65 if( !tc.ConvertFromLua( m_paramindex, Dest_ ) )
67 m_paramindex = m_SkipParams + 1;
68 return false;
71 ++m_paramindex;
72 return true;
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;
82 lua_State* m_pLS;
83 int m_paramindex;
84 bool m_bReturnValue;
85 bool m_bStatic;
86 int m_SkipParams;
89 template< class C, typename MtdTag, class CC > struct StaticMethodWrapper
91 static int LuaCFunc( lua_State* pLS )
93 try
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 );
103 return 0;
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();
118 return 1;
120 catch( const Error::Base& error )
122 error.ConvertToLua( pLS );
123 return 0;
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 );
142 return 0;
148 #endif