mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-03 10:22:45 -04:00
renamed ResourceCounter to CountedResource
This commit is contained in:
parent
e34ee0d81b
commit
ffb1940915
@ -1,38 +1,38 @@
|
||||
|
||||
class ResourceCounter(object):
|
||||
class CountedResource(object):
|
||||
"""
|
||||
This class is an attempt to combine the RAIA idiom with reference
|
||||
counting semantics in order to model shared resources. RAIA stands
|
||||
for "Resource Allocation Is Acquisition" (see 'Effective C++' for a
|
||||
more in-depth explanation)
|
||||
|
||||
When a resource is needed, create an appropriate ResourceCounter
|
||||
When a resource is needed, create an appropriate CountedResource
|
||||
object. If the resource is already available (meaning another
|
||||
ResourceCounter object of the same type already exists), no action
|
||||
CountedResource object of the same type already exists), no action
|
||||
is taken. Otherwise, acquire() is invoked, and the resource is
|
||||
allocated. The resource will remain valid until all matching
|
||||
ResourceCounter objects have been deleted. When no objects of
|
||||
a particular ResourceCounter type exist, the release() function for
|
||||
CountedResource objects have been deleted. When no objects of
|
||||
a particular CountedResource type exist, the release() function for
|
||||
that type is invoked and the managed resource is cleaned up.
|
||||
|
||||
Usage:
|
||||
Define a subclass of ResourceCounter that defines the
|
||||
Define a subclass of CountedResource that defines the
|
||||
@classmethods acquire() and release(). In these two
|
||||
functions, define your resource allocation and cleanup code.
|
||||
|
||||
IMPORTANT:
|
||||
If you define your own __init__ and __del__ methods, you
|
||||
MUST be sure to call down to the ones defined in
|
||||
ResourceCounter.
|
||||
CountedResource.
|
||||
|
||||
Notes:
|
||||
Until we figure out a way to wrangle a bit more functionality
|
||||
out of Python, you MUST NOT inherit from any class that has
|
||||
ResourceCounter as its base class. In debug mode, this will
|
||||
CountedResource as its base class. In debug mode, this will
|
||||
raise a runtime assertion during the invalid class's call to
|
||||
__init__(). If you have more than one resource that you want to
|
||||
manage/access with a single object, you should subclass
|
||||
ResourceCounter again. See the example code at the bottom of
|
||||
CountedResource again. See the example code at the bottom of
|
||||
this file to see how to accomplish this (This is useful for
|
||||
dependent resources).
|
||||
"""
|
||||
@ -50,8 +50,8 @@ class ResourceCounter(object):
|
||||
@classmethod
|
||||
def decrementCounter(cls):
|
||||
try:
|
||||
cls.RESOURCE_COUNTER_INIT
|
||||
del cls.RESOURCE_COUNTER_INIT
|
||||
cls.RESOURCE_COUNTER_INIT_FAILED
|
||||
del cls.RESOURCE_COUNTER_INIT_FAILED
|
||||
except AttributeError:
|
||||
cls.RESOURCE_COUNTER -= 1
|
||||
if cls.RESOURCE_COUNTER < 1:
|
||||
@ -67,12 +67,12 @@ class ResourceCounter(object):
|
||||
|
||||
def __init__(self):
|
||||
cls = type(self)
|
||||
cls.RESOURCE_COUNTER_INIT = True
|
||||
assert cls.mro()[1] == ResourceCounter, \
|
||||
cls.RESOURCE_COUNTER_INIT_FAILED = True
|
||||
assert cls.mro()[1] == CountedResource, \
|
||||
(lambda: \
|
||||
'%s cannot be subclassed.' \
|
||||
% cls.mro()[list(cls.mro()).index(ResourceCounter) - 1].__name__)()
|
||||
del cls.RESOURCE_COUNTER_INIT
|
||||
% cls.mro()[list(cls.mro()).index(CountedResource) - 1].__name__)()
|
||||
del cls.RESOURCE_COUNTER_INIT_FAILED
|
||||
self.incrementCounter()
|
||||
|
||||
def __del__(self):
|
||||
@ -80,7 +80,7 @@ class ResourceCounter(object):
|
||||
|
||||
|
||||
if __debug__ and __name__ == '__main__':
|
||||
class MouseResource(ResourceCounter):
|
||||
class MouseResource(CountedResource):
|
||||
"""
|
||||
A simple class to demonstrate the acquisition of a resource.
|
||||
"""
|
||||
@ -113,11 +113,11 @@ if __debug__ and __name__ == '__main__':
|
||||
def __del__(self):
|
||||
super(MouseResource, self).__del__()
|
||||
|
||||
class CursorResource(ResourceCounter):
|
||||
class CursorResource(CountedResource):
|
||||
"""
|
||||
A class to demonstrate how to implement a dependent
|
||||
resource. Notice how this class also inherits from
|
||||
ResourceCounter. Instead of subclassing MouseCounter,
|
||||
CountedResource. Instead of subclassing MouseCounter,
|
||||
we will just acquire it in our __init__() and release
|
||||
it in our __del__().
|
||||
"""
|
Loading…
x
Reference in New Issue
Block a user