Re: Partial serialization of a program

kila suelika via Boost-users <[email protected]>
Newsgroups gmane.comp.lib.boost.user
Message-ID <CAH8XdD3eeweR8X3SUhyMg=XPGg_OevVDZvrKeBKNp-9tYa_7kg@mail.gmail.com>
I write an example where one can manually set pointer in load:

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <sstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <map>
#include <string>

#include <boost/serialization/split_member.hpp>

std::map<std::string, int> m{ {"x",5} };
struct A
{
friend class boost::serialization::access;
int* x;

template <class Archive>
void save(Archive& ar, const unsigned int version) const {

}
template <class Archive>
void load(Archive& ar, const unsigned int version) {
x = &(::m["x"]);//Access outside data.
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};

int main()
{
A a;

std::stringstream s;
boost::archive::binary_oarchive oa(s);
oa << a;

boost::archive::binary_iarchive ia(s);
A a0;
ia >> a0;

std::cout << *(a0.x) << std::endl;
}

On Fri, Nov 4, 2022 at 7:20 AM Gavin Lambert via Boost-users <
[email protected]> wrote:

> On 3/11/2022 20:40, Markus Lavin wrote:
> > If a serialized object in the dynamic part contains a pointer to a
> > non-seralized object in the static part then how do I deal with that.
> > Clearly the address cannot be serialized since it may change between
> > builds and even between runs (ASLR). The program does contain a
> > 'registry' though where all relevant static objects are registered with
> > a unique identifier.
> >
> > So question is would it somehow be possible to have Boost serialization
> > use this registry to serialize the unique identifier instead of trying
> > to serialize the entire object when one of those pointers are serialized?
>
> Each class can choose exactly what data it serializes and deserializes,
> and what it does with it.
>
> For this case it sounds like you probably want
>
> https://www.boost.org/doc/libs/1_80_0/libs/serialization/doc/tutorial.html#splitting
> such that you can save an internal id and then reload that id and look
> up the corresponding pointer, rather than serializing the other object
> directly.  You can still cascade into the other object as a reference to
> save/load its dynamic state after that.
>
> You will likely also want to read about
>
> https://www.boost.org/doc/libs/1_80_0/libs/serialization/doc/special.html#objecttracking
>
> _______________________________________________
> Boost-users mailing list
> [email protected]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
>

_______________________________________________
Boost-users mailing list
[email protected]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.