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