Marshaling collection (array and etc.)

ProudNet is already set to direcly perform marshaling of a few of the basic collection (array and etc.) types by supporting Proud.CFastArray, std.vector and CAtlArray. Please refer to operator>>, operator<< override from marshaler.h for more details.

Following is the case showing how array type is being declared from PIDL.

Foo([in] Proud::CFastArray<MyType> a, [in] std::vector<MyType> b);

If there are any collection types in need of marsharling beyond the basic collection type defined in marshaler.h, override must be set to respond to those collection types. How you can override is guided in Using user-defined class type in RMI. If you want to see the implemented case of override, pleas refer to marshaler.h.

Next is the example of marshaling std.vector. If there are any collection types that ProudNet does not support as default then all you need to do is to create a marshaling routine as referring to below.

namespace Proud
{
// serialization functions that can be used in vector
// for output to stream
template<typename elem>
inline CMessage& operator>>(CMessage &a, std::vector<elem> &b)
{
// Gain size
int size;
a >> size;
// Prompt exception if the size is unacceptable. This could be caused by hacking.
if (size<0 ||size >= CNetConfig::MessageMaxLength)
ThrowExceptionOnReadArray(size);
// to reduce memory frag
b.reserve(size);
b.resize(0);
// read array one by one.
elem e;
for (int i = 0;i < size;i++)
{
a >> e;
b.push_back(e);
}
return a;
}
// serialization functions that can be used in vetor lists, unary itme elem and etc.
// for input from stream
template<typename elem>
inline CMessage& operator<<(CMessage &a, const std::vector<elem> &b)
{
// Write the size of array.
int size = (int)b.size();
a << size;
// Write each array factor.
for (std::vector<elem>::const_iterator i = b.begin();i != b.end();i++)
{
a << (*i);
}
return a;
}
template<typename elem>
inline void AppendTextOut(String &a, std::vector<elem> &b)
{
a += L"<vector>";
}
}