From 0482e4fcd113afefcb9d24a602a6dc13aa3547a3 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Mon, 9 Mar 2015 15:37:43 +0900 Subject: [PATCH] Supported the C++11 atomic. --- include/msgpack/unpack.hpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/msgpack/unpack.hpp b/include/msgpack/unpack.hpp index 2b6f1911..40bac13b 100644 --- a/include/msgpack/unpack.hpp +++ b/include/msgpack/unpack.hpp @@ -28,6 +28,11 @@ #include #include +#if !defined(MSGPACK_USE_CPP03) +#include +#endif + + #if defined(_MSC_VER) // avoiding confliction std::max, std::min, and macro in windows.h #ifndef NOMINMAX @@ -349,25 +354,46 @@ private: inline void init_count(void* buffer) { +#if defined(MSGPACK_USE_CPP03) *reinterpret_cast(buffer) = 1; +#else // defined(MSGPACK_USE_CPP03) + new (buffer) std::atomic(1); +#endif // defined(MSGPACK_USE_CPP03) } inline void decl_count(void* buffer) { +#if defined(MSGPACK_USE_CPP03) if(_msgpack_sync_decr_and_fetch(reinterpret_cast(buffer)) == 0) { free(buffer); } +#else // defined(MSGPACK_USE_CPP03) + if (--*reinterpret_cast*>(buffer) == 0) { + free(buffer); + } +#endif // defined(MSGPACK_USE_CPP03) } inline void incr_count(void* buffer) { +#if defined(MSGPACK_USE_CPP03) _msgpack_sync_incr_and_fetch(reinterpret_cast(buffer)); +#else // defined(MSGPACK_USE_CPP03) + ++*reinterpret_cast*>(buffer); +#endif // defined(MSGPACK_USE_CPP03) } +#if defined(MSGPACK_USE_CPP03) inline _msgpack_atomic_counter_t get_count(void* buffer) { return *reinterpret_cast(buffer); } +#else // defined(MSGPACK_USE_CPP03) +inline std::atomic const& get_count(void* buffer) +{ + return *reinterpret_cast*>(buffer); +} +#endif // defined(MSGPACK_USE_CPP03) struct fix_tag { char f1[65]; // FIXME unique size is required. or use is_same meta function.