Thread Tools Display Modes
Prev Previous Post   Next Post Next
03/01/14, 05:59 PM   #1
zork
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 29
ZO_ObjectPool

I have not wrapped my head around the ZO_ObjectPool yet.

Here is a snippet of code from zo_objectpool.lua. Not sure if that has been updated yet. (aug 2013)

Lua Code:
  1. --[[
  2.  
  3.     A generic pool to contain "active" and "free" objects.  Active objects
  4.     are typically objects which:
  5.         1. Have a relatively high construction cost
  6.         2. Are not lightweight enough to create many of them at once
  7.         3. Tend to be reused as dynamic elements of a larger container.
  8.        
  9.     The pool should "rapidly" reach a high-water mark of contained objects
  10.     which should flow between active and free states on a regular basis.
  11.    
  12.     Ideal uses of the ZO_ObjectPool would be to contain objects such as:
  13.         1. Scrolling combat text
  14.         2. Tracked quests
  15.         3. Buff icons
  16.        
  17.     The pools are not intended to be used to track a dynamic set of
  18.     contained objects whose membership grows to a predetermined size.
  19.     As such, do NOT use the pool to track:
  20.         1. Chat filters
  21.         2. Inventory slots
  22.         3. Action buttons (unless creating something like AutoBar)
  23.        
  24.     A common usage pattern is instantiating templated controls.  To facilitate this
  25.     without bloating your own code you should use ZO_ObjectPool_CreateControl which has
  26.     been written here as a convenience.  It creates a control named "template"..id where
  27.     id is an arbitrary value that will not conflict with other generated id's.
  28.    
  29.     If your system depends on having well-known names for controls, you should not use the
  30.     convenience function.    
  31. --]]
  32.  
  33. ZO_ObjectPool = ZO_Object:Subclass()
  34.  
  35. function ZO_ObjectPool:New(factoryFunction, resetFunction)
  36.     local pool = ZO_Object.New(self)
  37.        
  38.     if(factoryFunction)
  39.     then
  40.         resetFunction = resetFunction or ZO_ObjectPool_DefaultResetControl
  41.  
  42.         pool.m_Active   = {}
  43.         pool.m_Free     = {}
  44.         pool.m_Factory  = factoryFunction   -- Signature: function(ZO_ObjectPool)
  45.         pool.m_Reset    = resetFunction     -- Signature: function(objectBeingReset)
  46.         pool.m_NextFree = 1                 -- Just in case the user would like the pool to generate object keys.
  47.         pool.m_NextControlId = 0            -- Just in case the user would like the pool to generate id-based control suffixes
  48.     end
  49.    
  50.     return pool
  51. end
  52.  
  53. function ZO_ObjectPool:GetNextFree()
  54.     local nextPotentialFree = self.m_NextFree
  55.     self.m_NextFree = self.m_NextFree + 1
  56.  
  57.     local freeKey, object = next(self.m_Free)
  58.     if(freeKey == nil or object == nil)
  59.     then
  60.         return nextPotentialFree, nil
  61.     end
  62.  
  63.     return freeKey, object
  64. end
  65.  
  66. function ZO_ObjectPool:GetNextControlId()
  67.     self.m_NextControlId = self.m_NextControlId + 1
  68.     return self.m_NextControlId
  69. end
  70.  
  71. function ZO_ObjectPool:GetTotalObjectCount()
  72.     return self:GetActiveObjectCount() + self:GetFreeObjectCount()
  73. end
  74.  
  75. function ZO_ObjectPool:GetActiveObjectCount()
  76.     return NonContiguousCount(self.m_Active)
  77. end
  78.  
  79. function ZO_ObjectPool:GetActiveObjects()
  80.     return self.m_Active
  81. end
  82.  
  83. function ZO_ObjectPool:GetFreeObjectCount()
  84.     return NonContiguousCount(self.m_Free)
  85. end
  86.  
  87. function ZO_ObjectPool:AcquireObject(objectKey)
  88.     -- If the object referred to by this key is already
  89.     -- active there is very little work to do...just return it.    
  90.     if((objectKey ~= nil) and (self.m_Active[objectKey] ~= nil))
  91.     then
  92.         return self.m_Active[objectKey], objectKey
  93.     end
  94.    
  95.     local object = nil
  96.    
  97.     -- If we know the key that we want, use that object first, otherwise just return the first object from the free pool
  98.     -- A nil objectKey means that the caller doesn't care about tracking unique keys for these objects, or that the keys
  99.     -- the system uses can't directly be used to look up the data.  Just manage them with pool-generated id's
  100.     if(objectKey == nil)
  101.     then
  102.         objectKey, object = self:GetNextFree()
  103.     else
  104.         object = self.m_Free[objectKey]
  105.     end
  106.  
  107.     --
  108.     -- If the object is valid it was reclaimed from the free list, otherwise it needs to be created.
  109.     -- Creation uses the m_Factory member which receives this pool as its only argument.
  110.     -- Either way, after this, object must be non-nil
  111.     --
  112.     if(object)
  113.     then
  114.         self.m_Free[objectKey] = nil
  115.     else        
  116.         object = self:m_Factory()
  117.     end
  118.            
  119.     self.m_Active[objectKey] = object
  120.        
  121.     return object, objectKey
  122. end
  123.  
  124. function ZO_ObjectPool:GetExistingObject(objectKey)
  125.     return self.m_Active[objectKey]
  126. end
  127.  
  128. function ZO_ObjectPool:ReleaseObject(objectKey)
  129.     local object = self.m_Active[objectKey]
  130.    
  131.     if(object)
  132.     then
  133.         if(self.m_Reset)
  134.         then
  135.             self.m_Reset(object, self)
  136.         end
  137.        
  138.         self.m_Active[objectKey] = nil
  139.         self.m_Free[objectKey] = object
  140.     end
  141. end
  142.  
  143. function ZO_ObjectPool:ReleaseAllObjects()
  144.     for k, v in pairs(self.m_Active)
  145.     do
  146.         if(self.m_Reset)
  147.         then
  148.             self.m_Reset(v, self)
  149.         end
  150.        
  151.         self.m_Free[k] = v
  152.     end
  153.    
  154.     self.m_Active = {}
  155. end
  156.  
  157. function ZO_ObjectPool_CreateControl(templateName, objectPool, parentControl)
  158.     return CreateControlFromVirtual(templateName, parentControl, templateName, objectPool:GetNextControlId())
  159. end
  160.  
  161. function ZO_ObjectPool_CreateNamedControl(name, templateName, objectPool, parentControl)
  162.     return CreateControlFromVirtual(name, parentControl, templateName, objectPool:GetNextControlId())
  163. end
  164.  
  165. function ZO_ObjectPool_DefaultResetControl(control)
  166.     control:SetHidden(true)
  167. end

Pawkette used it in LootDrop. Maybe he/she knows and can enlighten us.

Last edited by zork : 03/01/14 at 06:02 PM.
  Reply With Quote
 

ESOUI » Developer Discussions » General Authoring Discussion » ZO_ObjectPool


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off