Advertisement
Xetrill

xs_obj.script

Aug 19th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. local type, tostring, sprintf
  2.     = type, tostring, string.format
  3.  
  4. --[[-----------------------------------------------------------------------\--
  5.     :: Class: XsObj
  6. --\\-----------------------------------------------------------------------]]--
  7.  
  8. -- TODO Maybe abandon luabind classes altogether?
  9. -- Debugging would be easier, since tables can be compared. And it should
  10. -- be possible to implement a OO-system that mimics luabind.
  11. class "XsObj"
  12.  
  13.     --[[-------------------------------------------------------------------\--
  14.         :: Fields: Static
  15.     --\\-------------------------------------------------------------------]]--
  16.  
  17.     local __xs_uid = 0
  18.  
  19.     --[[-------------------------------------------------------------------\--
  20.         :: Constructor / Finalizer
  21.     --\\-------------------------------------------------------------------]]--
  22.  
  23.     function XsObj:__init(cls)
  24.         if type(cls) ~= "string" or cls:len() == 0 then
  25.             abort("[XsObj:__init]: Invalid argument #1; expected the name of subclass.")
  26.         end
  27.  
  28.         __xs_uid = __xs_uid + 1
  29.  
  30.         self.__uid = __xs_uid
  31.         self.__cls = cls
  32.     end
  33.  
  34.     --[[-------------------------------------------------------------------\--
  35.         :: Methods: Overloads
  36.     --\\-------------------------------------------------------------------]]--
  37.  
  38.     -- TODO Move and rename to xs_utils.equals making it a more useful utility.
  39.     function XsObj:__eq(L, R)
  40.         local xL = type(L) == "userdata"
  41.         local xR = type(R) == "userdata"
  42.         if xL and xR then
  43.             -- XsObj
  44.             xL = type(L.__uid) == "number"
  45.             xR = type(R.__uid) == "number"
  46.             if xL and xR then
  47.                 return L.__uid == R.__uid
  48.             elseif xL or xR then
  49.                 return false
  50.             else
  51.                 -- game_object
  52.                 xL = type(L.clsid) == "function"
  53.                 xR = type(R.clsid) == "function"
  54.                 if xL and xR then
  55.                     return L:clsid() == R:clsid() and L:id() == R:id()
  56.                 elseif xL or xR then
  57.                     return false
  58.                 else
  59.                     abort("[XsObj.__eq]: Unable to determine object equality.")
  60.                 end
  61.             end
  62.         elseif xL or xR then
  63.             return false
  64.         else
  65.             return L == R
  66.         end
  67.     end
  68.  
  69.     function XsObj:__tostring()
  70.         return sprintf("%s [%08X]", self.__cls, self.__uid)
  71.     end
  72.  
  73.     function XsObj:__add(L, R)
  74.         self:failFromCall("__add")
  75.     end
  76.  
  77.     function XsObj:__sub(L, R)
  78.         self:failFromCall("__sub")
  79.     end
  80.  
  81.     function XsObj:__mul(L, R)
  82.         self:failFromCall("__mul")
  83.     end
  84.  
  85.     function XsObj:__div(L, R)
  86.         self:failFromCall("__div")
  87.     end
  88.  
  89.     function XsObj:__pow(L, R)
  90.         self:failFromCall("__pow")
  91.     end
  92.  
  93.     function XsObj:__lt(L, R)
  94.         self:failFromCall("__lt")
  95.     end
  96.  
  97.     function XsObj:__le(L, R)
  98.         self:failFromCall("__le")
  99.     end
  100.  
  101.     function XsObj:__call()
  102.         self:failFromCall("__call")
  103.     end
  104.  
  105.     function XsObj:__unm(obj)
  106.         self:failFromCall("__unm")
  107.     end
  108.  
  109.     function XsObj:__len(obj)
  110.         self:failFromCall("__len")
  111.     end
  112.  
  113.     --[[-------------------------------------------------------------------\--
  114.         :: Methods: Private
  115.     --\\-------------------------------------------------------------------]]--
  116.  
  117.     function XsObj:failFromCall(func)
  118.         abort("[%s:%s]: Unsupported operator/function call.", self.__cls, func)
  119.     end
  120.  
  121.     --[[-------------------------------------------------------------------\--
  122.         :: Methods: Public
  123.     --\\-------------------------------------------------------------------]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement