import dds
import ddsutil
import struct

class UInt8MultiArray___TypeSupport(dds.TypeSupport):

    _typename = 'std_msgs::msg::dds_::UInt8MultiArray_'
    _keylist = ''
    _metadescriptor = '<MetaData version="1.0.0"><Module name="std_msgs"><Module name="msg"><Module name="dds_"><Struct name="MultiArrayDimension_"><Member name="label_"><String/></Member><Member name="size_"><ULong/></Member><Member name="stride_"><ULong/></Member></Struct><Struct name="MultiArrayLayout_"><Member name="dim_"><Sequence><Type name="MultiArrayDimension_"/></Sequence></Member><Member name="data_offset_"><ULong/></Member></Struct><Struct name="UInt8MultiArray_"><Member name="layout_"><Type name="MultiArrayLayout_"/></Member><Member name="data_"><Sequence><Octet/></Sequence></Member></Struct></Module></Module></Module></MetaData>'

    def __init__(self):
        super(UInt8MultiArray___TypeSupport, self).__init__(self._typename,
              self._keylist,
              self._metadescriptor,
              struct.calcsize(UInt8MultiArray_()._get_packing_fmt()))

    def _serialize(self, o):
        if not isinstance(o, UInt8MultiArray_):
            raise TypeError('Incorrect data type')
        result = o._serialize()
        return result

    def _deserialize(self, buf):
        result = UInt8MultiArray_()
        data = struct.unpack(result._get_packing_fmt(), buf)
        result._deserialize(list(data))
        return result

class UInt8MultiArray_(ddsutil.TopicDataClass):

    @staticmethod
    def get_type_support():
        ''' Return type support information required for dds.Topic constructor '''
        return UInt8MultiArray___TypeSupport()

    @property
    def layout_(self):
        '''
        a DDS std_msgs::msg::dds_::MultiArrayLayout_ value
        @rtype: MultiArrayLayout_
        '''
        return self._layout__

    _layout__checker = ddsutil._class_checker(MultiArrayLayout_)
    @layout_.setter
    def layout_(self, value):
        self._layout__checker(value)
        self._layout__ = value

    @property
    def data_(self):
        '''
        a DDS sequence<octet> value
        @rtype: list
        '''
        return self._data__

    _data__checker = ddsutil._seq_checker(0,ddsutil._octet_checker)
    @data_.setter
    def data_(self, value):
        self._data__checker(value)
        self._data__ = value

    _member_attributes = ('layout_', 'data_')

    def __init__(self, **kwargs):
        ''' Create a UInt8MultiArray_ instance, optionally initializing fields by name '''
        # init attributes to default value
        self.layout_ = MultiArrayLayout_()
        self.data_ = []
        # set values for attributes passed in
        for key, value in kwargs.items():
            if key not in self._member_attributes:
                raise TypeError('Invalid argument name : %s' %(key))
            setattr(self, key, value)

    _packing_fmt = None

    @staticmethod
    def _get_packing_fmt():
        ''' Returns packing format of this class data '''
        if UInt8MultiArray_._packing_fmt is not None:
            return UInt8MultiArray_._packing_fmt
        fmt = ''
        fmt += ddsutil._align(fmt, MultiArrayLayout_._get_packing_fmt())
        fmt += ddsutil._align(fmt, ddsutil._pad_fmt('iiP?'))

        UInt8MultiArray_._packing_fmt = ddsutil._pad_fmt(fmt)
        return UInt8MultiArray_._packing_fmt

    def _serialize(self):
        ''' Serialize data for writing '''
        fmt = self._get_packing_fmt()
        args = self._get_packing_args()
        result = struct.pack(fmt, *args)
        return result

    def _deserialize(self, data):
        ''' Initialize values from data returned by struct.unpack()'''
        self.layout_ = MultiArrayLayout_()._deserialize(data)
        self.data_ = ddsutil._deserialize_seq(data, 'B', lambda seq_data: seq_data.pop(0))
        return self;

    def _get_packing_args(self):
        ''' Return an list of data suitable for struct.pack '''
        args = []
        args.extend(self.layout_._get_packing_args())
        args.extend(ddsutil._serialize_seq(self.data_, 'B', lambda seq_args, v: seq_args.append(v)))
        return args

