The reason for using the bit field in MFC is twofold.
conserve disk space.
I know how to serialize a DWORD. But how do I move or cast the 32 bit
"Greg G" <GregG@discussions.microsoft.com> ha scritto nel messaggio
news:C3C9643C-4F1E-4F33-927E-2DB90BADAD25@microsoft.com...
Where can I find example code showing serialization of a bit field in MFC?
I built a bit field class derived from CObject and see in the VC++
documentation how to IMPLEMENT_SERIAL. However, I can not seem to figure
out
how to write the
void CMyBitField::Serialize( CArchive& archive)
{
if( archive.IsStroring() )
{
archive << <unsigned int> bits << etc. ?????
}
else
{
archive >> ????
I don't understand what is the internal reresentation that you use for
bitfields.
Do you store bit fields into an unsigned int, which is a DWORD on 32-bits
architectures?
If so, I imagine you have a DWORD data member of your class, e.g. DWORD
m_dwBits;
so you just have to write/read this field in the serialization process, i.e.
something like this:
void CMyBitField::Serialize( CArchive& archive)
{
if( archive.IsStroring() )
{
archive << m_dwBits;
}
else
{
archive >> m_dwBits;
}
}
Of course, in this way you are limited to 32 bit fields.
You might also consider using the std::valarray<bool> specialization of
valarray template:
http://msdn2.microsoft.com/en-us/library/sch31sbx(VS.80).aspx
So, you might have a std::valarray<bool> instance into your CMyBitField
class, and for serialization you might write first the size of the valarray
(the element count, the number of bits), then the elements themself, e.g.
using code like so (not tested):
// std::valarray<bool> m_bits; // data member storing the bit fields
void CMyBitField::Serialize( CArchive& archive)
{
if( archive.IsStroring() )
{
// Write number of bits
archive << (DWORD)m_bits.size();
// Write the bit values
for ( size_t i = 0; i < m_bits.size(); i++ )
archive << m_bits[i];
}
else
{
// Read number of bits
DWORD bitCount;
archive >> bitCount;
ASSERT( bitCount != 0 );
// Read bit values
m_bits.resize( bitCount )
for ( size_t i = 0; i < numberOfBits; i++ )
archive >> m_bits[i];
}
}
}
However, you might consider using XML for serialization, too.
G