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
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
79# Dummy base class to identify types that are legitimate for SimObject
80# parameters.
81class ParamValue(object):
82 __metaclass__ = MetaParamValue
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
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
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
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:
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):
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):
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):
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 ---