easy serialization question

Hi, I'm using the boost serialization methods, they're great. I'm using some 3rd party classes that I don't have access to, so I can't add a serialize() member function to them. It's something like: class CMyClass { int m_nWhatever; CSomeClass m_sc; // I am just using this from someone else's library.... void serialize(Archive &ar, const unsigned int version) { ar & version; ar & m_nWhatever; ar & m_sc; // fails!!! } } how can I get around this? Thanks, Mark

Mark Wyszomierski wrote:
Hi, I'm using the boost serialization methods, they're great. I'm using some 3rd party classes that I don't have access to, so I can't add a serialize() member function to them. It's something like:
class CMyClass { int m_nWhatever; CSomeClass m_sc; // I am just using this from someone else's library....
void serialize(Archive &ar, const unsigned int version) { ar & version; ar & m_nWhatever; ar & m_sc; // fails!!! } }
how can I get around this?
See http://www.boost.org/libs/serialization/doc/tutorial.html#nonintrusiveversio... http://www.boost.org/libs/serialization/doc/serialization.html#free Jeff

Mark Wyszomierski wrote:
Hi, I'm using the boost serialization methods, they're great. I'm using some 3rd party classes that I don't have access to, so I can't add a serialize() member function to them. It's something like:
class CMyClass { int m_nWhatever; CSomeClass m_sc; // I am just using this from someone else's library....
void serialize(Archive &ar, const unsigned int version) { ar & version; ar & m_nWhatever; ar & m_sc; // fails!!! } }
how can I get around this?
Thanks, Mark _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Mark, you don't necessarily need a member function. see f.ex: http://boost.org/libs/serialization/doc/tutorial.html#nonintrusiveversion -- HTH dave

Hmm ok I split up the save() and load() functions so I can just
serialize the individual members of these 3rd party classes. That
works ok. The save() method is defined like:
voidd save(Archive &ar, const unsigned int version) const
{
ar & m_3rdPartyClass.m_SomeInteger;
...
}
One of the data types though is MFC's CString - which is just like
std:string, just a wrapper around a character array. The only way to
get the contents of a CString as a char array is a function called
GetBuffer() which returns a non-const char pointer to the internal
array. My compiler does not seem to like this, as I guess the save()
function is defined as const? Is this right, and if so, is there any
way I can get around this one?
ar & m_3rdPartyClass.m_SomeCString.GetBuffer(); // returns non
const char* !!
Thanks,
Mark
On 2/9/07, David Klein
Mark Wyszomierski wrote:
Hi, I'm using the boost serialization methods, they're great. I'm using some 3rd party classes that I don't have access to, so I can't add a serialize() member function to them. It's something like:
class CMyClass { int m_nWhatever; CSomeClass m_sc; // I am just using this from someone else's library....
void serialize(Archive &ar, const unsigned int version) { ar & version; ar & m_nWhatever; ar & m_sc; // fails!!! } }
how can I get around this?
Thanks, Mark _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Mark,
you don't necessarily need a member function. see f.ex:
http://boost.org/libs/serialization/doc/tutorial.html#nonintrusiveversion
-- HTH dave
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Mark Wyszomierski wrote:
Hmm ok I split up the save() and load() functions so I can just serialize the individual members of these 3rd party classes. That works ok. The save() method is defined like:
voidd save(Archive &ar, const unsigned int version) const { ar & m_3rdPartyClass.m_SomeInteger; ... }
One of the data types though is MFC's CString - which is just like std:string, just a wrapper around a character array. The only way to get the contents of a CString as a char array is a function called GetBuffer() which returns a non-const char pointer to the internal array. My compiler does not seem to like this, as I guess the save() function is defined as const? Is this right, and if so, is there any way I can get around this one?
ar & m_3rdPartyClass.m_SomeCString.GetBuffer(); // returns non const char* !!
I don't know how serialization for std::string is done, but it's supported by the library. Maybe you could create a temporary std::string from your MFC string and serialize that one instead? Though there may be a decrease in performance if you have a lot of strings to deal with. -- Regards, dave

David Klein wrote:
One of the data types though is MFC's CString - which is just like std:string, just a wrapper around a character array. The only way to get the contents of a CString as a char array is a function called GetBuffer() which returns a non-const char pointer to the internal array.
returns a non-const char pointer? so the mfc CString gives you a pointer to a buffer you might want to modify from outside the class? Looks like a bad idea to me. In any case I would use const_cast to cast the result to a "char const * const" to cast to a constant pointer to a const array of chars. This should do the trick. If you want to avoid sprinkling this syntactic vinegar through your code you mith make an non-intrusive serialization of mfc CString so you can just use: #include "...mfc/cstring.hpp" CString x; ... ar << x; ... ar >> x; Robert Ramey
My compiler does not seem to like this, as I guess the save() function is defined as const? Is this right, and if so, is there any way I can get around this one?
ar & m_3rdPartyClass.m_SomeCString.GetBuffer(); // returns non const char* !!
I don't know how serialization for std::string is done, but it's supported by the library. Maybe you could create a temporary std::string from your MFC string and serialize that one instead? Though there may be a decrease in performance if you have a lot of strings to deal with.

Quoting David Klein
Mark Wyszomierski wrote:
One of the data types though is MFC's CString - which is just like std:string, just a wrapper around a character array. The only way to get the contents of a CString as a char array is a function called GetBuffer() which returns a non-const char pointer to the internal array. My compiler does not seem to like this, as I guess the save() function is defined as const? Is this right, and if so, is there any way I can get around this one?
ar & m_3rdPartyClass.m_SomeCString.GetBuffer(); // returns non const char* !!
I don't know how serialization for std::string is done, but it's supported by the library. Maybe you could create a temporary std::string from your MFC string and serialize that one instead? Though there may be a decrease in performance if you have a lot of strings to deal with.
Try casting it to an LPCTSTR. That should actually be far enough for the Serializer to work, as that is const pointer to a TChar array, and TChar is simply a wchar_t or char depending on if you have Unicode turned on. I wouldn't put any bets on the serialized data loading into another app that was compiled with the opposite Unicode setting. ar & (LPCTSTR)(m_3rdPartyClass.m_SomeCString);

Thanks for all your replies. This seems to work:
ar & (CString::PCXSTR)m_strMyCString;
that PCXSTR makes it const, I suppose.
Is it possible though to serialize a char * pointer? Because I'm
stuffing this into an std::string before serialzing, in reality, like:
ar & std::string( (CString::PCXSTR)m_strMyCString);
not so bad writing it, but reading it back in requires 3 lines:
std::string strTemp;
ar & strTemp;
m_strMyCString = strTemp.c_str();
not sure if it's possible to just do it from a const char * pointer.
Thanks,
Mark
On 2/9/07, Jeffrey McBeth
Quoting David Klein
: Mark Wyszomierski wrote:
One of the data types though is MFC's CString - which is just like std:string, just a wrapper around a character array. The only way to get the contents of a CString as a char array is a function called GetBuffer() which returns a non-const char pointer to the internal array. My compiler does not seem to like this, as I guess the save() function is defined as const? Is this right, and if so, is there any way I can get around this one?
ar & m_3rdPartyClass.m_SomeCString.GetBuffer(); // returns non const char* !!
I don't know how serialization for std::string is done, but it's supported by the library. Maybe you could create a temporary std::string from your MFC string and serialize that one instead? Though there may be a decrease in performance if you have a lot of strings to deal with.
Try casting it to an LPCTSTR. That should actually be far enough for the Serializer to work, as that is const pointer to a TChar array, and TChar is simply a wchar_t or char depending on if you have Unicode turned on. I wouldn't put any bets on the serialized data loading into another app that was compiled with the opposite Unicode setting.
ar & (LPCTSTR)(m_3rdPartyClass.m_SomeCString);
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Use free serialization functions. This is referred to in the documentation as "non-intrusive". Look through the documentation and examples. Robert Ramey Mark Wyszomierski wrote:
Hi, I'm using the boost serialization methods, they're great. I'm using some 3rd party classes that I don't have access to, so I can't add a serialize() member function to them. It's something like:
class CMyClass { int m_nWhatever; CSomeClass m_sc; // I am just using this from someone else's library....
void serialize(Archive &ar, const unsigned int version) { ar & version; ar & m_nWhatever; ar & m_sc; // fails!!! } }
how can I get around this?
Thanks, Mark
participants (5)
-
David Klein
-
Jeff F
-
Jeffrey McBeth
-
Mark Wyszomierski
-
Robert Ramey