I have a BinaryMemoryReader class that has a function defined like this:
template <typename T>
inline void read(T **data) {
*data = (T*)&stream[position];
position += sizeof(T);
}
It handles most of the standard types from a stream in memory.
The custom complex types have read functions of their own, such as:
void AnotherClass::read(BinaryMemoryReader *const reader)
{
reader->read<bool>(&num);
reader->read<short>(&count);
}
What I'd like to is write the code that would, when I write
reader->read<AnotherClass>(&var);
would call the second function. It would mean that AnotherClasss read function would overload the BinaryMemoryReaders read function for that particular type.
It would allow me to write a much more cleaner code.
EDIT: The general idea is this:
- Have a central
BinaryMemoryReaderclass. - Have a generic read function within it to deal with the standard types.
- Have the syntax to call it be
bmr->read<int>(&intVar); - Have specialized read functions defined in their own classes.
- Most importantly, the syntax to call them would be
bmr->read<customType>(&customTypeVar);
That way the specific read functions would be associated with their own classes, but would be able to be called from the BinaryMemoryReader.
BinaryMemoryReader.BinaryMemoryReaderparameter and aT? This one could be easily specialized in any other class module.readfunction in the global namespace, but I'm not sure how to achieve this in your case with pointers and such.BinaryMemoryReaderclass? Would there be a way to have the same call for the standard types and the custom types?