Code
125template<class Derived, class InstanceType>126InstanceType* CvLuaScopedInstance<Derived, InstanceType>::GetInstance(lua_State* L, int idx, bool bErrorOnFail)127{128 const int stack_size = lua_gettop(L);129 bool bFail = true;130131 InstanceType* pkInstance = NULL;132 if(lua_type(L, idx) == LUA_TTABLE)133 {134 lua_getfield(L, idx, "__instance");135 if(lua_type(L, -1) == LUA_TLIGHTUSERDATA)136 {137 pkInstance = static_cast<InstanceType*>(lua_touserdata(L, -1));138 if(pkInstance)139 {140 bFail = false;141 }142 }143 }144145 lua_settop(L, stack_size);146147 if(bFail && bErrorOnFail)148 {149 if(idx == 1)150 luaL_error(L, "Not a valid instance. Either the instance is NULL or you used '.' instead of ':'.");151 Derived::HandleMissingInstance(L);152 }153 return pkInstance;154}