H
hurcan solter
consider the simplifed code fragment;
enum Endianness
{
BigEndian, ///<BigEndian
LittleEndian,///< LittleEndian
#if defined(BOOST_BIG_ENDIAN)
HostEndianness = BigEndian ///< Current host's Endiannnes.
#elif defined(BOOST_LITTLE_ENDIAN)
HostEndianness = LittleEndian
#else
#error we only supports big and little endian systems.
#endif
};
class ByteWrapper{
public:
template<typename T>
void put(T& t)
{
verify(sizeof t);
memcpy(&_buffer[_pos],reinterpret_cast< unsigned char*>(&t),sizeof
t);
_pos+=sizeof t;
}
template<typename T>
T get()
{
verify(sizeof(T));
T retval;
memcpy(&retval,&_buffer[_pos],sizeof(T));
_pos+=sizeof(T);
return retval;
}
private:
vector<unsigned char> _buffer;
size_t _pos;
};
template<typename T,Endianness Endian=LittleEndian>
class HLAgeneric
{
public:
void encode(ByteWrapper& byteWrapper)
{
if (HostEndianness!=Endian)
{
BYTESWAP(_value);
byteWrapper.put<T>(_value);
BYTESWAP(_value);
return;
}
byteWrapper.put<T>(_value);
}
void decode(ByteWrapper& byteWrapper)
{
_value=byteWrapper.get<T>();
if (HostEndianness!=Endian)
{
BYTESWAP(_value);
}
}
private:
T _value;
};
Well, Bytewrapper class is responsible for extracting and inserting
data from a network buffer, the thing is
for some floating point values, the data extracted from the buffer
could be NAN on the host machine(1.QNAN on my platform) because of the
Endianness.HLAgeneric can sort out the Endianness issue.However when i
get<T>() from ByteWrapper the memory representation of the returned
value changes.(it only changes in one bit but that's enough for
malfunction). I think it undergoes to some conversion, but i expected
it to be copied bitwise since the types are the same. What is the
expected behaviour here? Can I assign NAN to another variable ? if
not, how can i approach the problem?. I thought of specializing get
and creating retval on the heap but it won't fit or look good with the
rest of design.
Any pointers would be greatly appreciated...
enum Endianness
{
BigEndian, ///<BigEndian
LittleEndian,///< LittleEndian
#if defined(BOOST_BIG_ENDIAN)
HostEndianness = BigEndian ///< Current host's Endiannnes.
#elif defined(BOOST_LITTLE_ENDIAN)
HostEndianness = LittleEndian
#else
#error we only supports big and little endian systems.
#endif
};
class ByteWrapper{
public:
template<typename T>
void put(T& t)
{
verify(sizeof t);
memcpy(&_buffer[_pos],reinterpret_cast< unsigned char*>(&t),sizeof
t);
_pos+=sizeof t;
}
template<typename T>
T get()
{
verify(sizeof(T));
T retval;
memcpy(&retval,&_buffer[_pos],sizeof(T));
_pos+=sizeof(T);
return retval;
}
private:
vector<unsigned char> _buffer;
size_t _pos;
};
template<typename T,Endianness Endian=LittleEndian>
class HLAgeneric
{
public:
void encode(ByteWrapper& byteWrapper)
{
if (HostEndianness!=Endian)
{
BYTESWAP(_value);
byteWrapper.put<T>(_value);
BYTESWAP(_value);
return;
}
byteWrapper.put<T>(_value);
}
void decode(ByteWrapper& byteWrapper)
{
_value=byteWrapper.get<T>();
if (HostEndianness!=Endian)
{
BYTESWAP(_value);
}
}
private:
T _value;
};
Well, Bytewrapper class is responsible for extracting and inserting
data from a network buffer, the thing is
for some floating point values, the data extracted from the buffer
could be NAN on the host machine(1.QNAN on my platform) because of the
Endianness.HLAgeneric can sort out the Endianness issue.However when i
get<T>() from ByteWrapper the memory representation of the returned
value changes.(it only changes in one bit but that's enough for
malfunction). I think it undergoes to some conversion, but i expected
it to be copied bitwise since the types are the same. What is the
expected behaviour here? Can I assign NAN to another variable ? if
not, how can i approach the problem?. I thought of specializing get
and creating retval on the heap but it won't fit or look good with the
rest of design.
Any pointers would be greatly appreciated...