Some reorganisation of the abf directories.
[fail.git] / lua / basic_types.h
blob96e147c2395cc40f28c84334514cbc3716520a5d
1 #ifndef AWFUL_LUA_BASIC_TYPES_H
2 #define AWFUL_LUA_BASIC_TYPES_H
4 namespace awful { namespace LuaImpl
6 template< typename category, typename T > struct TypeConverter
8 };
10 template< typename T > struct NumericTypeConverter
12 NumericTypeConverter( lua_State* pLS_ ) :
13 m_pLS( pLS_ )
17 bool CheckType_nothrow( int Index_ )
19 return LuaImpl::CheckType_nothrow( m_pLS, Index_, LUA_TNUMBER );
22 void ConvertFromLua_nocheck( int Index_, T& Dest_ )
24 Dest_ = static_cast< T >( lua_tonumber( m_pLS, Index_ ) );
27 void ConvertFromLua( int Index_, T& Dest_ )
29 CheckType( m_pLS, Index_, LUA_TNUMBER );
30 ConvertFromLua_nocheck( Index_, Dest_ );
33 void ConvertToLua( const T& Value_ )
35 lua_pushnumber( m_pLS, static_cast< lua_Number >( Value_ ) );
38 lua_State* m_pLS;
41 template<> struct TypeConverter< NormalType, int32_t > : NumericTypeConverter< int32_t >
43 TypeConverter( lua_State* pLS_ ) : NumericTypeConverter< int32_t >( pLS_ ) {}
47 template<> struct TypeConverter< NormalType, uint32_t > : NumericTypeConverter< uint32_t >
49 TypeConverter( lua_State* pLS_ ) : NumericTypeConverter< uint32_t >( pLS_ ) {}
53 template<> struct TypeConverter< NormalType, float > : NumericTypeConverter< float >
55 TypeConverter( lua_State* pLS_ ) : NumericTypeConverter< float >( pLS_ ) {}
58 // bool
59 template<> struct TypeConverter< NormalType, bool >
61 TypeConverter( lua_State* pLS_ ) :
62 m_pLS( pLS_ )
66 bool CheckType_nothrow( int Index_ )
68 return LuaImpl::CheckType_nothrow( m_pLS, Index_, LUA_TBOOLEAN );
71 void ConvertFromLua_nocheck( int Index_, bool& Dest_ )
73 Dest_ = !!lua_toboolean( m_pLS, Index_ );
76 void ConvertFromLua( int Index_, bool& Dest_ )
78 CheckType( m_pLS, Index_, LUA_TBOOLEAN );
79 ConvertFromLua_nocheck( Index_, Dest_ );
82 void ConvertToLua( const bool& Value_ )
84 lua_pushboolean( m_pLS, Value_ );
87 lua_State* m_pLS;
90 // string
91 template<> struct TypeConverter< NormalType, std::string >
93 TypeConverter( lua_State* pLS_ ) :
94 m_pLS( pLS_ )
98 bool CheckType_nothrow( int Index_ )
100 return LuaImpl::CheckType_nothrow( m_pLS, Index_, LUA_TSTRING );
103 void ConvertFromLua_nocheck( int Index_, std::string& Dest_ )
105 Dest_ = lua_tostring( m_pLS, Index_ );
108 void ConvertFromLua( int Index_, std::string& Dest_ )
110 CheckType( m_pLS, Index_, LUA_TSTRING );
111 ConvertFromLua_nocheck( Index_, Dest_ );
114 void ConvertToLua( const std::string& Value_ )
116 lua_pushstring( m_pLS, Value_.c_str() );
119 lua_State* m_pLS;
122 // TODO: all other types
125 #endif