PDA

View Full Version : How to send enum through network?



Hostel
20th October 2010, 01:21
I need send an enum through network. I have idea how to do this, but I want to know yours solutions for this. Maybye there is better way than my.

My idea:


// h
Q_PROPERTY( Operation operation READ operation WRITE setOperation )
Q_ENUMS( Operation );

enum Operation
{
A,
B
};

// cpp
QDataStrem in( &tcpSocket );
// waiting for all data
QString sTmpTypOperacji;
Operation eTypOperacji;

in >> sTmpTypOperacji;

const QMetaObject* metaObj = this->metaObject();
int iEnum = metaObj->indexOfEnumerator( "Operation" );
QMetaEnum enumOper = metaObj->enumerator( iEnum );

eTypOperacji = (Operation)enumOper.keyToValue( sTmpTypOperacji.toLatin1() );

Are there other ways to send enum?

tbscope
20th October 2010, 04:35
The enum you have is just a list of numbers.
A = 0
B = 1
etc...

use qDebug() << "My enum =" << theEnum; to see the value

Just send the number via your network. On the other end cast it back to the enum again.

Hostel
20th October 2010, 17:52
Thanks for reply. I have another question. Enum is qint32 type or other? Int could be a different size on different os so when I read int which represent enum, so which type of int will be safe?

Timoteo
20th October 2010, 18:18
The enum you have is just a list of numbers.
A = 0
B = 1
etc...

use qDebug() << "My enum =" << theEnum; to see the value

Just send the number via your network. On the other end cast it back to the enum again.

No, don't. The size of an enum is undefined. Instead, pick a portable type (such as quint8) and use that instead.

squidge
21st October 2010, 07:54
Just cast the enum to a quint32 and send that through the network. If your using that enum constantly, then you might want to investigate more to determine the minimum and maximum values, but you would then have to ensure that it doesn't overflow in the future, so quint32 would be a better option.