3

I'm working on a body of code which deals with a custom String implementation rather than the std::string (long story but for various reasons this has to be used) which I will refer to as "String" from here on.

I was able to easily pack the String without issue using the "raw" type to pack the raw char bytes and the size but I'm having problems unpacking it.

I was able to manually unpack it as shown below.

// before this I've unpacked to the point where the following object has the string
msgpack::object_kv& kv = obj.via.map.ptr[0];
// kv.key == the String I want
String key = String(key.via.raw.ptr, key.via.raw.size); // this works

But I want to use the built in >> operator or the .as template function and haven't been able to manage it. I don't have access to modify the String class to add the msgpack_unpack function nor to add MSGPACK_DEFINE

I tried creating a struct and giving it a msgpack_unpack function, but apparently it calls it with msgpack::object::implicit_type which my compiler replies with

error: 'struct msgpack::object::implicit_type' is private

And then I can't figure out any way of getting the msgpack::object out of the "implicit_type" object.

Any ideas?

Zeroshade
  • 463
  • 2
  • 8
  • 1
    Maybe this can do the job `String& operator>>( String, yourtype)` implemented as a function instead of member of the class – hetepeperfan Jul 31 '13 at 14:40
  • After taking another look I finally got why my attempts at operator>> before weren't working, I'll answer the question with the full result. – Zeroshade Jul 31 '13 at 17:47

1 Answers1

2

So I figured it out! Thanks to hetepeperfan for the idea

After taking another crack at using operator>> overloading, the problem that prevented it from working before the strange way the operator>> being overloaded is called from the msgpack code.

namespace msgpack {
    String& operator>>(msgpack::object o, String& v) {
        v = String(o.via.raw.ptr, o.via.raw.size);
        return v;
    }
}

I needed to both use the msgpack namespace, and match the signature by returning the String by reference and take the object in NOT by reference. This seems a little ridiculous but it works and I can now use the built in "as<>" and convert functionality. awesome!

Zeroshade
  • 463
  • 2
  • 8