How to serialize a pointer of map?
rockingdice opened this issue · 2 comments
rockingdice commented
e.g.
std::map<int, int> * map_ptr{nullptr};
I know how to serialize a map or a pointer, but how to write the serialize function for the pointer of a map? Can I do it with a one-liner?
fraillt commented
That will be a bit verbose, but here's what you need to do step-by-step :)
- first make sure that
#include <map>
and#include <bitsery/ext/StdMap.h>
is included. - optionally you can create a alias for StdMap
using bitsery::ext::StdMap
. - now to the essence of the problem:
- first you need to specify is owning pointer or observer, and you should use extension method with lambda overload, because you need to specify how you'll be serializing underlying type
s.ext(map_ptr, PointerOwner{ PointerType::Nullable }, [](S& s, std::map<int, int> d) { .. }
- now, inside of lambda, you should specify how we'll be serializing map itself
- first you need to specify is owning pointer or observer, and you should use extension method with lambda overload, because you need to specify how you'll be serializing underlying type
That's it.
Here's how pointer + map serialization might look like
s.ext(map_ptr, PointerOwner{ PointerType::Nullable }, [](S& s, std::map<int, int> d) {
s.ext(d, StdMap{ 10 }, [](S& s, int& key, int& value) {
s.value4b(key);
s.value4b(value);
});
});
Hope that helps ;)
rockingdice commented
Thank you, it's working like a charm ;)