Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 1 | :: superclass_pyname = ofclass.superclass.pyname if ofclass.superclass else "loxi.OFObject" |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 2 | :: from loxi_ir import * |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 3 | :: import py_gen.oftype |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 4 | :: import py_gen.util as util |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame] | 5 | :: type_members = [m for m in ofclass.members if type(m) == OFTypeMember] |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 6 | :: 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 Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 12 | class ${ofclass.pyname}(${superclass_pyname}): |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 13 | :: if ofclass.virtual: |
| 14 | subtypes = {} |
| 15 | |
| 16 | :: #endif |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame] | 17 | :: for m in type_members: |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 18 | ${m.name} = ${m.value} |
| 19 | :: #endfor |
| 20 | |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 21 | def __init__(${', '.join(['self'] + ["%s=None" % m.name for m in normal_members])}): |
| 22 | :: for m in normal_members: |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 23 | if ${m.name} != None: |
| 24 | self.${m.name} = ${m.name} |
| 25 | else: |
Rich Lane | 0f7f9d5 | 2013-11-29 17:00:49 -0800 | [diff] [blame] | 26 | :: if m.name == 'xid': |
| 27 | :: # HACK for message xid |
| 28 | self.${m.name} = None |
| 29 | :: else: |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 30 | self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype, version=version)} |
Rich Lane | 0f7f9d5 | 2013-11-29 17:00:49 -0800 | [diff] [blame] | 31 | :: #endif |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 32 | :: #endfor |
| 33 | return |
| 34 | |
| 35 | def pack(self): |
| 36 | packed = [] |
| 37 | :: include("_pack.py", ofclass=ofclass) |
| 38 | return ''.join(packed) |
| 39 | |
| 40 | @staticmethod |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 41 | def unpack(reader): |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 42 | :: 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 Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 49 | 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 Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 55 | :: for m in normal_members: |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 56 | if self.${m.name} != other.${m.name}: return False |
| 57 | :: #endfor |
| 58 | return True |
| 59 | |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 60 | def pretty_print(self, q): |
| 61 | :: include('_pretty_print.py', ofclass=ofclass) |
Rich Lane | 78a3bc4 | 2013-11-29 19:22:08 -0800 | [diff] [blame] | 62 | |
| 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 |