import dds
import ddsutil
import struct

class PointField___TypeSupport(dds.TypeSupport):

    _typename = 'sensor_msgs::msg::dds_::PointField_'
    _keylist = ''
    _metadescriptor = '<MetaData version="1.0.0"><Module name="sensor_msgs"><Module name="msg"><Module name="dds_"><Struct name="PointField_"><Member name="name_"><String/></Member><Member name="offset_"><ULong/></Member><Member name="datatype_"><Octet/></Member><Member name="count_"><ULong/></Member></Struct></Module></Module></Module></MetaData>'

    def __init__(self):
        super(PointField___TypeSupport, self).__init__(self._typename,
              self._keylist,
              self._metadescriptor,
              struct.calcsize(PointField_()._get_packing_fmt()))

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

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

class PointField_(ddsutil.TopicDataClass):

    @staticmethod
    def get_type_support():
        ''' Return type support information required for dds.Topic constructor '''
        return PointField___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 offset_(self):
        '''
        a DDS unsigned long value
        @rtype: int
        '''
        return self._offset__

    _offset__checker = ddsutil._ulong_checker
    @offset_.setter
    def offset_(self, value):
        self._offset__checker(value)
        self._offset__ = value

    @property
    def datatype_(self):
        '''
        a DDS octet value
        @rtype: int
        '''
        return self._datatype__

    _datatype__checker = ddsutil._octet_checker
    @datatype_.setter
    def datatype_(self, value):
        self._datatype__checker(value)
        self._datatype__ = value

    @property
    def count_(self):
        '''
        a DDS unsigned long value
        @rtype: int
        '''
        return self._count__

    _count__checker = ddsutil._ulong_checker
    @count_.setter
    def count_(self, value):
        self._count__checker(value)
        self._count__ = value

    _member_attributes = ('name_', 'offset_', 'datatype_', 'count_')

    def __init__(self, **kwargs):
        ''' Create a PointField_ instance, optionally initializing fields by name '''
        # init attributes to default value
        self.name_ = ''
        self.offset_ = 0
        self.datatype_ = 0
        self.count_ = 0
        # 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 PointField_._packing_fmt is not None:
            return PointField_._packing_fmt
        fmt = ''
        fmt += 'P'
        fmt += 'I'
        fmt += 'B'
        fmt += 'I'

        PointField_._packing_fmt = ddsutil._pad_fmt(fmt)
        return PointField_._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.offset_ = data.pop(0)
        self.datatype_ = data.pop(0)
        self.count_ = 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.append(self.offset_)
        args.append(self.datatype_)
        args.append(self.count_)
        return args

