MCPcopy Create free account
hub / github.com/MonaSolutions/MonaServer / ToObject

Function ToObject

MonaServer/sources/Script.h:366–641  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

364
365 template<typename Type>
366 static Type* ToObject(lua_State *pState,bool& isConst,int index=-1,bool withLogs=false) {
367 if(lua_getmetatable(pState,index)==0) {
368 if(!withLogs) return NULL;
369 SCRIPT_BEGIN(pState)
370 SCRIPT_ERROR("'this' argument not present or has no metatable, call method with ':' colon operator")
371 SCRIPT_END
372 return NULL;
373 }
374
375 // already deleted?
376 lua_getfield(pState,-1,"|this");
377 Type* pThis = (Type*)lua_touserdata(pState, -1);
378 if (!pThis) {
379 bool isDeleted(lua_isnumber(pState, -1) ? true : false);
380 lua_pop(pState,2);
381 if(!withLogs) return NULL;
382 SCRIPT_BEGIN(pState)
383 if (isDeleted)
384 SCRIPT_ERROR(typeid(Type).name()," object deleted")
385 else
386 SCRIPT_ERROR(typeid(Type).name()," argument not present, call method with ':' colon operator")
387 SCRIPT_END
388 return NULL;
389 }
390 lua_pop(pState,1);
391
392 // check type
393 lua_getfield(pState,-1,"|type");
394 if (lua_touserdata(pState, -1) != &typeid(Type)) {
395 if(withLogs) {
396 SCRIPT_BEGIN(pState)
397 SCRIPT_ERROR("Cast error, type ",lua_touserdata(pState, -1)," doesn't match ",&typeid(Type), " of ",typeid(Type).name()," type")
398 SCRIPT_END
399 }
400 lua_pop(pState,2);
401 return NULL;
402 }
403 lua_pop(pState,1);
404
405 // isConst?
406 lua_getfield(pState,-1,"|const");
407 isConst = lua_toboolean(pState,-1) ? true : 0;
408
409 lua_pop(pState,2);
410
411 return pThis;
412 }
413
414
415 template<typename Type>
416 static void AddComparator(lua_State *pState) {
417 lua_getmetatable(pState, -1);
418
419 lua_pushlightuserdata(pState, (void*)&typeid(Type));
420 lua_gettable(pState, LUA_REGISTRYINDEX);
421 if (!lua_istable(pState, -1)) {
422 lua_pop(pState, 1);
423 lua_newtable(pState);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected