Hi,
a collegue of mine is trying to write a serialisable container (reads at
construction, writes at destruction).
The writing part is pretty easy: simply iterate through the container and
write into the file.
Reading from the file is (should) be easy too:
while you're getting data from the file (stream) insert it into your
container.
Unfortunatly this fails with the container map, because the
map::value_type's key is const :-/
This is the stripped code:
#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>
template< class containerT >
class MyContainer : public containerT
{
public:
void read()
{
typename containerT::value_type val;
/* normally:
read from file and make the assigment,
this is simply for demonstration
*/
val = val;
/* insert into container */
insert(end(), val);
}
};
template< class containerT >
void foo()
{
MyContainer< containerT > cont;
cont.read();
}
int main(int, char**)
{
/* compiles */
foo< std::vector< int > >();
foo< std::list< char > >();
foo< std::set< char > >();
foo< std::deque< long > >();
/* fails */
foo< std::map< char, int > >();
return 0;
}
Any ideas, how to modify the code for being able to assign to the value and
add to the container??
The read function would look something like this:
void read()
{
std::ifstream stream(m_path.c_str());
while (!stream.eof())
{
typename containerT::value_type val;
stream >> val;
insert(end(), val);
}
}
Thanks in advance :)
Jorge