params.py (4859:97c7749896a6) | params.py (5033:2a48ab2b86d5) |
---|---|
1# Copyright (c) 2004-2006 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 32 unchanged lines hidden (view full) --- 41# Note that the default values are loaded into the class's attribute 42# space when the parameter dictionary is initialized (in 43# MetaSimObject._new_param()); after that point they aren't used. 44# 45##################################################################### 46 47import copy 48import datetime | 1# Copyright (c) 2004-2006 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 32 unchanged lines hidden (view full) --- 41# Note that the default values are loaded into the class's attribute 42# space when the parameter dictionary is initialized (in 43# MetaSimObject._new_param()); after that point they aren't used. 44# 45##################################################################### 46 47import copy 48import datetime |
49import inspect | |
50import re 51import sys 52import time 53 54import convert 55import proxy 56import ticks 57from util import * --- 4 unchanged lines hidden (view full) --- 62 return SimObject.isSimObject(*args, **kwargs) 63 64def isSimObjectSequence(*args, **kwargs): 65 return SimObject.isSimObjectSequence(*args, **kwargs) 66 67def isSimObjectClass(*args, **kwargs): 68 return SimObject.isSimObjectClass(*args, **kwargs) 69 | 49import re 50import sys 51import time 52 53import convert 54import proxy 55import ticks 56from util import * --- 4 unchanged lines hidden (view full) --- 61 return SimObject.isSimObject(*args, **kwargs) 62 63def isSimObjectSequence(*args, **kwargs): 64 return SimObject.isSimObjectSequence(*args, **kwargs) 65 66def isSimObjectClass(*args, **kwargs): 67 return SimObject.isSimObjectClass(*args, **kwargs) 68 |
69allParams = {} 70 71class MetaParamValue(type): 72 def __new__(mcls, name, bases, dct): 73 cls = super(MetaParamValue, mcls).__new__(mcls, name, bases, dct) 74 assert name not in allParams 75 allParams[name] = cls 76 return cls 77 78 |
|
70# Dummy base class to identify types that are legitimate for SimObject 71# parameters. 72class ParamValue(object): | 79# Dummy base class to identify types that are legitimate for SimObject 80# parameters. 81class ParamValue(object): |
82 __metaclass__ = MetaParamValue |
|
73 74 cxx_predecls = [] 75 swig_predecls = [] 76 77 # default for printing to .ini file is regular string conversion. 78 # will be overridden in some cases 79 def ini_str(self): 80 return str(self) --- 33 unchanged lines hidden (view full) --- 114 if kwargs: 115 raise TypeError, 'extra unknown kwargs %s' % kwargs 116 117 if not hasattr(self, 'desc'): 118 raise TypeError, 'desc attribute missing' 119 120 def __getattr__(self, attr): 121 if attr == 'ptype': | 83 84 cxx_predecls = [] 85 swig_predecls = [] 86 87 # default for printing to .ini file is regular string conversion. 88 # will be overridden in some cases 89 def ini_str(self): 90 return str(self) --- 33 unchanged lines hidden (view full) --- 124 if kwargs: 125 raise TypeError, 'extra unknown kwargs %s' % kwargs 126 127 if not hasattr(self, 'desc'): 128 raise TypeError, 'desc attribute missing' 129 130 def __getattr__(self, attr): 131 if attr == 'ptype': |
122 try: 123 ptype = SimObject.allClasses[self.ptype_str] 124 if not isinstance(ptype, type): 125 raise NameError 126 self.ptype = ptype 127 return ptype 128 except NameError: 129 raise 130 #raise TypeError, \ 131 # "Param qualifier '%s' is not a type" % self.ptype_str | 132 ptype = SimObject.allClasses[self.ptype_str] 133 assert issubclass(ptype, SimObject.SimObject) 134 self.ptype = ptype 135 return ptype 136 |
132 raise AttributeError, "'%s' object has no attribute '%s'" % \ 133 (type(self).__name__, attr) 134 135 def convert(self, value): 136 if isinstance(value, proxy.BaseProxy): 137 value.set_param_desc(self) 138 return value 139 if not hasattr(self, 'ptype') and isNullPointer(value): --- 15 unchanged lines hidden (view full) --- 155 def cxx_decl(self): 156 return '%s %s;' % (self.ptype.cxx_type, self.name) 157 158# Vector-valued parameter description. Just like ParamDesc, except 159# that the value is a vector (list) of the specified type instead of a 160# single value. 161 162class VectorParamValue(list): | 137 raise AttributeError, "'%s' object has no attribute '%s'" % \ 138 (type(self).__name__, attr) 139 140 def convert(self, value): 141 if isinstance(value, proxy.BaseProxy): 142 value.set_param_desc(self) 143 return value 144 if not hasattr(self, 'ptype') and isNullPointer(value): --- 15 unchanged lines hidden (view full) --- 160 def cxx_decl(self): 161 return '%s %s;' % (self.ptype.cxx_type, self.name) 162 163# Vector-valued parameter description. Just like ParamDesc, except 164# that the value is a vector (list) of the specified type instead of a 165# single value. 166 167class VectorParamValue(list): |
168 __metaclass__ = MetaParamValue |
|
163 def ini_str(self): 164 return ' '.join([v.ini_str() for v in self]) 165 166 def getValue(self): 167 return [ v.getValue() for v in self ] 168 169 def unproxy(self, base): 170 return [v.unproxy(base) for v in self] --- 41 unchanged lines hidden (view full) --- 212 213 def __getattr__(self, attr): 214 if self.ptype_str: 215 attr = self.ptype_str + '.' + attr 216 return ParamFactory(self.param_desc_class, attr) 217 218 # E.g., Param.Int(5, "number of widgets") 219 def __call__(self, *args, **kwargs): | 169 def ini_str(self): 170 return ' '.join([v.ini_str() for v in self]) 171 172 def getValue(self): 173 return [ v.getValue() for v in self ] 174 175 def unproxy(self, base): 176 return [v.unproxy(base) for v in self] --- 41 unchanged lines hidden (view full) --- 218 219 def __getattr__(self, attr): 220 if self.ptype_str: 221 attr = self.ptype_str + '.' + attr 222 return ParamFactory(self.param_desc_class, attr) 223 224 # E.g., Param.Int(5, "number of widgets") 225 def __call__(self, *args, **kwargs): |
220 caller_frame = inspect.currentframe().f_back | |
221 ptype = None 222 try: | 226 ptype = None 227 try: |
223 ptype = eval(self.ptype_str, 224 caller_frame.f_globals, caller_frame.f_locals) 225 if not isinstance(ptype, type): 226 raise TypeError, \ 227 "Param qualifier is not a type: %s" % ptype 228 except NameError: | 228 ptype = allParams[self.ptype_str] 229 except KeyError: |
229 # if name isn't defined yet, assume it's a SimObject, and 230 # try to resolve it later 231 pass 232 return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs) 233 234Param = ParamFactory(ParamDesc) 235VectorParam = ParamFactory(VectorParamDesc) 236 --- 58 unchanged lines hidden (view full) --- 295 296 def __sub__(self, other): 297 newobj = self.__class__(self) 298 newobj.value -= other 299 newobj._check() 300 return newobj 301 302# Metaclass for bounds-checked integer parameters. See CheckedInt. | 230 # if name isn't defined yet, assume it's a SimObject, and 231 # try to resolve it later 232 pass 233 return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs) 234 235Param = ParamFactory(ParamDesc) 236VectorParam = ParamFactory(VectorParamDesc) 237 --- 58 unchanged lines hidden (view full) --- 296 297 def __sub__(self, other): 298 newobj = self.__class__(self) 299 newobj.value -= other 300 newobj._check() 301 return newobj 302 303# Metaclass for bounds-checked integer parameters. See CheckedInt. |
303class CheckedIntType(type): | 304class CheckedIntType(MetaParamValue): |
304 def __init__(cls, name, bases, dict): 305 super(CheckedIntType, cls).__init__(name, bases, dict) 306 307 # CheckedInt is an abstract base class, so we actually don't 308 # want to do any processing on it... the rest of this code is 309 # just for classes that derive from CheckedInt. 310 if name == 'CheckedInt': 311 return --- 107 unchanged lines hidden (view full) --- 419 self._check() 420 def __add__(self, other): 421 if isinstance(other, Addr): 422 return self.value + other.value 423 else: 424 return self.value + other 425 426 | 305 def __init__(cls, name, bases, dict): 306 super(CheckedIntType, cls).__init__(name, bases, dict) 307 308 # CheckedInt is an abstract base class, so we actually don't 309 # want to do any processing on it... the rest of this code is 310 # just for classes that derive from CheckedInt. 311 if name == 'CheckedInt': 312 return --- 107 unchanged lines hidden (view full) --- 420 self._check() 421 def __add__(self, other): 422 if isinstance(other, Addr): 423 return self.value + other.value 424 else: 425 return self.value + other 426 427 |
427class MetaRange(type): | 428class MetaRange(MetaParamValue): |
428 def __init__(cls, name, bases, dict): 429 super(MetaRange, cls).__init__(name, bases, dict) 430 if name == 'Range': 431 return 432 cls.cxx_type = 'Range< %s >' % cls.type.cxx_type 433 cls.cxx_predecls = \ 434 ['#include "base/range.hh"'] + cls.type.cxx_predecls 435 --- 224 unchanged lines hidden (view full) --- 660# What Enum() must do is generate a new type encapsulating the 661# provided list/dictionary so that specific values of the parameter 662# can be instances of that type. We define two hidden internal 663# classes (_ListEnum and _DictEnum) to serve as base classes, then 664# derive the new type from the appropriate base class on the fly. 665 666allEnums = {} 667# Metaclass for Enum types | 429 def __init__(cls, name, bases, dict): 430 super(MetaRange, cls).__init__(name, bases, dict) 431 if name == 'Range': 432 return 433 cls.cxx_type = 'Range< %s >' % cls.type.cxx_type 434 cls.cxx_predecls = \ 435 ['#include "base/range.hh"'] + cls.type.cxx_predecls 436 --- 224 unchanged lines hidden (view full) --- 661# What Enum() must do is generate a new type encapsulating the 662# provided list/dictionary so that specific values of the parameter 663# can be instances of that type. We define two hidden internal 664# classes (_ListEnum and _DictEnum) to serve as base classes, then 665# derive the new type from the appropriate base class on the fly. 666 667allEnums = {} 668# Metaclass for Enum types |
668class MetaEnum(type): | 669class MetaEnum(MetaParamValue): |
669 def __new__(mcls, name, bases, dict): 670 assert name not in allEnums 671 672 cls = super(MetaEnum, mcls).__new__(mcls, name, bases, dict) 673 allEnums[name] = cls 674 return cls 675 676 def __init__(cls, name, bases, init_dict): --- 491 unchanged lines hidden --- | 670 def __new__(mcls, name, bases, dict): 671 assert name not in allEnums 672 673 cls = super(MetaEnum, mcls).__new__(mcls, name, bases, dict) 674 allEnums[name] = cls 675 return cls 676 677 def __init__(cls, name, bases, init_dict): --- 491 unchanged lines hidden --- |