From d91112167e0a45b20b12b963aa75c414cee0824a Mon Sep 17 00:00:00 2001 From: Joe Shochet Date: Tue, 21 Sep 2004 21:21:57 +0000 Subject: [PATCH] pool collection --- direct/src/showbase/Pool.py | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 direct/src/showbase/Pool.py diff --git a/direct/src/showbase/Pool.py b/direct/src/showbase/Pool.py new file mode 100755 index 0000000000..5631635249 --- /dev/null +++ b/direct/src/showbase/Pool.py @@ -0,0 +1,92 @@ +""" + +Pool is a collection of python objects that you can checkin and +checkout. This is useful for a cache of objects that are expensive to load +and can be reused over and over, like splashes on cannonballs, or +bulletholes on walls. The pool is unsorted. Items do not have to be unique +or be the same type. + +Internally the pool is implemented with 2 lists, free items and used items. + +p = Pool([1,2,3,4,5]) +x = p.checkout() +p.checkin(x) + +""" + + +from direct.directnotify import DirectNotifyGlobal + +class Pool: + + notify = DirectNotifyGlobal.directNotify.newCategory("Pool") + + def __init__(self, free=[]): + self.__free = free + self.__used = [] + + def add(self, item): + """ + Add an item to the free list. + """ + self.__free.append(item) + + def remove(self, item): + """ + Remove an item. Error is flagged if the item is not in the pool. + """ + if item in self.__free: + self.__free.remove(item) + if item in self.__used: + self.__used.remove(item) + self.notify.error("item not in pool") + + def checkout(self): + """ + Get an arbitrary item from the pool. + """ + if not self.__free: + self.notify.error("no items are free") + item = self.__free.pop() + self.__used.append(item) + return item + + def checkin(self, item): + """ + Put back a checked out item. + Error if the item is not checked out. + """ + if item not in self.__used: + self.notify.error("item is not checked out") + self.__used.remove(item) + self.__free.append(item) + + def reset(self): + """ + Resets the pool so all items are free. + """ + self.__free.extend(self.__used) + self.__used = [] + + def hasFree(self): + """ + Returns true if there is at least one free item. + """ + return (len(self.__free) != 0) + + def isFree(self, item): + """ + Returns true if this item is free for check out. + """ + return (item in self.__free) + + def isUsed(self, item): + """ + Returns true if this item has already been checked out. + """ + return (item in self.__used) + + def __repr__(self): + return "free = %s\nused = %s" % (self.__free, self.__used) + +