0

I would like to send the following struct over msgpack.

struct MyStruct {
    std::string     name{""};
    int*            val{nullptr};

    MSGPACK_DEFINE( name, val );
};

Thus far in all of my projects, the only way I've streamed with msgpack is using MSGPACK_DEFINE, then writing the struct to msgpack::sbuffer (and sending it). the MSGPACK_DEFINE macro complains that that perhaps I missed the "->" so I'm guessing it doesn't detect that it's a pointer.

Smart pointers seem to work though:

struct MyStruct {
    std::string          name{""};
    std::shared_ptr<int> val{nullptr};

    MSGPACK_DEFINE( name, val );
};

The caveat is that the receiver on the other end needs val to be a raw pointer. I would like to do this without converting on the receiving side. Any ideas?

kiss-o-matic
  • 1,111
  • 16
  • 32

1 Answers1

0

You failed to explain why you wish to do this. Pointers are never meaningful when serialized (otherwise it is in-process data and there is no need to serialize).

Just pass the value that the pointer points to. If you need to represent "a number or NULL", then pass a struct containing an integer and boolean.

struct NullableInt {
    int value{0};
    bool null{true};
};
Colorado.Rob
  • 195
  • 8
  • That was just a simple example. In practice it's actually a pointer to other structs. I'm sending a tree structure. I'm fine with serializing it (somehow) but unaware of any msgpack macro that might work, or a way around it. I think the answer is a simpler structure on the back end, and then do all the magic w/ the raw pointers on the receiving end. – kiss-o-matic Jul 14 '16 at 18:35
  • Let me reiterate: pointers are never meaningful when serialized. – Colorado.Rob Jul 14 '16 at 18:44
  • Let me clear this up: I'm wondering if the macro can dereference the pointer and serialize it for me in some meaningful way. If it's wrapped in a shared_ptr, it works fine. – kiss-o-matic Jul 14 '16 at 21:43
  • Ah. Now I understand what you are after. No, there is no support for raw pointers except in the case of char*. You would need to extend MSGPACK with something like what is in adapter/char_ptr.hpp or adapter/cpp11/shared_ptr.hpp. – Colorado.Rob Jul 15 '16 at 14:41