2

When I parse from the buffer, I have to know,what the exact class type(myclass) of the buffer is. So I cant handle all the buffer parse in an uniform way. I have to distinguish every class type(can I?).

My questions is "how can i handle all the buffer parse in an uniform way,and dont need to care what the exact class type is." Is there any solution? Thanks for your help!

   class myclass {
private:
    std::string m_str;
    std::vector<int> m_vec;
public:
    MSGPACK_DEFINE(m_str, m_vec);
};

int main(void) {
        std::vector<myclass> vec;
        // add some elements into vec...

        // you can serialize myclass directly
        msgpack::sbuffer sbuf;
        msgpack::pack(sbuf, vec);

        msgpack::unpacked msg;
        msgpack::unpack(&msg, sbuf.data(), sbuf.size());

        msgpack::object obj = msg.get();

        // you can convert object to myclass directly
        std::vector<myclass> rvec;
        obj.convert(&rvec);
}

I expecte it works like this

class base 
{
.....
};

class A:public base
{
....
};

class B:public base
{
....
};


base* parse(msgpack::sbuffer sbuf)
{
...
}


int main(void)
 {
    A a;
        msgpack::sbuffer sbuf;
        msgpack::pack(sbuf, a);
    base* a1 = parse(sbuf);
    ....    

    B b;
        msgpack::sbuffer sbuf;
        msgpack::pack(sbuf, b);
    base* b1 = parse(sbuf);
    ....        
}
sinopec
  • 851
  • 1
  • 9
  • 16
  • If you had something like that, how do you imagine using it? – Vaughn Cato Dec 21 '12 at 02:41
  • I suppose to have a base class, And the base class can tell us how to handle different situation through virtual functions... – sinopec Dec 21 '12 at 02:58
  • Could you give some code showing how you might expect it to work? – Vaughn Cato Dec 21 '12 at 03:04
  • class base { ..... }; class A:public base { .... }; class B:public base { .... }; base* parse(msgpack::sbuffer sbuf) { ... } int main(void) { A a; msgpack::sbuffer sbuf; msgpack::pack(sbuf, a); base* a1 = parse(sbuf); .... B b; msgpack::sbuffer sbuf; msgpack::pack(sbuf, b); base* b1 = parse(sbuf); .... } – sinopec Dec 21 '12 at 03:11
  • hello did you find any solution for this ? I have similar case. – Cozdemir Jan 16 '21 at 09:48

0 Answers0