Serializing the unserializables...
- Dettagli
- Categoria: TSL
- Pubblicato Lunedì, 01 Ottobre 2012 01:41
- Scritto da Giampa
- Visite: 2451
In case it is not possible to modify the source code it is nevertheless possible to serialize it through a specialization of the bitwise operator & for the [i,o]archive classes.
Assuming as example the need of serializing the boost::regex instances we need to specialize the tsl::operator & in the following way:
namespace tsl {
namespace valid {
// the following line will make boost:regex as a valid basic type for tsl during static asserts
template <> struct Basic< boost::regex> : public std::true_type {};
}
// this is the operator specialization in case of named serialization (i.e. with a name provided as label)
template <>
inline OArchive & operator & (OArchive &out,const Named<const boost::regex&> &t) {
return out.save(std::get<1>(t).str(),std::get<0>(t));
}
// this is the operator specialization for simple seriaization (i.e. with default name / label)
template <>
inline OArchive & operator & (OArchive &out,const boost::regex &value) {
return out.save(value.str());
}
// operator specilization for deserialization
template <>
inline IArchive & operator & (IArchive &in,boost::regex &value) {
std::string s;
in.restore(s);
value=s;
return in;
}
}
It is now possible to invoke the (de)serializtion operator & as any other serializable class:
// serialization
const boost::regex original(".*[a-z]+");
tsl::OJsonArchive oa(stream);
oa & original;
// deserialization
boost::regex restored;
tsl::IJsonArchive ia(stream);
ia & restored;
The resulting json archive will look like:
{
"TSL_String": ".*[a-z]+"
}
The full example source is below:
#include <boost/regex.hpp>
#include <sstream>
#include <tsl/json_archive.h>
namespace tsl {
namespace valid {
// the following line will make boost:regex as a valid type for tsl during static asserts
template <> struct Basic< boost::regex> : public std::true_type {};
}
// this is the operator specialization in case of named serialization (i.e. with a name provided as label)
template <>
inline OArchive & operator & (OArchive &out,const Named<const boost::regex&> &t) {
return out.save(std::get<1>(t).str(),std::get<0>(t));
}
// this is the operator specialization for simple seriaization (i.e. with default name / label)
template <>
inline OArchive & operator & (OArchive &out,const boost::regex &value) {
return out.save(value.str());
}
// operator specilization for deserialization
template <>
inline IArchive & operator & (IArchive &in,boost::regex &value) {
std::string s;
in.restore(s);
value=s;
return in;
}
}
using namespace std;
int main(int argc,const char *argv[]) {
try {
stringstream stream;
stream.unsetf(ios::skipws);
const boost::regex original(".*[a-z]+");
{ // serialization
tsl::OJsonArchive oa(stream);
oa & original;
}
cout << stream.str() << endl;
boost::regex restored;
{ // deserialization
tsl::IJsonArchive ia(stream);
ia & restored;
}
assert(original==restored);
} catch (const runtime_error &error) {
cerr << "Exception: " << error.what() << endl;
}
}



