There is a method to marshal character information that differs depending on validity or invalidity of filed between character types. You can also have multi-methods of marshaling by taking leverage on switch/case characters or polymorphic objects. For easier understanding, we made an example of using swich/case as followed.
namespace Proud
{
enum UnitType
{
Zergling,
Queen,
Broodling,
};
struct Unit
{
UnitType m_type;
Vector2D m_position;
int m_energy;
float m_lifeTime;
int m_attackPower;
};
CMessage& operator<<(CMessage& msg,const Unit& unit)
{
msg<<unit.m_type<<unit.m_position;
switch(unit.m_type)
{
case Zergling:
msg<<unit.m_attackPower;
break;
case Queen:
msg<<unit.m_energy;
break;
case Broodling:
msg<<unit.m_lifeTime;
break;
}
return msg;
}
CMessage& operator>>(CMessage& msg,Unit& unit)
{
msg>>unit.m_type>>unit.m_position;
switch(unit.m_type)
{
case Zergling:
msg>>unit.m_attackPower;
break;
case Queen:
msg>>unit.m_energy;
break;
case Broodling:
msg>>unit.m_lifeTime;
break;
}
return msg;
}
}