import dds
import ddsutil
import struct

class String___TypeSupport(dds.TypeSupport):

    _typename = 'std_msgs::msg::dds_::String_'
    _keylist = ''
    _metadescriptor = '<MetaData version="1.0.0"><Module name="std_msgs"><Module name="msg"><Module name="dds_"><Struct name="String_"><Member name="data_"><String/></Member></Struct></Module></Module></Module></MetaData>'

    def __init__(self):
        super(String___TypeSupport, self).__init__(self._typename,
              self._keylist,
              self._metadescriptor,
              struct.calcsize(String_()._get_packing_fmt()))

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

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

class String_(ddsutil.TopicDataClass):

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

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

    _data__checker = ddsutil._str_checker
    @data_.setter
    def data_(self, value):
        self._data__checker(value)
        self._data__ = value

    _member_attributes = ('data_')

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

        String_._packing_fmt = ddsutil._pad_fmt(fmt)
        return String_._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.data_ = ddsutil._ptr_to_bytes(data.pop(0)).decode('ISO-8859-1')
        return self;

    def _get_packing_args(self):
        ''' Return an list of data suitable for struct.pack '''
        args = []
        args.append(ddsutil._bytes_to_ptr(self.data_.encode('ISO-8859-1')))
        return args

