Pool.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. Pool is a collection of python objects that you can checkin and
  3. checkout. This is useful for a cache of objects that are expensive to load
  4. and can be reused over and over, like splashes on cannonballs, or
  5. bulletholes on walls. The pool is unsorted. Items do not have to be unique
  6. or be the same type.
  7. Internally the pool is implemented with 2 lists, free items and used items.
  8. Example:
  9. .. code-block:: python
  10. p = Pool([1, 2, 3, 4, 5])
  11. x = p.checkout()
  12. p.checkin(x)
  13. """
  14. __all__ = ['Pool']
  15. from direct.directnotify import DirectNotifyGlobal
  16. class Pool:
  17. notify = DirectNotifyGlobal.directNotify.newCategory("Pool")
  18. def __init__(self, free=None):
  19. if free:
  20. self.__free = free
  21. else:
  22. self.__free = []
  23. self.__used = []
  24. def add(self, item):
  25. """
  26. Add an item to the free list.
  27. """
  28. self.__free.append(item)
  29. def remove(self, item):
  30. """
  31. Remove an item. Error is flagged if the item is not in the pool.
  32. """
  33. if item in self.__free:
  34. self.__free.remove(item)
  35. elif item in self.__used:
  36. self.__used.remove(item)
  37. else:
  38. self.notify.error("item not in pool")
  39. def checkout(self):
  40. """
  41. Get an arbitrary item from the pool.
  42. """
  43. if not self.__free:
  44. self.notify.error("no items are free")
  45. item = self.__free.pop()
  46. self.__used.append(item)
  47. return item
  48. def checkin(self, item):
  49. """
  50. Put back a checked out item.
  51. Error if the item is not checked out.
  52. """
  53. if item not in self.__used:
  54. self.notify.error("item is not checked out")
  55. self.__used.remove(item)
  56. self.__free.append(item)
  57. def reset(self):
  58. """
  59. Resets the pool so all items are free.
  60. """
  61. self.__free.extend(self.__used)
  62. self.__used = []
  63. def hasFree(self):
  64. """
  65. Returns true if there is at least one free item.
  66. """
  67. return (len(self.__free) != 0)
  68. def isFree(self, item):
  69. """
  70. Returns true if this item is free for check out.
  71. """
  72. return (item in self.__free)
  73. def isUsed(self, item):
  74. """
  75. Returns true if this item has already been checked out.
  76. """
  77. return (item in self.__used)
  78. def getNumItems(self):
  79. """
  80. Returns the number of free items and the number of used items.
  81. """
  82. return len(self.__free), len(self.__used)
  83. def cleanup(self, cleanupFunc=None):
  84. """
  85. Completely cleanup the pool and all of its objects.
  86. cleanupFunc will be called on every free and used item.
  87. """
  88. if cleanupFunc:
  89. # Make a list of all the items first in case the act of
  90. # calling cleanupFunc moves some from used to free.
  91. allItems = self.__free + self.__used
  92. for item in allItems:
  93. cleanupFunc(item)
  94. del self.__free
  95. del self.__used
  96. def __repr__(self):
  97. return "free = %s\nused = %s" % (self.__free, self.__used)