import dds
import ddsutil
import struct

class ChannelFloat32___TypeSupport(dds.TypeSupport):

    _typename = 'sensor_msgs::msg::dds_::ChannelFloat32_'
    _keylist = ''
    _metadescriptor = '<MetaData version="1.0.0"><Module name="sensor_msgs"><Module name="msg"><Module name="dds_"><Struct name="ChannelFloat32_"><Member name="name_"><String/></Member><Member name="values_"><Sequence><Float/></Sequence></Member></Struct></Module></Module></Module></MetaData>'

    def __init__(self):
        super(ChannelFloat32___TypeSupport, self).__init__(self._typename,
              self._keylist,
              self._metadescriptor,
              struct.calcsize(ChannelFloat32_()._get_packing_fmt()))

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

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

class ChannelFloat32_(ddsutil.TopicDataClass):

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

    @property
    def name_(self):
        '''
        a DDS string value
        @rtype: str
        '''
        return self._name__

    _name__checker = ddsutil._str_checker
    @name_.setter
    def name_(self, value):
        self._name__checker(value)
        self._name__ = value

    @property
    def values_(self):
        '''
        a DDS sequence<float> value
        @rtype: list
        '''
        return self._values__

    _values__checker = ddsutil._seq_checker(0,ddsutil._float_checker)
    @values_.setter
    def values_(self, value):
        self._values__checker(value)
        self._values__ = value

    _member_attributes = ('name_', 'values_')

    def __init__(self, **kwargs):
        ''' Create a ChannelFloat32_ instance, optionally initializing fields by name '''
        # init attributes to default value
        self.name_ = ''
        self.values_ = []
        # 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 ChannelFloat32_._packing_fmt is not None:
            return ChannelFloat32_._packing_fmt
        fmt = ''
        fmt += 'P'
        fmt += ddsutil._align(fmt, ddsutil._pad_fmt('iiP?'))

        ChannelFloat32_._packing_fmt = ddsutil._pad_fmt(fmt)
        return ChannelFloat32_._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.name_ = ddsutil._ptr_to_bytes(data.pop(0)).decode('ISO-8859-1')
        self.values_ = ddsutil._deserialize_seq(data, 'f', 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.append(ddsutil._bytes_to_ptr(self.name_.encode('ISO-8859-1')))
        args.extend(ddsutil._serialize_seq(self.values_, 'f', lambda seq_args, v: seq_args.append(v)))
        return args

