"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""

try:
    import cStringIO.StringIO as BytesIO
except ImportError:
    from io import BytesIO
import struct

import compas_lcmtypes.senlcm.prosilica_attribute_t

class prosilica_t(object):
    __slots__ = ["utime", "self", "n_attributes", "PvAttributes"]

    __typenames__ = ["int64_t", "boolean", "int32_t", "compas_lcmtypes.senlcm.prosilica_attribute_t"]

    __dimensions__ = [None, None, None, ["n_attributes"]]

    def __init__(self):
        self.utime = 0
        self.self = False
        self.n_attributes = 0
        self.PvAttributes = []

    def encode(self):
        buf = BytesIO()
        buf.write(prosilica_t._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        buf.write(struct.pack(">qbi", self.utime, self.self, self.n_attributes))
        for i0 in range(self.n_attributes):
            assert self.PvAttributes[i0]._get_packed_fingerprint() == compas_lcmtypes.senlcm.prosilica_attribute_t._get_packed_fingerprint()
            self.PvAttributes[i0]._encode_one(buf)

    def decode(data):
        if hasattr(data, 'read'):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != prosilica_t._get_packed_fingerprint():
            raise ValueError("Decode error")
        return prosilica_t._decode_one(buf)
    decode = staticmethod(decode)

    def _decode_one(buf):
        self = prosilica_t()
        self.utime = struct.unpack(">q", buf.read(8))[0]
        self.self = bool(struct.unpack('b', buf.read(1))[0])
        self.n_attributes = struct.unpack(">i", buf.read(4))[0]
        self.PvAttributes = []
        for i0 in range(self.n_attributes):
            self.PvAttributes.append(compas_lcmtypes.senlcm.prosilica_attribute_t._decode_one(buf))
        return self
    _decode_one = staticmethod(_decode_one)

    def _get_hash_recursive(parents):
        if prosilica_t in parents: return 0
        newparents = parents + [prosilica_t]
        tmphash = (0x84aedcc25441391a+ compas_lcmtypes.senlcm.prosilica_attribute_t._get_hash_recursive(newparents)) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _get_hash_recursive = staticmethod(_get_hash_recursive)
    _packed_fingerprint = None

    def _get_packed_fingerprint():
        if prosilica_t._packed_fingerprint is None:
            prosilica_t._packed_fingerprint = struct.pack(">Q", prosilica_t._get_hash_recursive([]))
        return prosilica_t._packed_fingerprint
    _get_packed_fingerprint = staticmethod(_get_packed_fingerprint)

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", prosilica_t._get_packed_fingerprint())[0]

