On 30/10/2017 at 8:04 AM, cozzie said:Thanks guys and sorry for the late response. I've adder the ReadXxx and WriteXxx helpers, the code's much cleaner now. I decided to stick with writing/ casting only the individual standard types (float, uint etc).
What I didn't figure out yet is how I can read a full chunk of data and "place" that in a std::vector. For example, say that I know that there are 100 floats in the file, would it be possible to read them all at once and place them in a std::vector<float> (with a size of 100)?
I'd add methods for doing that to you serializer/deserializer classes:
class Deserializer {
/* ... */
void readFloatArray(std::vector<float>* v) {
size_t arraySize = readUInt(); // we saved the size so we know how much to read
v->resize(arraySize);
for (size_t i = 0; i < arraySize; ++i)
v->push_back(readFloat());
}
/* ... */
};
class Serializer {
/* ... */
void writeFloatArray(const std::vector<float>& v) {
writeUInt(v.size()); // save size, for reading back
for (size_t i = 0; i < v.size(); ++i)
writeFloat(v[i]);
}
/* ... */
};
Then, to read the 100 floats:
File file;
file.open("whatever.dat");
std::vector<float> myFloats;
file.readFloatArray(&myFloats);