11

Hi has anybody already managed to serialize a C++11 std::shared_ptr with boost::serialization. There are lots of obsolete posts out there but none with an acceptable solution. I am not going into discussion why I want to use the std::shared_ptr just accept it!

I found this other post: boost serialize and std::shared_ptr but it does not answer my problem how to serialize the std::shared_ptr it only suggests to uses the boost::shared_ptr which is not possible in my case.

And this post How can boost::serialization be used with std::shared_ptr from C++11? does also only recommend to use boost::shared_ptr.

I would be interested if anybody has already found a work around that manages to serialize the std::shared_ptr.

I finally found a solution to my problem see https://stackoverflow.com/a/14999396/2039157 for my answer.

peterh
  • 11,875
  • 18
  • 85
  • 108
denim
  • 1,329
  • 1
  • 15
  • 24
  • Convert std::shared_ptr to boost::shared_ptr for serialize. – ForEveR Feb 20 '13 at 07:59
  • Yes, I was going to say, if using `boost::shared_ptr` is an option, this is the answer: http://stackoverflow.com/questions/9944274/boost-serialize-and-stdshared-ptr – jogojapan Feb 20 '13 at 08:00
  • Hm, what exactly do you mean by convert? I hope you don't mean to replace the std::shared_ptr with boost::shared_ptr ;) Please give an example on how the conversion should work. – denim Feb 20 '13 at 08:03
  • @denim What I mean was replacement, yes. Anyway, what about the duplicate question? – jogojapan Feb 20 '13 at 08:08
  • @jogojapan Hm,of course my question is a duplicate. But the question is a year old and already closed. And the answer is not acceptable. The std::shared_ptr is already in the C++11 standard. – denim Feb 20 '13 at 08:16
  • 1
    @denim `std::shared_ptr` was in the Standard when that question was posted. And the answers and comments may actually say all there is to say: The stated goal of the boost serialization library is to provide serialization for all STL types, so `std::shared_ptr` is going to be supported. If it isn't supported yet, it's a matter of time until it is. I don't see how the truth of this has fundamentally changed since then. Moreover, you could have updated that question, or, if really necessary, filed a new question *with a link* to the old one, and an explanation. – jogojapan Feb 20 '13 at 08:34
  • @jogojapan Sorry, I agree. I didn't know that it is ok to update an "old" question. And sorry that I didn't post the link. I didn't post it because there were many more posts regarding this topic which would also have been worth posting them. Again you are right it is maybe only a matter of time. I just wanted to know if anyone has already found a work around. What do you suggest to solve this problem now? – denim Feb 20 '13 at 08:43
  • @denim I'd suggest editing this question to add a reference to the other one and explaining you are interested in updates. The question may still get closed as a duplicate, but by then somebody might have given an answer that helps you. If not, try updating the old question. Each time you update this or the old question it'll jump to the top of the "active questions", so a lot of people will look at it. If there is still no answer then, it may mean there is no good answer. (After all, boost still seems not to implement this as far as I understand.) – jogojapan Feb 20 '13 at 08:51
  • @jogojapan Unfortunately I also saw it somewhere in the boost mailing list that the std::shared_ptr wont be supported that soon. Anyway thanks for your help. – denim Feb 20 '13 at 09:06
  • 1
    Um.. I'm a little out of the C++ currently, but having the actual serializer/deserializer in the boost which is open, and taking into consideration that the shared pointers - even if different - they probably have quite similar structure, and taking into consideration that we don't want to serialize the smart pointer, but its contents - is it really so hard to copy the boost's header/code, trim it down to only the code that (de)serializes the smart pointers, and adapt it to use shptr instead of boost::shptr? Assuming that ctors/dtors/getter are similar, that should be almost find&replace thing – quetzalcoatl Feb 20 '13 at 09:09
  • @AlexChamberlain Sorry, for the harsh words, but many other posts about this problem just deal with using the boost::shared_ptr instead of the std::shared_ptr. If this would be an option for me then I would not have posted this question. What do you think? – denim Feb 20 '13 at 09:10
  • Please don't take this suggestion as aggression or whetever. I just sense that this should not be as hard as it may seem, especially with the fact that someone in boost already did it for another kind of similar shptr. – quetzalcoatl Feb 20 '13 at 09:12
  • @quetzalcoatl I have already thought about copying and adjusting the relevant header (boost/serialization/shared_ptr.hpp). But the header also uses the Archive.reset() function which also expects the boost::shared_ptr as a parameter and that was the point when i gave up because it would go to deep into the boost library. – denim Feb 20 '13 at 09:52
  • 1
    Thanks for looking deeper into it! I've just skimmed through the (old) 1.46 version of boost/serialize/shptr and I believe that the two 'ar.reset' seen in `load` funtions are in fact nothing more than delegating the job of "fetch read object from archieve, then smartpointer.reset(thatobject)". I can be wrong of course, I did not check the Archive's code:), but that's quite logical/typical responsibility separation. In 1.46 version they are called just after `ar>>r` where R is the raw pointer. I think for your own usage, you could try to inlinining the `t.reset(r)` instead of `ar.reset(t,r)` – quetzalcoatl Feb 20 '13 at 10:08
  • @quetzalcoatl Thank you too. You can find the reset() function in boost/archive/shared_ptr_helper.hpp and it is doing a lot more. Each archive implementation (e.g. xml_iarchive in xml_iarchive.hpp) derives from shared_ptr_helper, so I guess it is really deep anchored in boost/archive. The only thing I can think of is to make an extended xml_iarchive_ext class that derives from xml_iarchive and additionally derives from an adjusted shared_ptr_helper class but I am not sure if I will accomplish this. – denim Feb 20 '13 at 10:20
  • aaahhh.. ok I got the problem now.. I've got rusty on C++, eh. I totally forgot that here the smartpointers **work alone**, there is no common hidden static manager that would internally synchronize the magic inside, instead as the smartpointer objects are copied/initialized form each other, they build up proper structures for themselves.. If we'd do `ptr.reset(r)` that would create brand new pointer that would be detached from all other smartpointers.. This obviously means that smart pointers have to be tracked in almost the same way as normal pointers.. – quetzalcoatl Feb 20 '13 at 10:39
  • Still, the existing code should be readily adaptable, but surely, jsut as you say, more work needs to be done. If you say that there's some "smartpointerhelper" mixing class that the archive derives from, then it should be completely possible to exploit the N*inherit available in C++ to create another mixin that would provide overloads of "archieve.reset" for sharedptrs from std, not boost - and simply add that mixin to original boost's Archieve. Or as you say, add new derived archive type. Now I've got really curious about what's down there inside. But I'm a bit low on time before weekend:/ – quetzalcoatl Feb 20 '13 at 10:43

1 Answers1

0

Isn't it possible to "just" create a serializer<< and deserializer>> that will "just" turn the shptr-serialization into object-pointer serialization? If not done yet, I'm almost sure that this is possible in < 100 lines of code.

Also, I know that you have specified not to ask, but still I will: When you try to serialize "X" variable that is shptr, why can't you just serialize dereferenced *shptr ? That doesn't seem enough difference for me to bother with adding serialization for smartptrs.. Hm.. ok. esthetics. But still I think it is doable.

Anyways, however you can hit another wall: if you use class inheritance and if you carry along the objects within shptr via pointer to the base class, you will have the obvious problem that the serializer will not know what actually it is ordered to serialize. But that's a classic problem for serialization without RTTI, that, well, you hve to somehow solve, but that's not the problem of shptrs.

disclaimer: the last time I used boost::serialization was few years ago. I did not actually try to serialize shptr, but from what I remember, that was quite an open framework, relatively easy to extend for any custom type, so I assume shptrs are no better than my weird-classes-that-I-managed-to-serialize :)

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • 2
    The problem is that the boost::serialization framework provides a header file "boost/serialization/shared_ptr.hpp" that allows to serialize a boost::shared_ptr and this works quite will (I tried it). The problem is that I have std::shared_ptr which I have to serialize and there is no special header in the boost::serialization lib that handles the std::shared_ptr. So I wanted to know if anybody else managed to serialize a std::shared_ptr. – denim Feb 20 '13 at 08:12