|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2007-03-09 14:47:21
Here is your proposal
> static map<string, bool> string_map;
>
> template <class Archive>
> void read_string(Archive ar, string& a_string) {
> string s;
> ar >> s; // read from disk
> map<string, bool>::iterator i = string_map.find(s);
>
> if (i == string_map.end()) {
> i = string_map.insert(make_pair(s, true));
> }
>
> a_string = i->first;
> }
>
> void destroy_map() { string_map.clear(); }
First I would use a set rather than map as I think it
would be sufficient.
Second I would make this a "helper class" . Your
special "Lear Archive" would be derived from
an existing archive and the helper class using
multiple inheritance. Look at the implemenation
of shared_ptr checked into the HEADof CVS
to see how to do this.
class LearHelper {
std::set<std::string> m_strings;
};
class LearArchive : binary_oarchive_impl, LearHelper {
};
class lear_string : std::string {
typedef mpl:_true? boost::serialization:is_wrapper;
template<class Archive>
void save(Archive &ar, const unsigned int version) const {
std::string::iterator it = m_strings.find(*this);
// if the string isn't in the set
if(it != std::string::end())
m_strings.insert(*this);
// serialize the one in the set
ar << *it;
}
void load(Archive &ar, const unsigned int version) {
// load the string to a temporary location
std::string tstr;
ar >> tsr;
// look it up in the set;
std::string::iterator it = m_strings.find(*this);
// if the string isn't in the set
if(it != std::string::end())
it = m_strings.insert(tsr);
// and copy from the one in the set
*this = tsr;
reset_object_address(... // in case someone loads a pointer to a
string
}
}
finally, whenever you serialize a string, you use the lear_string wrapper
some class ...
std::sting my_string;
....
save(...
ar << lear_string(my_string);
...
load(...
ar >> lear_string(my_string);
};
Food for thought. This should keep you busy for a while. Good Luck.
Robert Ramey
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net