enum support had been incomplete.
This fix made enum support complete.

Replaced int with auto on c++11 scoped enum.
Replaced template specializations with function overloads on operator<< and operator>> for enum.

enum can convert to object with and without zone.

When you want to adapt enum, you need to write as follows:

// NOT msgpack.hpp

enum yourenum {
    elem
};

MSGPACK_ADD_ENUM(yourenum);

// msgpack.hpp should be included after MSGPACK_ADD_ENUM(...)

int main() {
    msgpack::object obj(yourenum::elem);
}
This commit is contained in:
Takatoshi Kondo
2015-01-31 21:59:21 +09:00
parent 978e6b9057
commit c4fb47c00d
8 changed files with 269 additions and 76 deletions

View File

@@ -20,6 +20,7 @@
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/msgpack_tuple_fwd.hpp"
#include "msgpack/adaptor/int_fwd.hpp"
#include "msgpack/object_fwd.hpp"
#define MSGPACK_DEFINE(...) \
@@ -42,7 +43,6 @@
#define MSGPACK_ADD_ENUM(enum) \
namespace msgpack { \
MSGPACK_API_VERSION_NAMESPACE(v1) { \
template <> \
inline object const& operator>> (object const& o, enum& v) \
{ \
int tmp; \
@@ -50,7 +50,10 @@
v = static_cast<enum>(tmp); \
return o; \
} \
template <> \
inline void operator<< (object& o, const enum& v) \
{ \
o << static_cast<int>(v); \
} \
inline void operator<< (object::with_zone& o, const enum& v) \
{ \
o << static_cast<int>(v); \

View File

@@ -47,18 +47,21 @@
#define MSGPACK_ADD_ENUM(enum) \
namespace msgpack { \
MSGPACK_API_VERSION_NAMESPACE(v1) { \
template <> \
inline object const& operator>> (object const& o, enum& v) \
{ \
int tmp; \
std::underlying_type<enum>::type tmp; \
o >> tmp; \
v = static_cast<enum>(tmp); \
return o; \
} \
template <> \
inline void operator<< (object& o, const enum& v) \
{ \
auto tmp = static_cast<std::underlying_type<enum>::type>(v); \
o << tmp; \
} \
inline void operator<< (object::with_zone& o, const enum& v) \
{ \
int tmp = static_cast<std::underlying_type<enum>::type>(v); \
auto tmp = static_cast<std::underlying_type<enum>::type>(v); \
o << tmp; \
} \
namespace detail { \