Original Post
Just as a little side project ive been working on my own stream class. Basically it works like this cStream MyStream; MyStream << MyInt << MyFloat << MyDouble; MyStream >> MyInt >> MyFloat >> MyDouble; Its FIFO and works well for basic data types/class. At the heart of it are two templated operator overloads for << and >> Example: To extend the class further ive been toying with the idea of having more complex classes (e.g. ones which have memory on the heap) working with the stream. To do this I thought about haveing a Serialise/Deserialise (for want of better names) function for the class which gets passed a reference to the stream. Then in this function the class adds all its internal data into the stream and the deserialise function will retrieve it. This works well enough but I wanted to to be able to use the same syntax as adding simple types to the stream as more complex ones e.g. MyStream << MyInt << MyComplexClass; To accomplish this I created a base class (cStreamSerialiseBase) with virtual Serialise/Deserialise methods which can then be overloaded by the inheriting class. Then I created an overloaded << and >> function which takes a reference to a cStreamSerialiseBase class e.g. Unfortuantly this doesnt work. The class just calls the original templated << and >> instead of the one above. Im sure there is a way to do this but I cant think of another way to acheive what I want. If anyone has any help they can offer Id be very grateful:) Cheers!
template<class T>
cStream& operator<<(T& rhs)
{
//Get data size and check available
//space
int iSpaceToAdd = 0;
int iSize = sizeof(rhs);
if(this->bDynamicSize == true && this->iMemoryLeft < iSize)
{
this->ResizeStream(iSize);
}
if(this->iMemoryLeft >= iSize)
{
//Stream big enough, copy data in.
this->CopyDataBlockToStream((&rhs),iSize);
}
//Return a reference to ourselves
return(*this);
}
cStream& operator<<(cStreamSerialiseBase& Class)
{
Class.Serialise((*this));
return(*this);
}