blob: dfcd1fc8837ab2a07d73a89de4bcdc26fd609dd3 [file] [log] [blame]
Rich Lane9d98adf2013-11-29 18:37:24 -08001:: superclass_pyname = ofclass.superclass.pyname if ofclass.superclass else "loxi.OFObject"
Rich Lane002e70c2013-05-09 17:08:16 -07002:: from loxi_ir import *
Rich Laneda11f8b2013-06-19 15:53:25 -07003:: import py_gen.oftype
Rich Lane038c9e62014-01-27 15:33:18 -08004:: import py_gen.util as util
Rich Lanea2de2192013-11-29 17:58:32 -08005:: type_members = [m for m in ofclass.members if type(m) == OFTypeMember]
Rich Lane038c9e62014-01-27 15:33:18 -08006:: normal_members = [m for m in ofclass.members if type(m) == OFDataMember or
7:: type(m) == OFDiscriminatorMember]
8:: if ofclass.virtual:
9:: discriminator_fmts = { 1: "B", 2: "!H", 4: "!L" }
10:: discriminator_fmt = discriminator_fmts[ofclass.discriminator.length]
11:: #endif
Rich Lane9d98adf2013-11-29 18:37:24 -080012class ${ofclass.pyname}(${superclass_pyname}):
Rich Lane038c9e62014-01-27 15:33:18 -080013:: if ofclass.virtual:
14 subtypes = {}
15
16:: #endif
Rich Lanea2de2192013-11-29 17:58:32 -080017:: for m in type_members:
Rich Laned1dd9e62013-02-20 18:28:01 -080018 ${m.name} = ${m.value}
19:: #endfor
20
Rich Lane8ca3b772013-04-30 13:36:55 -070021 def __init__(${', '.join(['self'] + ["%s=None" % m.name for m in normal_members])}):
22:: for m in normal_members:
Rich Laned1dd9e62013-02-20 18:28:01 -080023 if ${m.name} != None:
24 self.${m.name} = ${m.name}
25 else:
Rich Lane0f7f9d52013-11-29 17:00:49 -080026:: if m.name == 'xid':
27:: # HACK for message xid
28 self.${m.name} = None
29:: else:
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070030 self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype, version=version)}
Rich Lane0f7f9d52013-11-29 17:00:49 -080031:: #endif
Rich Laned1dd9e62013-02-20 18:28:01 -080032:: #endfor
33 return
34
35 def pack(self):
36 packed = []
37:: include("_pack.py", ofclass=ofclass)
38 return ''.join(packed)
39
40 @staticmethod
Rich Lane5b2745c2013-11-30 00:36:24 -080041 def unpack(reader):
Rich Lane038c9e62014-01-27 15:33:18 -080042:: if ofclass.virtual:
43 subtype, = reader.peek(${repr(discriminator_fmt)}, ${ofclass.discriminator.offset})
44 subclass = ${ofclass.pyname}.subtypes.get(subtype)
45 if subclass:
46 return subclass.unpack(reader)
47
48:: #endif
Rich Laned1dd9e62013-02-20 18:28:01 -080049 obj = ${ofclass.pyname}()
50:: include("_unpack.py", ofclass=ofclass)
51 return obj
52
53 def __eq__(self, other):
54 if type(self) != type(other): return False
Rich Lane8ca3b772013-04-30 13:36:55 -070055:: for m in normal_members:
Rich Laned1dd9e62013-02-20 18:28:01 -080056 if self.${m.name} != other.${m.name}: return False
57:: #endfor
58 return True
59
Rich Laned1dd9e62013-02-20 18:28:01 -080060 def pretty_print(self, q):
61:: include('_pretty_print.py', ofclass=ofclass)
Rich Lane78a3bc42013-11-29 19:22:08 -080062
63:: # Register with our superclass
64:: if ofclass.superclass:
65:: type_field_name = ofclass.superclass.discriminator.name
66:: type_value = ofclass.member_by_name(type_field_name).value
67${superclass_pyname}.subtypes[${type_value}] = ${ofclass.pyname}
68:: #endif