mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-18 03:29:49 +02:00
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:
@@ -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); \
|
||||
|
@@ -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 { \
|
||||
|
Reference in New Issue
Block a user