
On 10/28/2008 10:13 PM, Robert Ramey wrote:
XML archives check that the reading tag matches the writing tag.
This is convenient and helpful for checking your code even if you don't plan to use xml archives on your deliverable.
Thanks for the tip. But the deliverable has to use text archives and still has to provide some sort of type checking during deserialziation. It could be as simple as saving the class name to the archive and checking on deserialzation whether the class name in the archive and the target class matches. I presume I could implement my own archive but I was hoping this functionality already exists. -Frank
Robert Ramey
Frank Winklmeier wrote:
Hi,
is there any way to check whether the class type used in deserialization is identical to the class type in the serialization? Below is a simple example, that serializes class "Data" into a text archive and deserializes this archive into class "StringData". If that happens I would like an exception to be thrown. Is this possible?
Thanks for any help, Frank
#include
#include #include <iostream> #include <sstream>
using namespace std;
class Data { public: Data() { for (int i=0; i<100; i++) a[i]=i; }; int a[100];
template<class Archive> void serialize(Archive & ar, const unsigned int) { ar & a; } };
class StringData { public: StringData() {}; string s;
template<class Archive> void serialize(Archive & ar, const unsigned int) { ar & s; } };
int main() {
ostringstream oss;
// Serialize the class const Data d1; { boost::archive::text_oarchive oa(oss); oa << d1; }
// Deserialize it using the wrong type StringData d2; { istringstream iss(oss.str()); boost::archive::text_iarchive ia(iss); ia >> d2; }
cout << oss.str() << endl; cout << d2.s << endl; }
Output: ======= 22 serialization::archive 4 0 0 100 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users