MessagePack, c++: How to use MSGPACK_DEFINE with c++11 enum classes
Asked Answered
A

3

5

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++.

Aroma answered 22/11, 2013 at 21:30 Comment(0)
A
0

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.

Aroma answered 23/11, 2013 at 1:33 Comment(1)
This is the only way to use enum class with MessagePack? Not so elegant!Ranunculus
A
7

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());
}
Ataraxia answered 14/4, 2015 at 2:49 Comment(0)
A
0

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.

Aroma answered 23/11, 2013 at 1:33 Comment(1)
This is the only way to use enum class with MessagePack? Not so elegant!Ranunculus
R
0

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.

Ranunculus answered 3/4, 2015 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.