msgpack / msgpack-c

MessagePack implementation for C and C++ / msgpack.org[C/C++]
Other
3.02k stars 878 forks source link

Best approach for mapping a std::map or a struct ? #690

Open SysOverdrive opened 6 years ago

SysOverdrive commented 6 years ago

I am building a bigger message with multiple records that I store internally in my project as a struct or a map ( I can use both).

I have seen the example msgpack::packer pk2(&buffer2); pk2.pack_map(2); pk2.pack(std::string("x")); pk2.pack(3); pk2.pack(std::string("y")); pk2.pack(3.4321);

what do you think is the best way to pack std::map or struct ?

redboltz commented 6 years ago

@SysOverdrive , it depends on your application. Generally speaking, if the data is variable length, use std::map is good. But key type and value type need to be fixed. struct is a part of your application data structure. msgpack-c can provide custom mapping (ARRAY or MAP) for them.

SysOverdrive commented 6 years ago

Do you mean that key value types have to be fixed on the other side of the communication ?

If I use the above implementation does msgpack get the name of the value it has to send ? For example if I send something such as :

msgpack::packer MsgMapPacker(&SecondBuf); MsgMapPacker.pack_map(4); MsgMapPacker.pack(ShouldStartStruct.hmd_video_frame_size);

Will msgpack also send the name hmd_video_frame_size along side its string value ?

redboltz commented 6 years ago

I'm not 100% sure what you are asking. But I guess that MSGPACK_DEFINE_MAP might the one you are looking for. See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_adaptor#classes

SysOverdrive commented 6 years ago

Maybe this should be another issue :

Basically I am asking for the simplest way to encode Name/Value data? Previously I was asking if packer.pack(mystruct.field) would also retain the field value, my struct beeing definde with MSGPACK_DEFINE_MAP(subject, name);.

This question has arisen because now i get this error message   "Notification mal-formatted or missing: Unpack failed: error = 0" and I presume that I might not be packing correctly both field name and value

redboltz commented 6 years ago

You can implement minimal and complete code on Wandbox like this: https://wandbox.org/permlink/Hwl8003q8XVYr6Nb

Then, click run and share, finally you will get permalink. Please post the permalink that demonstrates your issue.

SysOverdrive commented 6 years ago

So I implemented the minimal with a map of strings and it works perfect. No I am trying to send an object a little more complex : https://wandbox.org/permlink/Hwl8003q8XVYr6Nb How should I send the CalibrationShouldStartStruct data ?

SysOverdrive commented 6 years ago

https://pastebin.com/MtU5rhyi

This is the code that I mentioned

redboltz commented 6 years ago

Compile errors are detected. Your code is not complete. https://wandbox.org/permlink/2zSKeIeO7ndHqzSy

Use wandbox and at lease checking compilation, please.

SysOverdrive commented 6 years ago

As I understand I cannot use std::any (cpp17) and there is no such thing as std::object. I think It is not possible to achieve my goals with a map.

Can I somehow send the CalibrationShouldStartStruct as a whole ? Should I try and use an Object instead of a struct ?

redboltz commented 6 years ago

What does std::object mean? The root class of all classes? It doesn't exist in C++. It's not C++ style. It seems that you need to learn C++ style object handling. It's not a msgpack-c domain. More general C++ topic. I think that it is better to ask other place suck as https://stackoverflow.com/.

SysOverdrive commented 6 years ago

Well yes. But can I use another part of the msgpack api instead of map to achieve sending a data structure like CalibrationShouldStartStruct ?

Anything that uses directly struct.

redboltz commented 6 years ago

I think that std::any or std::variant is good for that. If you can not use them, you can use boost::any or boost::variant instead. msgpack-c doesn't directly support it so you need to write adaptor by yourself. See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_adaptor

redboltz commented 6 years ago

msgpack-c can pack CalibrationShouldStartStruct https://wandbox.org/permlink/96y68F2Sua0w5jeI