Auto-byte-swapping that can be used in a union [modified]
-
We've got a bunch of apps that need to prepare data for a machine that uses big-endian byte order. So we have a bunch of structs that look like:
struct tagFOO_COMREG
{
UINT16 ui16DataLength_BE;
UINT32 ui32SeqNum_BE;
// ...
} FOO_COMREG_s, *PFOO_COMREG_s;where the _BE is a reminder that these values have to be byte-swapped. So, one day I thought it would be convenient to be able to assign to one of the struct members without having to explicitly do the byte swapping. So I wrote some classes like:
class UINT16_BE
{
private:
BYTE m_abData[2];
public:
UINT16_BE(void)
{
// do nothing
}
UINT16_BE(UINT16 ui16)
{
m_abData[0] = static_cast<BYTE>(ui16 >> 8);
m_abData[1] = static_cast<BYTE>(ui16);
}
operator UINT16(void)
{
return ((m_abData[0] << 8) | m_abData[1]);
}
};And it works fine. Until my co-worker comes up to me asking how to fix a compiler error he's having. He's trying to put these in a union, which doesn't work because the constructors keep it from being recognized as POD (despite the fact that the class was designed to be memcpy-able). So, is there a way around this?
modified on Monday, February 11, 2008 2:13 PM
-
We've got a bunch of apps that need to prepare data for a machine that uses big-endian byte order. So we have a bunch of structs that look like:
struct tagFOO_COMREG
{
UINT16 ui16DataLength_BE;
UINT32 ui32SeqNum_BE;
// ...
} FOO_COMREG_s, *PFOO_COMREG_s;where the _BE is a reminder that these values have to be byte-swapped. So, one day I thought it would be convenient to be able to assign to one of the struct members without having to explicitly do the byte swapping. So I wrote some classes like:
class UINT16_BE
{
private:
BYTE m_abData[2];
public:
UINT16_BE(void)
{
// do nothing
}
UINT16_BE(UINT16 ui16)
{
m_abData[0] = static_cast<BYTE>(ui16 >> 8);
m_abData[1] = static_cast<BYTE>(ui16);
}
operator UINT16(void)
{
return ((m_abData[0] << 8) | m_abData[1]);
}
};And it works fine. Until my co-worker comes up to me asking how to fix a compiler error he's having. He's trying to put these in a union, which doesn't work because the constructors keep it from being recognized as POD (despite the fact that the class was designed to be memcpy-able). So, is there a way around this?
modified on Monday, February 11, 2008 2:13 PM
ClementsDan wrote:
He's trying to put these in a union
The constructor's not going to work there...maybe something like this will work...
class UINT16_BE
{
private:
BYTE m_abData[2];
public:
UINT16_BE &operator =(UINT16 ui16)
{
m_abData[0] = static_cast(ui16 >> 8);
m_abData[1] = static_cast(ui16);
return *this;
}
operator UINT16() const
{
return ((m_abData[0] << 8) | m_abData[1]);
}
};Mark
Last modified: 22mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
ClementsDan wrote:
He's trying to put these in a union
The constructor's not going to work there...maybe something like this will work...
class UINT16_BE
{
private:
BYTE m_abData[2];
public:
UINT16_BE &operator =(UINT16 ui16)
{
m_abData[0] = static_cast(ui16 >> 8);
m_abData[1] = static_cast(ui16);
return *this;
}
operator UINT16() const
{
return ((m_abData[0] << 8) | m_abData[1]);
}
};Mark
Last modified: 22mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Fixed! ;P