1   
  2   
  3   
  4   
  5   
  6  """ 
  7  defines class, that describes C++ enum 
  8  """ 
  9   
 10  import copy 
 11  import types 
 12  import compilers 
 13  import declaration 
 14   
 16      """ 
 17      describes C++ enum 
 18      """ 
 19 -    def __init__( self, name='', values=None ): 
  20          """creates class that describes C++ enum declaration 
 21   
 22          The items of the list 'values' may either be strings containing 
 23          the enumeration value name or tuples (name, numvalue). 
 24   
 25          @param name: Enum name 
 26          @type name: str 
 27          @param parent: Parent declaration 
 28          @type parent: declaration_t 
 29          @param values: Enumeration values 
 30          @type values: list 
 31          """ 
 32          declaration.declaration_t.__init__( self, name ) 
 33   
 34           
 35           
 36          self._values = [] 
 37   
 38           
 39          self.values = values 
 40          self._byte_size = 0 
 41          self._byte_align = 0 
  42   
 47   
 49          """implementation details""" 
 50          return [self.values] 
  51   
 53          return copy.copy(self._values) 
  55          self._values = [] 
 56           
 57          if (values==None): 
 58              return 
 59           
 60          if type(values)!=list: 
 61              raise ValueError, "'values' must be a list (got a %s instead)"%type(values).__name__ 
 62           
 63           
 64           
 65          for item in values: 
 66              if isinstance(item, types.StringTypes): 
 67                  self.append_value(item) 
 68              elif type(item)==tuple: 
 69                  name,num = item 
 70                  self.append_value(name, num) 
 71              else: 
 72                  raise ValueError, "'values' contains an invalid item: %s"%item 
  73      values = property( _get_values, _set_values 
 74                         , doc="""A list of tuples (valname(str), valnum(int)) that contain the enumeration values. 
 75                         @type: list""") 
 76   
 78          """Append another enumeration value to the enum. 
 79   
 80          The numeric value may be None in which case it is automatically determined by 
 81          increasing the value of the last item. 
 82   
 83          When the 'values' attribute is accessed the resulting list will be in the same 
 84          order as append_value() was called. 
 85   
 86          @param valuename: The name of the value. 
 87          @type valuename: str 
 88          @param valuenum: The numeric value or None. 
 89          @type valuenum: int 
 90          """ 
 91           
 92          if valuenum==None: 
 93              if len(self._values)==0: 
 94                  valuenum = 0 
 95              else: 
 96                  valuenum = self._values[-1][1]+1 
 97   
 98           
 99          self._values.append((valuename, int(valuenum))) 
 100   
102          """Check if this enum has a particular name among its values. 
103   
104          @param name: Enumeration value name 
105          @type name: str 
106          @return: True if there is an enumeration value with the given name 
107          """ 
108          for val,num in self._values: 
109              if val==name: 
110                  return True 
111          return False 
 112   
114          """returns a dictionary, that maps between enum name( key ) and enum value( value )""" 
115          x = {} 
116          for val, num in self._values: 
117              x[val] = num 
118          return x 
 119   
122   
124          return self._byte_size 
 126          self._byte_size = new_byte_size 
 127      byte_size = property( _get_byte_size, _set_byte_size 
128                            , doc="Size of this class in bytes @type: int") 
129   
135          self._byte_align = new_byte_align 
 136      byte_align = property( _get_byte_align, _set_byte_align 
137                            , doc="Alignment of this class in bytes @type: int") 
 138