Question

The following sample does not compile, complaining that

In file included from /usr/include/msgpack.hpp:18:
/usr/include/msgpack/object.hpp:211:3: error: member reference base type 'logd::log_level' is not a structure or union

and a corresponding error for the other enum class. My question is how, using msgpack's c++ api, does one serialize a class with members that are of c++11 enum class type?

#ifndef LOG_MSG_HPP_
#define LOG_MSG_HPP_

#include <stdlib.h>
#include <msgpack.hpp>

/** @namespace logd */
namespace logd {

enum class log_level { SILENT,... DEBUG };

enum class log_domain { AI, ...  MISC };

class log_msg {
    public:
        log_msg(log_level lev, log_domain dom, std::string msg);
        log_level level();
        log_domain domain();
        std::string message();
        ~log_msg();
        MSGPACK_DEFINE(lev_, dom_, msg_);

    private:
        log_msg();
        log_level   lev_ {log_level::DEBUG};
        log_domain  dom_ {log_domain::MISC};
        std::string msg_ {"No message given."};
};
} /* namespace logd */
#endif /* LOG_MSG_HPP_ */

NOTE: Since the enums are mine, I can happily modify them to make msgpack happy. Unfortunately, I can find no references on the subject in their docs or the first couple pages of Google. I am also not able to determine what to do from reading their headers/source since I'm rather new to c++.

Was it helpful?

Solution 2

One way that seems to work is to wrap the enum in a union...

union log_level_t {
    log_level lev;
    int       levint;
}

...

    log_level_t lev_;

...

MSGPACK_DEFINE(lev_.levint, dom_.domint, msg);

This seems like a crappy approach, but it works.

OTHER TIPS

You can use MSGPACK_ADD_ENUM() macro. It has been supported since version 1.0.0. I recommend version 1.1.0 or later.

See: https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor#enums

I applied MSGPACK_ADD_ENUM() to your code:

#ifndef LOG_MSG_HPP_
#define LOG_MSG_HPP_

#include <stdlib.h>
#include <msgpack.hpp>

/** @namespace logd */
namespace logd {

enum class log_level { SILENT,DEBUG };

enum class log_domain { AI, MISC };

class log_msg {
    public:
        log_msg(log_level lev, log_domain dom, std::string msg):lev_(lev), dom_(dom), msg_(msg) {}
        log_level level() { return lev_;}
        log_domain domain() { return dom_;}
        std::string message() { return msg_; }
        ~log_msg() {}
        MSGPACK_DEFINE(lev_, dom_, msg_);
        log_msg() = default;

    private:
        log_level   lev_ {log_level::DEBUG};
        log_domain  dom_ {log_domain::MISC};
        std::string msg_ {"No message given."};
};
} /* namespace logd */

MSGPACK_ADD_ENUM(logd::log_level);
MSGPACK_ADD_ENUM(logd::log_domain);


#endif /* LOG_MSG_HPP_ */

#include <sstream>
#include <cassert>

int main() {
    logd::log_msg lm { logd::log_level::SILENT, logd::log_domain::AI, "hello" };
    std::stringstream ss;
    msgpack::pack(ss, lm);

    msgpack::object obj = msgpack::unpack(ss.str().data(), ss.str().size()).get();
    logd::log_msg lm2 = obj.as<logd::log_msg>();
    assert(lm.level() == lm2.level());
    assert(lm.domain() == lm2.domain());
    assert(lm.message() == lm2.message());
}

I found another solution, maybe a little more elegant (ok, it's not so perfect):

MSGPACK_DEFINE((int&)lev_, (int&)dom_, msg)

So you can keep your original enum without creating a new union.

The deserialisation method should work without any problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top