alibaba / yalantinglibs

A collection of modern C++ libraries, include coro_rpc, struct_pack, struct_json, struct_xml, struct_pb, easylog, async_simple
https://alibaba.github.io/yalantinglibs/
Apache License 2.0
1.51k stars 238 forks source link

[feat][struct_pack] not support shared_ptr #437

Open manuel76413 opened 1 year ago

manuel76413 commented 1 year ago

I saw the struct_pack library supports many STL containers but after testing, I regretted that this library does not support the smart point of C++ 11. like shared_ptr etc.

for example, I have a tree node struct like:

enum class type_e { root = 0, users = 1, projs = 2, };

struct Node { type_e type; std::string name; std::string value; std::list<std::shared_ptr<Node>> children; };

for now, this library cannot serialize/deserialize the above Node structure. you can reference another nonintrusive library named alpaca, which supports smart point.

poor-circle commented 1 year ago

Thank you. It's in my todolist.

poor-circle commented 1 year ago

But, in this case, the shared_ptr is unnecessary.

poor-circle commented 1 year ago

If you don't want to shared the nodes, just remove the shared_ptr.

enum class type_e {
root = 0,
users = 1,
projs = 2,
};

struct Node {
type_e type;
std::string name;
std::string value;
std::list<Node> children;
};

Serialize/deserilaize shared_ptr will cost additional price. First we need check if the address is same as previous one to shared the object. Second deserialize shared_ptr may cause memory leak and we need to check it. Anyway I will try to support it later.

manuel76413 commented 1 year ago

thanks for your reply, This library is very cute

982945902 commented 6 months ago

shared_ptr可能会导致循环引用问题,这个不是很好解决吧

poor-circle commented 6 months ago

shared_ptr可能会导致循环引用问题,这个不是很好解决吧

  1. 首先在编译期检查类型是否成环。只有类型系统成环才可能出现循环引用。
  2. 类型成环的情况下,要么禁止序列化这种类型,要么在反序列化时跑下dfs检测是否循环引用。
982945902 commented 6 months ago

shared_ptr可能会导致循环引用问题,这个不是很好解决吧

  1. 首先在编译期检查类型是否成环。只有类型系统成环才可能出现循环引用。
  2. 类型成环的情况下,要么禁止序列化这种类型,要么在反序列化时跑下dfs检测是否循环引用。 soga