Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created February 27, 2011 20:01
Show Gist options
  • Save frsyuki/846477 to your computer and use it in GitHub Desktop.
Save frsyuki/846477 to your computer and use it in GitHub Desktop.
diff --git a/cpp/src/msgpack/object.hpp b/cpp/src/msgpack/object.hpp
index 96c026e..4d8f2f7 100644
--- a/cpp/src/msgpack/object.hpp
+++ b/cpp/src/msgpack/object.hpp
@@ -26,6 +26,7 @@
#include <typeinfo>
#include <limits>
#include <ostream>
+#include <tr1/type_traits>
namespace msgpack {
@@ -205,26 +206,90 @@ inline object& operator>> (object o, object& v)
return v;
}
+
+namespace type {
+namespace detail {
+
+ template <bool B, typename T = void>
+ struct enable_if_enum_check {
+ typedef T type;
+ };
+
+ template <typename T>
+ struct enable_if_enum_check<false, T> { };
+
+ template <typename E, typename T = void>
+ struct enable_if_enum : public enable_if_enum_check<std::tr1::is_enum<E>::value, T> { };
+
+ template <typename E, typename T = void>
+ struct disable_if_enum : public enable_if_enum_check<!std::tr1::is_enum<E>::value, T> { };
+
+} // namespace detail
+} // namespace type
+
+
template <typename T>
-inline T& operator>> (object o, T& v)
+inline T& msgpack_unpack(object o, T& v, typename type::detail::disable_if_enum<T>::type* = 0)
{
v.msgpack_unpack(o.convert());
return v;
}
+template <typename T>
+inline T& msgpack_unpack(object o, T& v, typename type::detail::enable_if_enum<T>::type* = 0)
+{
+ v = static_cast<T>(o.as<int>());
+ return v;
+}
+
+template <typename T>
+inline T& operator>> (object o, T& v)
+{
+ return msgpack_unpack(o, v);
+ //v.msgpack_unpack(o.convert());
+ //return v;
+}
+
template <typename Stream, typename T>
-inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
+inline packer<Stream>& msgpack_pack(packer<Stream>& o, const T& v, typename type::detail::disable_if_enum<T>::type* = 0)
{
v.msgpack_pack(o);
return o;
}
+template <typename Stream, typename T>
+inline packer<Stream>& msgpack_pack(packer<Stream>& o, T v, typename type::detail::enable_if_enum<T>::type* = 0)
+{
+ return o.pack_int(static_cast<int>(v));
+}
+
+template <typename Stream, typename T>
+inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
+{
+ return msgpack_pack(o, v);
+ //v.msgpack_pack(o);
+ //return o;
+}
+
+
template <typename T>
-void operator<< (object::with_zone& o, const T& v)
+void msgpack_deconvert(object::with_zone& o, const T& v, typename type::detail::disable_if_enum<T>::type* = 0)
{
v.msgpack_object(static_cast<object*>(&o), o.zone);
}
+template <typename T>
+void msgpack_deconvert(object::with_zone& o, T v, typename type::detail::enable_if_enum<T>::type* = 0)
+{
+ o << static_cast<int>(v);
+}
+
+template <typename T>
+void operator<< (object::with_zone& o, const T& v)
+{
+ msgpack_deconvert(o, v);
+}
+
inline bool operator==(const object x, const object y)
{
diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am
index 5225f28..41ea434 100644
--- a/cpp/test/Makefile.am
+++ b/cpp/test/Makefile.am
@@ -1,7 +1,7 @@
AM_CPPFLAGS = -I../src
AM_C_CPPFLAGS = -I../src
-AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main
+AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main -lpthread
check_PROGRAMS = \
zone \
diff --git a/cpp/test/convert.cc b/cpp/test/convert.cc
index f2a8523..4c10a93 100644
--- a/cpp/test/convert.cc
+++ b/cpp/test/convert.cc
@@ -1,3 +1,4 @@
+#include <tr1/type_traits>
#include <msgpack.hpp>
#include <gtest/gtest.h>
@@ -55,7 +56,7 @@ public:
flags_t flag;
- MSGPACK_DEFINE((int&)flag);
+ MSGPACK_DEFINE(flag);
};
TEST(convert, enum_member)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment