Rich Lane | 78a3bc4 | 2013-11-29 19:22:08 -0800 | [diff] [blame] | 1 | :: import py_gen.util as util |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 2 | :: superclass_pyname = ofclass.superclass.pyname if ofclass.superclass else "loxi.OFObject" |
Rich Lane | 78a3bc4 | 2013-11-29 19:22:08 -0800 | [diff] [blame] | 3 | :: fmts = { 1: "B", 2: "!H", 4: "!L" } |
| 4 | :: fmt = fmts[ofclass.discriminator.length] |
| 5 | :: trail = ' '.join([x.pyname for x in util.ancestors(ofclass)]) |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 6 | class ${ofclass.pyname}(${superclass_pyname}): |
Rich Lane | 78a3bc4 | 2013-11-29 19:22:08 -0800 | [diff] [blame] | 7 | subtypes = {} |
| 8 | |
| 9 | @staticmethod |
| 10 | def unpack(reader): |
| 11 | subtype, = reader.peek(${repr(fmt)}, ${ofclass.discriminator.offset}) |
| 12 | try: |
| 13 | subclass = ${ofclass.pyname}.subtypes[subtype] |
| 14 | except KeyError: |
| 15 | raise loxi.ProtocolError("unknown ${trail} subtype %#x" % subtype) |
| 16 | return subclass.unpack(reader) |
| 17 | |
| 18 | :: # Register with our superclass |
| 19 | :: if ofclass.superclass: |
| 20 | :: type_field_name = ofclass.superclass.discriminator.name |
| 21 | :: type_value = ofclass.member_by_name(type_field_name).value |
| 22 | ${superclass_pyname}.subtypes[${type_value}] = ${ofclass.pyname} |
| 23 | :: #endif |