mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-17 12:12:10 -04:00
added support for callable default value generators in POD to avoid modification of default values
This commit is contained in:
parent
02183867c0
commit
6eb422c26c
@ -968,6 +968,20 @@ if __debug__:
|
|||||||
assert len(q) == 0
|
assert len(q) == 0
|
||||||
assert q.isEmpty()
|
assert q.isEmpty()
|
||||||
|
|
||||||
|
def mostDerivedLast(classList):
|
||||||
|
"""pass in list of classes. sorts list in-place, with derived classes
|
||||||
|
appearing after their bases"""
|
||||||
|
def compare(a, b):
|
||||||
|
if issubclass(a, b):
|
||||||
|
result=1
|
||||||
|
elif issubclass(b, a):
|
||||||
|
result=-1
|
||||||
|
else:
|
||||||
|
result=0
|
||||||
|
#print a, b, result
|
||||||
|
return result
|
||||||
|
classList.sort(compare)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ParamObj/ParamSet
|
ParamObj/ParamSet
|
||||||
=================
|
=================
|
||||||
@ -1363,7 +1377,19 @@ faction3 = s3.faction
|
|||||||
class POD:
|
class POD:
|
||||||
DataSet = {
|
DataSet = {
|
||||||
# base class does not define any data items, but they would
|
# base class does not define any data items, but they would
|
||||||
# appear here as 'name': value,
|
# appear here as 'name': defaultValue,
|
||||||
|
#
|
||||||
|
# WARNING: default values of mutable types that do not copy by
|
||||||
|
# value ( dicts, lists etc.) will be shared by all class instances
|
||||||
|
# if default value is callable, it will be called to get actual
|
||||||
|
# default value
|
||||||
|
#
|
||||||
|
# for example:
|
||||||
|
#
|
||||||
|
# class MapData(POD):
|
||||||
|
# DataSet = {
|
||||||
|
# 'spawnIndices': Functor(list, [1,5,22]),
|
||||||
|
# }
|
||||||
}
|
}
|
||||||
def __init__(self, **kwArgs):
|
def __init__(self, **kwArgs):
|
||||||
self.__class__._compileDefaultDataSet()
|
self.__class__._compileDefaultDataSet()
|
||||||
@ -1417,7 +1443,10 @@ class POD:
|
|||||||
getDataNames = classmethod(getDataNames)
|
getDataNames = classmethod(getDataNames)
|
||||||
def getDefaultValue(cls, name):
|
def getDefaultValue(cls, name):
|
||||||
cls._compileDefaultDataSet()
|
cls._compileDefaultDataSet()
|
||||||
return cls._DataSet[name]
|
dv = cls._DataSet[name]
|
||||||
|
if callable(dv):
|
||||||
|
dv = dv()
|
||||||
|
return dv
|
||||||
getDefaultValue = classmethod(getDefaultValue)
|
getDefaultValue = classmethod(getDefaultValue)
|
||||||
def _compileDefaultDataSet(cls):
|
def _compileDefaultDataSet(cls):
|
||||||
if cls.__dict__.has_key('_DataSet'):
|
if cls.__dict__.has_key('_DataSet'):
|
||||||
@ -1460,25 +1489,15 @@ class POD:
|
|||||||
argStr += '%s=%s,' % (name, repr(getSetter(self, name, 'get')()))
|
argStr += '%s=%s,' % (name, repr(getSetter(self, name, 'get')()))
|
||||||
return '%s(%s)' % (self.__class__.__name__, argStr)
|
return '%s(%s)' % (self.__class__.__name__, argStr)
|
||||||
|
|
||||||
""" TODO
|
if __debug__:
|
||||||
if __dev__:
|
class PODtest(POD):
|
||||||
@staticmethod
|
|
||||||
def unitTest():
|
|
||||||
tColor = 'red'
|
|
||||||
tColor2 = 'blue'
|
|
||||||
class test(POD):
|
|
||||||
DataSet = {
|
DataSet = {
|
||||||
'color': tColor,
|
'foo': dict,
|
||||||
}
|
}
|
||||||
|
p1 = PODtest()
|
||||||
t = test()
|
p2 = PODtest()
|
||||||
assert t.getColor() == tColor
|
p1.foo[1] = 2
|
||||||
t.setColor(tColor2)
|
assert len(p2.foo) == 0
|
||||||
assert t.getColor() == tColor2
|
|
||||||
|
|
||||||
t2 = test().makeCopy()
|
|
||||||
assert t2.getColor() == t.getColor() == tColor2
|
|
||||||
"""
|
|
||||||
|
|
||||||
def bound(value, bound1, bound2):
|
def bound(value, bound1, bound2):
|
||||||
"""
|
"""
|
||||||
@ -1721,20 +1740,6 @@ def describeException(backTrace = 4):
|
|||||||
description += "%s: %s" % (exceptionName, extraInfo)
|
description += "%s: %s" % (exceptionName, extraInfo)
|
||||||
return description
|
return description
|
||||||
|
|
||||||
def mostDerivedLast(classList):
|
|
||||||
"""pass in list of classes. sorts list in-place, with derived classes
|
|
||||||
appearing after their bases"""
|
|
||||||
def compare(a, b):
|
|
||||||
if issubclass(a, b):
|
|
||||||
result=1
|
|
||||||
elif issubclass(b, a):
|
|
||||||
result=-1
|
|
||||||
else:
|
|
||||||
result=0
|
|
||||||
#print a, b, result
|
|
||||||
return result
|
|
||||||
classList.sort(compare)
|
|
||||||
|
|
||||||
def clampScalar(value, a, b):
|
def clampScalar(value, a, b):
|
||||||
# calling this ought to be faster than calling both min and max
|
# calling this ought to be faster than calling both min and max
|
||||||
if a < b:
|
if a < b:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user