Package pygccxml :: Package declarations :: Module enumeration

Source Code for Module pygccxml.declarations.enumeration

  1  # Copyright 2004-2008 Roman Yakovenko. 
  2  # Distributed under the Boost Software License, Version 1.0. (See 
  3  # accompanying file LICENSE_1_0.txt or copy at 
  4  # http://www.boost.org/LICENSE_1_0.txt) 
  5   
  6  """ 
  7  defines class, that describes C++ enum 
  8  """ 
  9   
 10  import copy 
 11  import types 
 12  import compilers 
 13  import declaration 
 14   
15 -class enumeration_t( declaration.declaration_t ):
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 # A list of tuples (valname(str), valnum(int)). The order of the list should 35 # be the same as the order in the C/C++ source file. 36 self._values = [] 37 38 # Initialize values via property access 39 self.values = values 40 self._byte_size = 0 41 self._byte_align = 0
42
43 - def __eq__(self, other):
44 if not declaration.declaration_t.__eq__( self, other ): 45 return False 46 return self.values == other.values
47
48 - def _get__cmp__items( self ):
49 """implementation details""" 50 return [self.values]
51
52 - def _get_values(self):
53 return copy.copy(self._values)
54 - def _set_values(self, values):
55 self._values = [] 56 # None is treated like an empty list 57 if (values==None): 58 return 59 # Check that we have indeed a list... 60 if type(values)!=list: 61 raise ValueError, "'values' must be a list (got a %s instead)"%type(values).__name__ 62 # Append the items individually. This has the effect that there's 63 # some additional type checking and that a copy of 'values' is stored 64 # and the caller cannot further manipulate the list via his own reference 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
77 - def append_value(self, valuename, valuenum=None):
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 # No number given? Then use the previous one + 1 92 if valuenum==None: 93 if len(self._values)==0: 94 valuenum = 0 95 else: 96 valuenum = self._values[-1][1]+1 97 98 # Store the new value 99 self._values.append((valuename, int(valuenum)))
100
101 - def has_value_name(self, name):
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
113 - def get_name2value_dict( self ):
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
120 - def i_depend_on_them( self, recursive=True ):
121 return []
122
123 - def _get_byte_size(self):
124 return self._byte_size
125 - def _set_byte_size( self, new_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
130 - def _get_byte_align(self):
131 if self.compiler == compilers.MSVC_PDB_9: 132 compilers.on_missing_functionality( self.compiler, "byte align" ) 133 return self._byte_align
134 - def _set_byte_align( self, new_byte_align ):
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