import dds
import ddsutil
import struct

class Duration___TypeSupport(dds.TypeSupport):

    _typename = 'builtin_interfaces::msg::dds_::Duration_'
    _keylist = ''
    _metadescriptor = '<MetaData version="1.0.0"><Module name="builtin_interfaces"><Module name="msg"><Module name="dds_"><Struct name="Duration_"><Member name="sec_"><Long/></Member><Member name="nanosec_"><ULong/></Member></Struct></Module></Module></Module></MetaData>'

    def __init__(self):
        super(Duration___TypeSupport, self).__init__(self._typename,
              self._keylist,
              self._metadescriptor,
              struct.calcsize(Duration_()._get_packing_fmt()))

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

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

class Duration_(ddsutil.TopicDataClass):

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

    @property
    def sec_(self):
        '''
        a DDS long value
        @rtype: int
        '''
        return self._sec__

    _sec__checker = ddsutil._long_checker
    @sec_.setter
    def sec_(self, value):
        self._sec__checker(value)
        self._sec__ = value

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

    _nanosec__checker = ddsutil._ulong_checker
    @nanosec_.setter
    def nanosec_(self, value):
        self._nanosec__checker(value)
        self._nanosec__ = value

    _member_attributes = ('sec_', 'nanosec_')

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

        Duration_._packing_fmt = ddsutil._pad_fmt(fmt)
        return Duration_._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.sec_ = data.pop(0)
        self.nanosec_ = data.pop(0)
        return self;

    def _get_packing_args(self):
        ''' Return an list of data suitable for struct.pack '''
        args = []
        args.append(self.sec_)
        args.append(self.nanosec_)
        return args

