I'd want to write a fast code to load /save vectors of custom classes.
Both to-from memory and to-from disk.
I have (in example)
struct mydata { int one, double two }
vector <mydata> my_vector_of_data
Ok, I'cant write to disk the whole vector, isn't ? (without looping on it.. )
So, How can I do the work ? A stringstream object and << >> operations can be a way ?
Thanks
Use std::streams. The stream is a concept that has several implementations as you have seen: It can be a stream to a string, it can be a stream to a file, etc. Just write the inner state data of the class to a stream and you are done! I would define an IStream pure virtual class (BTW, COM has an IStream interface that you should use if you are developing COM).
class IStream
{
//Very simplified:
public:
virtualvoid Load(istream &is) = 0;
virtualvoid Save(ostream &os) = 0;
};
class MyClass : public IStream
{
....
//IStream:
public:
virtualvoid Load(istream &is)
{
//Code to load from input string goes here.
}
virtualvoid Save(ostream &os)
{
//Code to write to the stream goes here.
}
};
Now your code that has access to the vector or other collection can just iterate through the collection of objects, dynamic_cast<IStream&>, and just call Load() or Save() with the appropriately created string.
However in that case you will not have a compilation error, but run-time error (if you're lucky)
I think it is better to define a template of methods that the class will have in order to be serialised (like save and load methods).
Then create the operators << and >> against the streams.
To containers you could use copy.
1 2 3 4 5 6 7 8 9 10
template<class Iter>
void print(std::ostream &out, Iter begin, Iter end){
std::copy(
begin,
end,
std::ostream_iterator
<typename std::iterator_traits<Iter>::value_type>
(out, " ")
);
}
Write the same...? No, if all the containers/classes you use will compile when you substitute T with the container type, then you have no worries, unless you have a second personality that likes to use template functions with types it can't support. Just do this:
1 2
template<class T>
std::ostream& std::operator<<(std::ostream &out, const T &v)
And assume that it is a container that supports the begin() and end() functions - also you may need to specialize the template for maps.