diff --git a/.github/depends/zlib.sh b/.github/depends/zlib.sh index fce05159..3c2c8b25 100755 --- a/.github/depends/zlib.sh +++ b/.github/depends/zlib.sh @@ -27,9 +27,9 @@ while getopts "b:t:p:" c; do done mkdir $prefix || exit 1 -wget https://zlib.net/zlib-1.2.13.tar.gz || exit 1 -tar -xf zlib-1.2.13.tar.gz || exit 1 -cd zlib-1.2.13 +wget https://zlib.net/zlib-1.3.tar.gz || exit 1 +tar -xf zlib-1.3.tar.gz || exit 1 +cd zlib-1.3 build() { diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 8c3bf1ed..e286d279 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -56,6 +56,7 @@ jobs: -D MSGPACK_BUILD_TESTS=ON \ -D CMAKE_BUILD_TYPE=Debug \ -D MSGPACK_GEN_COVERAGE=ON \ + -D MSGPACK_USE_STD_VARIANT_ADAPTOR=ON \ -D CMAKE_PREFIX_PATH="$HOME/zlib-prefix/64;$HOME/boost-prefix/64" \ -B build \ -S . || exit 1 diff --git a/.github/workflows/gha.yml b/.github/workflows/gha.yml index 1fbb5ebc..29fd31bf 100644 --- a/.github/workflows/gha.yml +++ b/.github/workflows/gha.yml @@ -145,6 +145,7 @@ jobs: 3) export CXX="g++-10" export MSGPACK_CXX_VERSION="MSGPACK_CXX17=ON" + export MSGPACK_USE_STD_VARIANT_ADAPTOR="MSGPACK_USE_STD_VARIANT_ADAPTOR=ON" ;; 4) export CXX="clang++-10" diff --git a/.gitignore b/.gitignore index 40f5e0af..7b96d6df 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,8 @@ Makefile /test/streaming_c /test/version /test/zone + +build +*-build +.cache +compile_commands.json diff --git a/CHANGELOG.md b/CHANGELOG.md index f29bffe8..73199b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 2023-07-08 version 6.1.0 + * Remove dependency on boost in chrono.hpp (#1076) + * Add support for std::variant behavior (#1075) + * Fix msgpack::type::variant behavior to respect MessagePack format (#1071) + * Add rebind allocators (#1065) + # 2023-03-02 version 6.0.0 ## << breaking changes >> * Change CMake package name of C++ library to msgpack-cxx (#1054) diff --git a/CMakeLists.txt b/CMakeLists.txt index d44df4e2..8dc6d610 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,16 +23,17 @@ OPTION (MSGPACK_CXX14 "Using c++14 compiler" OFF) OPTION (MSGPACK_CXX17 "Using c++17 compiler" OFF) OPTION (MSGPACK_CXX20 "Using c++20 compiler" OFF) -OPTION (MSGPACK_32BIT "32bit compile" OFF) -OPTION (MSGPACK_USE_BOOST "Use Boost libraried" ON) -OPTION (MSGPACK_USE_X3_PARSE "Use Boost X3 parse" OFF) -OPTION (MSGPACK_BUILD_TESTS "Build tests" OFF) -OPTION (MSGPACK_BUILD_DOCS "Build Doxygen documentation" ON) -OPTION (MSGPACK_FUZZ_REGRESSION "Enable regression testing" OFF) -OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples" OFF) -OPTION (MSGPACK_GEN_COVERAGE "Generate coverage report" OFF) -OPTION (MSGPACK_USE_STATIC_BOOST "Statically link with boost libraries" OFF) -OPTION (MSGPACK_CHAR_SIGN "Char sign to use (signed or unsigned)") +OPTION (MSGPACK_32BIT "32bit compile" OFF) +OPTION (MSGPACK_USE_BOOST "Use Boost libraried" ON) +OPTION (MSGPACK_USE_X3_PARSE "Use Boost X3 parse" OFF) +OPTION (MSGPACK_BUILD_TESTS "Build tests" OFF) +OPTION (MSGPACK_BUILD_DOCS "Build Doxygen documentation" ON) +OPTION (MSGPACK_FUZZ_REGRESSION "Enable regression testing" OFF) +OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples" OFF) +OPTION (MSGPACK_GEN_COVERAGE "Generate coverage report" OFF) +OPTION (MSGPACK_USE_STATIC_BOOST "Statically link with boost libraries" OFF) +OPTION (MSGPACK_CHAR_SIGN "Char sign to use (signed or unsigned)") +OPTION (MSGPACK_USE_STD_VARIANT_ADAPTOR "Enable the adaptor for std::variant" OFF) SET (CMAKE_CXX_STANDARD_REQUIRED ON) @@ -92,6 +93,10 @@ ELSE () TARGET_COMPILE_DEFINITIONS(msgpack-cxx INTERFACE MSGPACK_DEFAULT_API_VERSION=3) ENDIF () +IF (MSGPACK_USE_STD_VARIANT_ADAPTOR) + TARGET_COMPILE_DEFINITIONS(msgpack-cxx INTERFACE MSGPACK_USE_STD_VARIANT_ADAPTOR) +ENDIF () + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.1) INCLUDE (CheckCXXSourceCompiles) diff --git a/Files.cmake b/Files.cmake index 5cdf80af..721a31fb 100644 --- a/Files.cmake +++ b/Files.cmake @@ -33,6 +33,7 @@ SET (msgpack-cxx_HEADERS include/msgpack/adaptor/cpp17/carray_byte.hpp include/msgpack/adaptor/cpp17/optional.hpp include/msgpack/adaptor/cpp17/string_view.hpp + include/msgpack/adaptor/cpp17/variant.hpp include/msgpack/adaptor/cpp17/vector_byte.hpp include/msgpack/adaptor/cpp20/span.hpp include/msgpack/adaptor/define.hpp @@ -542,6 +543,7 @@ SET (msgpack-cxx_HEADERS include/msgpack/v1/adaptor/cpp17/carray_byte.hpp include/msgpack/v1/adaptor/cpp17/optional.hpp include/msgpack/v1/adaptor/cpp17/string_view.hpp + include/msgpack/v1/adaptor/cpp17/variant.hpp include/msgpack/v1/adaptor/cpp17/vector_byte.hpp include/msgpack/v1/adaptor/cpp20/span.hpp include/msgpack/v1/adaptor/define.hpp diff --git a/README.md b/README.md index 9f6a459b..f4103c11 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ `msgpack` for C++ =================== -Version 6.0.0 [![Build Status](https://github.com/msgpack/msgpack-c/workflows/CI/badge.svg?branch=cpp_master)](https://github.com/msgpack/msgpack-c/actions) [![Build status](https://ci.appveyor.com/api/projects/status/8kstcgt79qj123mw/branch/cpp_master?svg=true)](https://ci.appveyor.com/project/redboltz/msgpack-c/branch/cpp_master) +Version 6.1.0 [![Build Status](https://github.com/AztecProtocol/msgpack-c/actions/workflows/gha.yml/badge.svg)](https://github.com/msgpack/msgpack-c/actions) [![Build status](https://ci.appveyor.com/api/projects/status/8kstcgt79qj123mw/branch/cpp_master?svg=true)](https://ci.appveyor.com/project/redboltz/msgpack-c/branch/cpp_master) [![codecov](https://codecov.io/gh/msgpack/msgpack-c/branch/cpp_master/graph/badge.svg)](https://codecov.io/gh/msgpack/msgpack-c/branch/cpp_master) It's like JSON but smaller and faster. diff --git a/appveyor.yml b/appveyor.yml index 4218f3a3..6f5034b8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 6.0.0.{build} +version: 6.1.0.{build} branches: only: @@ -23,10 +23,10 @@ environment: boost_subdir: lib32-msvc-14.0 build_script: - ps: | - appveyor DownloadFile http://zlib.net/zlib-1.2.13.tar.gz -FileName zlib-1.2.13.tar.gz - 7z x zlib-1.2.13.tar.gz 2> $null - 7z x zlib-1.2.13.tar 2> $null - cd zlib-1.2.13 + appveyor DownloadFile http://zlib.net/zlib-1.3.tar.gz -FileName zlib-1.3.tar.gz + 7z x zlib-1.3.tar.gz 2> $null + 7z x zlib-1.3.tar 2> $null + cd zlib-1.3 md build md prefix @@ -34,7 +34,7 @@ build_script: cmake ` -G $env:msvc ` - -D CMAKE_INSTALL_PREFIX="$env:APPVEYOR_BUILD_FOLDER\zlib-1.2.13\prefix" ` + -D CMAKE_INSTALL_PREFIX="$env:APPVEYOR_BUILD_FOLDER\zlib-1.3\prefix" ` .. if ($LastExitCode -ne 0) { exit $LastExitCode } @@ -52,7 +52,7 @@ build_script: -D MSGPACK_BUILD_EXAMPLES=ON ` -D MSGPACK_BUILD_TESTS=ON ` -D CMAKE_EXE_LINKER_FLAGS=/LIBPATH:"$env:boost_prefix\$env:boost_subdir" ` - -D CMAKE_PREFIX_PATH="$env:boost_prefix;$env:APPVEYOR_BUILD_FOLDER\zlib-1.2.13\prefix" ` + -D CMAKE_PREFIX_PATH="$env:boost_prefix;$env:APPVEYOR_BUILD_FOLDER\zlib-1.3\prefix" ` -D CMAKE_INSTALL_PREFIX="$env:APPVEYOR_BUILD_FOLDER\prefix" ` -D CMAKE_CXX_FLAGS="/D_VARIADIC_MAX=10 /EHsc /DBOOST_ALL_DYN_LINK" ` .. @@ -62,5 +62,5 @@ build_script: if ($LastExitCode -ne 0) { exit $LastExitCode } test_script: -- set PATH=%PATH%;%APPVEYOR_BUILD_FOLDER%\zlib-1.2.13\build\Release;%APPVEYOR_BUILD_FOLDER%\build\release;%boost_prefix%\%boost_subdir% +- set PATH=%PATH%;%APPVEYOR_BUILD_FOLDER%\zlib-1.3\build\Release;%APPVEYOR_BUILD_FOLDER%\build\release;%boost_prefix%\%boost_subdir% - ctest -VV -C Release diff --git a/ci/build_cmake.sh b/ci/build_cmake.sh index a89c4464..b6ad78c5 100755 --- a/ci/build_cmake.sh +++ b/ci/build_cmake.sh @@ -21,6 +21,7 @@ cmake \ -D MSGPACK_CHAR_SIGN=${CHAR_SIGN} \ -D MSGPACK_DEFAULT_API_VERSION=${API_VERSION} \ -D MSGPACK_USE_X3_PARSE=${X3_PARSE} \ + -D MSGPACK_USE_STD_VARIANT_ADAPTOR=${STD_VARIANT_ADAPTOR} \ -D CMAKE_CXX_FLAGS="${CXXFLAGS} ${ARCH_FLAG}" \ -D CMAKE_INSTALL_PREFIX=$prefix_dir \ -B $build_dir \ diff --git a/erb/v1/cpp03_zone.hpp.erb b/erb/v1/cpp03_zone.hpp.erb index 6b3a57d3..86bc8328 100644 --- a/erb/v1/cpp03_zone.hpp.erb +++ b/erb/v1/cpp03_zone.hpp.erb @@ -29,121 +29,101 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { class zone { struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} + finalizer(void (*func)(void*), void* data, finalizer* next): m_func(func), m_data(data), m_next(next) {} void operator()() { m_func(m_data); } void (*m_func)(void*); void* m_data; + finalizer* m_next; }; + struct finalizer_array { - finalizer_array():m_tail(MSGPACK_NULLPTR), m_end(MSGPACK_NULLPTR), m_array(MSGPACK_NULLPTR) {} - void call() { - finalizer* fin = m_tail; - for(; fin != m_array; --fin) (*(fin-1))(); - } + finalizer_array(): m_head(MSGPACK_NULLPTR) {} + ~finalizer_array() { - call(); - ::free(m_array); - } - void clear() { - call(); - m_tail = m_array; + clear(); } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; - if(fin == m_end) { - push_expand(func, data); - return; + void clear() { + finalizer* fin = m_head; + finalizer* tmp = MSGPACK_NULLPTR; + while(fin) { + (*fin)(); + tmp = fin; + fin = fin->m_next; + delete tmp; } + m_head = MSGPACK_NULLPTR; + } - fin->m_func = func; - fin->m_data = data; - - ++m_tail; + void push(void (*func)(void* data), void* data) { + m_head = new finalizer(func, data, m_head); } - void push_expand(void (*func)(void*), void* data) { - const size_t nused = static_cast(m_end - m_array); - size_t nnext; - if(nused == 0) { - nnext = (sizeof(finalizer) < 72/2) ? - 72 / sizeof(finalizer) : 8; - } else { - nnext = nused * 2; - } - finalizer* tmp = - static_cast(::realloc(m_array, sizeof(finalizer) * nnext)); - if(!tmp) { - throw std::bad_alloc(); - } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - ++m_tail; + void pop() { + finalizer* n = m_head->m_next; + delete m_head; + m_head = n; } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; + + finalizer* m_head; + private: + finalizer_array(const finalizer_array&); + finalizer_array& operator=(const finalizer_array&); }; + struct chunk { chunk* m_next; }; - struct chunk_list { - chunk_list(size_t chunk_size) - { - chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); - if(!c) { - throw std::bad_alloc(); - } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { + struct chunk_list { + chunk_list(size_t chunk_size, char* ptr): m_free(chunk_size), m_ptr(ptr), m_head(MSGPACK_NULLPTR) {} + ~chunk_list() { chunk* c = m_head; while(c) { chunk* n = c->m_next; ::free(c); c = n; } + m_head = MSGPACK_NULLPTR; } - void clear(size_t chunk_size) - { + + void clear(size_t chunk_size, char* ptr) { chunk* c = m_head; - while(true) { + while(c) { chunk* n = c->m_next; - if(n) { - ::free(c); - c = n; - } else { - m_head = c; - break; - } + ::free(c); + c = n; } - m_head->m_next = MSGPACK_NULLPTR; + m_head = MSGPACK_NULLPTR; m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); + m_ptr = ptr; } + size_t m_free; char* m_ptr; chunk* m_head; + + private: + chunk_list(const chunk_list&); + chunk_list& operator=(const chunk_list&); }; + size_t m_chunk_size; - chunk_list m_chunk_list; + chunk_list* m_chunk_list; finalizer_array m_finalizer_array; public: zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); -public: void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); + void* allocate_no_align(size_t size); + bool allocated() { + return m_chunk_list != MSGPACK_NULLPTR; + } + void push_finalizer(void (*func)(void*), void* data); template @@ -152,24 +132,23 @@ public: void clear(); void swap(zone& o); - static void* operator new(std::size_t size) - { + + static void* operator new(std::size_t size) { void* p = ::malloc(size); if (!p) throw std::bad_alloc(); return p; } - static void operator delete(void *p) /* throw() */ - { + + static void operator delete(void *p) /* throw() */ { ::free(p); } - static void* operator new(std::size_t size, void* place) /* throw() */ - { - return ::operator new(size, place); - } - static void operator delete(void* p, void* place) /* throw() */ - { - ::operator delete(p, place); + + static void* operator new(std::size_t /*size*/, void* mem) /* throw() */ { + return mem; } + + static void operator delete(void * /*p*/, void* /*mem*/) /* throw() */ {} + /// @cond <%0.upto(GENERATION_LIMIT) {|i|%> template , typename A<%=j%><%}%>> @@ -188,18 +167,26 @@ private: static char* get_aligned(char* ptr, size_t align); + chunk_list& get_chank_lst(); + char* allocate_expand(size_t size); private: zone(const zone&); zone& operator=(const zone&); }; -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ +inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(MSGPACK_NULLPTR) {} + +inline zone::~zone() { + m_finalizer_array.~finalizer_array(); + if(m_chunk_list) { + m_chunk_list->~chunk_list(); + ::free(m_chunk_list); + m_chunk_list = MSGPACK_NULLPTR; + } } -inline char* zone::get_aligned(char* ptr, size_t align) -{ +inline char* zone::get_aligned(char* ptr, size_t align) { MSGPACK_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( @@ -207,37 +194,45 @@ inline char* zone::get_aligned(char* ptr, size_t align) ); } -inline void* zone::allocate_align(size_t size, size_t align) -{ - char* aligned = get_aligned(m_chunk_list.m_ptr, align); - size_t adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); - if (m_chunk_list.m_free < adjusted_size) { +inline zone::chunk_list& zone::get_chank_lst() { + if (!m_chunk_list) { + void* ptr = ::malloc(sizeof(chunk_list) + m_chunk_size); + if (!ptr) + throw std::bad_alloc(); + m_chunk_list = new (ptr) chunk_list(m_chunk_size, reinterpret_cast(ptr) + sizeof(chunk_list)); + } + return *m_chunk_list; +} + +inline void* zone::allocate_align(size_t size, size_t align) { + chunk_list& chank_lst = get_chank_lst(); + char* aligned = get_aligned(chank_lst.m_ptr, align); + size_t adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); + if (chank_lst.m_free < adjusted_size) { size_t enough_size = size + align - 1; char* ptr = allocate_expand(enough_size); aligned = get_aligned(ptr, align); - adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); + adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; return aligned; } -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { +inline void* zone::allocate_no_align(size_t size) { + chunk_list& chank_lst = get_chank_lst(); + char* ptr = chank_lst.m_ptr; + if(chank_lst.m_free < size) { ptr = allocate_expand(size); } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + chank_lst.m_free -= size; + chank_lst.m_ptr += size; return ptr; } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; - +inline char* zone::allocate_expand(size_t size) { + chunk_list& cl = get_chank_lst(); size_t sz = m_chunk_size; while(sz < size) { @@ -254,60 +249,54 @@ inline char* zone::allocate_expand(size_t size) char* ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; return ptr; } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ +inline void zone::push_finalizer(void (*func)(void*), void* data) { m_finalizer_array.push(func, data); } template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ +inline void zone::push_finalizer(msgpack::unique_ptr obj) { m_finalizer_array.push(&zone::object_delete, obj.release()); } -inline void zone::clear() -{ +inline void zone::clear() { m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -inline void zone::swap(zone& o) -{ +inline void zone::swap(zone& o) { using std::swap; swap(m_chunk_size, o.m_chunk_size); swap(m_chunk_list, o.m_chunk_list); - swap(m_finalizer_array, o.m_finalizer_array); + swap(m_finalizer_array.m_head, o.m_finalizer_array.m_head); } template -void zone::object_destruct(void* obj) -{ - static_cast(obj)->~T(); +void zone::object_delete(void* obj) { + delete static_cast(obj); } template -void zone::object_delete(void* obj) -{ - delete static_cast(obj); +void zone::object_destruct(void* obj) { + static_cast(obj)->~T(); } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; +inline void zone::undo_allocate(size_t size) { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { +inline std::size_t aligned_size(std::size_t size, std::size_t align) { return (size + align - 1) / align * align; } @@ -321,14 +310,14 @@ T* zone::allocate(<%=(1..i).map{|j|"A#{j} a#{j}"}.join(', ')%>) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(<%=(1..i).map{|j|"a#{j}"}.join(', ')%>); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } <%}%> diff --git a/example/boost/CMakeLists.txt b/example/boost/CMakeLists.txt index ad9c979c..6464513b 100644 --- a/example/boost/CMakeLists.txt +++ b/example/boost/CMakeLists.txt @@ -1,4 +1,4 @@ -FIND_PACKAGE (Boost REQUIRED COMPONENTS system) +FIND_PACKAGE (Boost REQUIRED) FIND_PACKAGE (Threads REQUIRED) FIND_PACKAGE (ZLIB REQUIRED) @@ -22,7 +22,6 @@ FOREACH (source_file ${exec_PROGRAMS}) ) TARGET_LINK_LIBRARIES (${source_file_we} PRIVATE msgpack-cxx - Boost::system Threads::Threads ) IF (ZLIB_FOUND) diff --git a/example/x3/CMakeLists.txt b/example/x3/CMakeLists.txt index 5fc6c1af..963d6da4 100644 --- a/example/x3/CMakeLists.txt +++ b/example/x3/CMakeLists.txt @@ -1,5 +1,5 @@ IF (MSGPACK_USE_X3_PARSE AND MSGPACK_DEFAULT_API_VERSION VERSION_GREATER 1) - FIND_PACKAGE (Boost REQUIRED COMPONENTS context system) + FIND_PACKAGE (Boost REQUIRED COMPONENTS context) FIND_PACKAGE (Threads REQUIRED) LIST (APPEND exec_PROGRAMS @@ -42,7 +42,6 @@ IF (MSGPACK_USE_X3_PARSE AND MSGPACK_DEFAULT_API_VERSION VERSION_GREATER 1) TARGET_LINK_LIBRARIES (${source_file_we} PRIVATE msgpack-cxx Boost::context - Boost::system Threads::Threads ) IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") diff --git a/fuzz/CMakeLists.txt b/fuzz/CMakeLists.txt index 819130ed..97796874 100644 --- a/fuzz/CMakeLists.txt +++ b/fuzz/CMakeLists.txt @@ -1,5 +1,5 @@ FIND_PACKAGE (Threads REQUIRED) -FIND_PACKAGE (Boost REQUIRED COMPONENTS system filesystem unit_test_framework) +FIND_PACKAGE (Boost REQUIRED COMPONENTS filesystem unit_test_framework) LIST (APPEND check_PROGRAMS regression_runner.cpp @@ -19,7 +19,6 @@ FOREACH (source_file ${check_PROGRAMS}) msgpack-cxx Threads::Threads Boost::filesystem - Boost::system Boost::unit_test_framework ) diff --git a/include/msgpack/adaptor/cpp17/variant.hpp b/include/msgpack/adaptor/cpp17/variant.hpp new file mode 100644 index 00000000..bd73ff9a --- /dev/null +++ b/include/msgpack/adaptor/cpp17/variant.hpp @@ -0,0 +1,16 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2023 Uy Ha +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef MSGPACK_TYPE_CPP17_VARIANT_HPP +#define MSGPACK_TYPE_CPP17_VARIANT_HPP + +#include "msgpack/v1/adaptor/cpp17/variant.hpp" + +#endif // MSGPACK_TYPE_CPP17_VARIANT_HPP diff --git a/include/msgpack/adaptor/tr1/unordered_map.hpp b/include/msgpack/adaptor/tr1/unordered_map.hpp index b24ac231..468220f9 100644 --- a/include/msgpack/adaptor/tr1/unordered_map.hpp +++ b/include/msgpack/adaptor/tr1/unordered_map.hpp @@ -47,7 +47,7 @@ namespace adaptor { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_map& v) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); MSGPACK_STD_TR1::unordered_map tmp; @@ -103,7 +103,7 @@ struct object_with_zone template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_multimap& v) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); MSGPACK_STD_TR1::unordered_multimap tmp; diff --git a/include/msgpack/adaptor/tr1/unordered_set.hpp b/include/msgpack/adaptor/tr1/unordered_set.hpp index 9c92c210..339815a2 100644 --- a/include/msgpack/adaptor/tr1/unordered_set.hpp +++ b/include/msgpack/adaptor/tr1/unordered_set.hpp @@ -47,7 +47,7 @@ namespace adaptor { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_set& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; MSGPACK_STD_TR1::unordered_set tmp; @@ -101,7 +101,7 @@ struct object_with_zone template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_multiset& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; MSGPACK_STD_TR1::unordered_multiset tmp; diff --git a/include/msgpack/assert.hpp b/include/msgpack/assert.hpp index 8eeccf82..0b2d0bcd 100644 --- a/include/msgpack/assert.hpp +++ b/include/msgpack/assert.hpp @@ -23,4 +23,20 @@ #endif // defined(MSGPACK_NO_BOOST) +#ifdef __wasm__ +struct AbortStream { + void operator<< [[noreturn]] (const auto& error) { + info(error.what()); + std::abort(); + } +}; +#define THROW AbortStream() << +#define try if (true) +#define catch(...) if (false) +#define RETHROW +#else +#define THROW throw +#define RETHROW THROW +#endif + #endif // MSGPACK_ASSERT_HPP diff --git a/include/msgpack/sysdep.hpp b/include/msgpack/sysdep.hpp index 0b857b77..e31a6425 100644 --- a/include/msgpack/sysdep.hpp +++ b/include/msgpack/sysdep.hpp @@ -141,9 +141,9 @@ # endif # if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400) -# define _msgpack_be64(x) (_byteswap_uint64(x)) +# define _msgpack_be64(x) (_byteswap_uint64((uint64_t)x)) # elif defined(bswap_64) -# define _msgpack_be64(x) bswap_64(x) +# define _msgpack_be64(x) bswap_64((uint64_t)x) # elif defined(__DARWIN_OSSwapInt64) # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x) # else @@ -183,11 +183,11 @@ } while (0); #define _msgpack_store16(to, num) \ - do { uint16_t val = _msgpack_be16(num); memcpy(to, &val, 2); } while(0) + do { uint16_t val = (uint16_t)_msgpack_be16(num); memcpy(to, &val, 2); } while(0) #define _msgpack_store32(to, num) \ - do { uint32_t val = _msgpack_be32(num); memcpy(to, &val, 4); } while(0) + do { uint32_t val = (uint32_t)_msgpack_be32(num); memcpy(to, &val, 4); } while(0) #define _msgpack_store64(to, num) \ - do { uint64_t val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0) + do { uint64_t val = (uint64_t)_msgpack_be64(num); memcpy(to, &val, 8); } while(0) /* #define _msgpack_load16(cast, from) \ diff --git a/include/msgpack/type.hpp b/include/msgpack/type.hpp index e48bdade..1ab49745 100644 --- a/include/msgpack/type.hpp +++ b/include/msgpack/type.hpp @@ -37,9 +37,7 @@ #include "adaptor/cpp11/array_char.hpp" #include "adaptor/cpp11/array_unsigned_char.hpp" -#if !defined(MSGPACK_NO_BOOST) #include "adaptor/cpp11/chrono.hpp" -#endif // !defined(MSGPACK_NO_BOOST) #include "adaptor/cpp11/forward_list.hpp" #include "adaptor/cpp11/reference_wrapper.hpp" @@ -63,6 +61,10 @@ #include "adaptor/cpp17/carray_byte.hpp" #include "adaptor/cpp17/vector_byte.hpp" +#if MSGPACK_HAS_INCLUDE() +#include "adaptor/cpp17/variant.hpp" +#endif // MSGPACK_HAS_INCLUDE() + #if MSGPACK_HAS_INCLUDE() #include "adaptor/cpp20/span.hpp" #endif // MSGPACK_HAS_INCLUDE() diff --git a/include/msgpack/v1/adaptor/array_ref.hpp b/include/msgpack/v1/adaptor/array_ref.hpp index d44be164..6051d3f6 100644 --- a/include/msgpack/v1/adaptor/array_ref.hpp +++ b/include/msgpack/v1/adaptor/array_ref.hpp @@ -154,9 +154,9 @@ namespace adaptor { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref& v) const { - if (!v.data) { throw msgpack::type_error(); } - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if (v.size() < o.via.bin.size) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if (v.size() < o.via.bin.size) { THROW msgpack::type_error(); } if (o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; @@ -174,9 +174,9 @@ struct convert > { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref& v) const { - if (!v.data) { throw msgpack::type_error(); } - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if (v.size() < o.via.bin.size) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if (v.size() < o.via.bin.size) { THROW msgpack::type_error(); } if (o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; @@ -194,8 +194,8 @@ struct convert > { template struct convert > > { msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref >& v) const { - if (!v.data) { throw msgpack::type_error(); } - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } v.data->resize(o.via.bin.size); if (o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; @@ -215,7 +215,7 @@ template struct pack > { template msgpack::packer& operator()(msgpack::packer& o, const msgpack::type::array_ref& v) const { - if (!v.data) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } uint32_t size = checked_get_container_size(v.size()); o.pack_array(size); for (typename T::const_iterator it(v.data->begin()), it_end(v.data->end()); @@ -230,7 +230,7 @@ template struct pack > { template msgpack::packer& operator()(msgpack::packer& o, const msgpack::type::array_ref& v) const { - if (!v.data) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } uint32_t size = checked_get_container_size(v.size()); o.pack_array(size); for (T const* it = v.data; @@ -244,7 +244,7 @@ struct pack > { template struct object_with_zone > { void operator()(msgpack::object::with_zone& o, const msgpack::type::array_ref& v) const { - if (!v.data) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } o.type = msgpack::type::ARRAY; if (v.data->empty()) { o.via.array.ptr = MSGPACK_NULLPTR; @@ -277,7 +277,7 @@ struct object_with_zone > { template struct object_with_zone > { void operator()(msgpack::object::with_zone& o, const msgpack::type::array_ref& v) const { - if (!v.data) { throw msgpack::type_error(); } + if (!v.data) { THROW msgpack::type_error(); } o.type = msgpack::type::ARRAY; uint32_t size = checked_get_container_size(v.size()); msgpack::object* p = static_cast(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object))); diff --git a/include/msgpack/v1/adaptor/bool.hpp b/include/msgpack/v1/adaptor/bool.hpp index 420d5398..9836bce9 100644 --- a/include/msgpack/v1/adaptor/bool.hpp +++ b/include/msgpack/v1/adaptor/bool.hpp @@ -25,7 +25,7 @@ namespace adaptor { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, bool& v) const { - if(o.type != msgpack::type::BOOLEAN) { throw msgpack::type_error(); } + if(o.type != msgpack::type::BOOLEAN) { THROW msgpack::type_error(); } v = o.via.boolean; return o; } diff --git a/include/msgpack/v1/adaptor/boost/fusion.hpp b/include/msgpack/v1/adaptor/boost/fusion.hpp index 9d12a368..d7b5019a 100644 --- a/include/msgpack/v1/adaptor/boost/fusion.hpp +++ b/include/msgpack/v1/adaptor/boost/fusion.hpp @@ -112,9 +112,9 @@ struct as< >::type > { T operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } if (o.via.array.size != checked_get_container_size(boost::mpl::size::value)) { - throw msgpack::type_error(); + THROW msgpack::type_error(); } using tuple_t = decltype(to_tuple(std::declval(), gen_seq::value>())); return to_t( @@ -140,9 +140,9 @@ struct as< template struct convert::value>::type > { msgpack::object const& operator()(msgpack::object const& o, T& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } if (o.via.array.size != checked_get_container_size(boost::fusion::size(v))) { - throw msgpack::type_error(); + THROW msgpack::type_error(); } uint32_t index = 0; boost::fusion::for_each(v, convert_imp(o, index)); diff --git a/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp b/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp index ecf8ac17..509183ff 100644 --- a/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp +++ b/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp @@ -122,6 +122,12 @@ struct basic_variant : int_init(v); } basic_variant(unsigned long long v):base(uint64_t(v)) {} + basic_variant(float v) { + double_init(v); + } + basic_variant(double v) { + double_init(v); + } bool is_nil() const { return boost::get(this) != MSGPACK_NULLPTR; @@ -177,71 +183,50 @@ struct basic_variant : int64_t as_int64_t() const { return boost::get(*this); } - int64_t& as_int64_t() { - return boost::get(*this); - } uint64_t as_uint64_t() const { return boost::get(*this); } - uint64_t& as_uint64_t() { - return boost::get(*this); - } double as_double() const { - return boost::get(*this); - } - double& as_double() { - return boost::get(*this); + if (is_double()) { + return boost::get(*this); + } + if (is_int64_t()) { + return static_cast(boost::get(*this)); + } + if (is_uint64_t()) { + return static_cast(boost::get(*this)); + } + THROW msgpack::type_error(); } std::string const& as_string() const { return boost::get(*this); } - std::string& as_string() { - return boost::get(*this); - } #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 boost::string_ref const& as_boost_string_ref() const { return boost::get(*this); } - boost::string_ref& as_boost_string_ref() { - return boost::get(*this); - } #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 std::vector const& as_vector_char() const { return boost::get >(*this); } - std::vector& as_vector_char() { - return boost::get >(*this); - } raw_ref const& as_raw_ref() const { return boost::get(*this); } ext const& as_ext() const { return boost::get(*this); } - ext& as_ext() { - return boost::get(*this); - } ext_ref const& as_ext_ref() const { return boost::get(*this); } std::vector > const& as_vector() const { return boost::get > >(*this); } - std::vector >& as_vector() { - return boost::get > >(*this); - } std::map, basic_variant > const& as_map() const { return boost::get, basic_variant > >(*this); } - std::map, basic_variant >& as_map() { - return boost::get, basic_variant > >(*this); - } std::multimap, basic_variant > const& as_multimap() const { return boost::get, basic_variant > >(*this); } - std::multimap, basic_variant >& as_multimap() { - return boost::get, basic_variant > >(*this); - } private: template void int_init(T v) { @@ -252,6 +237,19 @@ struct basic_variant : static_cast(*this) = uint64_t(v); } } + void double_init(double v) { + if (v == v) { // check for nan + if (v >= 0 && v <= double(std::numeric_limits::max()) && v == double(uint64_t(v))) { + static_cast(*this) = uint64_t(v); + return; + } + else if (v < 0 && v >= double(std::numeric_limits::min()) && v == double(int64_t(v))) { + static_cast(*this) = int64_t(v); + return; + } + } + static_cast(*this) = v; + } }; template @@ -405,7 +403,7 @@ struct object_imp : boost::static_visitor { } template void operator()(T const&) const { - throw msgpack::type_error(); + THROW msgpack::type_error(); } object_imp(msgpack::object& o):o_(o) {} msgpack::object& o_; diff --git a/include/msgpack/v1/adaptor/boost/string_ref.hpp b/include/msgpack/v1/adaptor/boost/string_ref.hpp index 79b60898..63ad2ae2 100644 --- a/include/msgpack/v1/adaptor/boost/string_ref.hpp +++ b/include/msgpack/v1/adaptor/boost/string_ref.hpp @@ -39,7 +39,7 @@ struct convert { v = boost::string_ref(o.via.str.ptr, o.via.str.size); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/boost/string_view.hpp b/include/msgpack/v1/adaptor/boost/string_view.hpp index e1743900..91069c0a 100644 --- a/include/msgpack/v1/adaptor/boost/string_view.hpp +++ b/include/msgpack/v1/adaptor/boost/string_view.hpp @@ -39,7 +39,7 @@ struct convert { v = boost::string_view(o.via.str.ptr, o.via.str.size); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/carray.hpp b/include/msgpack/v1/adaptor/carray.hpp index 8fff4468..4f71a600 100644 --- a/include/msgpack/v1/adaptor/carray.hpp +++ b/include/msgpack/v1/adaptor/carray.hpp @@ -26,8 +26,8 @@ namespace adaptor { template struct convert { msgpack::object const& operator()(msgpack::object const& o, T* v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if (o.via.array.size > N) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if (o.via.array.size > N) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; do { @@ -44,16 +44,16 @@ struct convert { msgpack::object const& operator()(msgpack::object const& o, char(&v)[N]) const { switch (o.type) { case msgpack::type::BIN: - if (o.via.bin.size > N) { throw msgpack::type_error(); } + if (o.via.bin.size > N) { THROW msgpack::type_error(); } std::memcpy(v, o.via.bin.ptr, o.via.bin.size); break; case msgpack::type::STR: - if (o.via.str.size > N) { throw msgpack::type_error(); } + if (o.via.str.size > N) { THROW msgpack::type_error(); } std::memcpy(v, o.via.str.ptr, o.via.str.size); if (o.via.str.size < N) v[o.via.str.size] = '\0'; break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; @@ -65,16 +65,16 @@ struct convert { msgpack::object const& operator()(msgpack::object const& o, unsigned char(&v)[N]) const { switch (o.type) { case msgpack::type::BIN: - if (o.via.bin.size > N) { throw msgpack::type_error(); } + if (o.via.bin.size > N) { THROW msgpack::type_error(); } std::memcpy(v, o.via.bin.ptr, o.via.bin.size); break; case msgpack::type::STR: - if (o.via.str.size > N) { throw msgpack::type_error(); } + if (o.via.str.size > N) { THROW msgpack::type_error(); } std::memcpy(v, o.via.str.ptr, o.via.str.size); if (o.via.str.size < N) v[o.via.str.size] = '\0'; break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/check_container_size.hpp b/include/msgpack/v1/adaptor/check_container_size.hpp index 8c4f23ce..7cfb4e89 100644 --- a/include/msgpack/v1/adaptor/check_container_size.hpp +++ b/include/msgpack/v1/adaptor/check_container_size.hpp @@ -32,7 +32,7 @@ namespace detail { template inline void check_container_size(std::size_t size) { - if (size > 0xffffffff) throw container_size_overflow("container size overflow"); + if (size > 0xffffffff) THROW container_size_overflow("container size overflow"); } template <> @@ -41,12 +41,12 @@ inline void check_container_size<4>(std::size_t /*size*/) { template inline void check_container_size_for_ext(std::size_t size) { - if (size > 0xffffffff) throw container_size_overflow("container size overflow"); + if (size > 0xffffffff) THROW container_size_overflow("container size overflow"); } template <> inline void check_container_size_for_ext<4>(std::size_t size) { - if (size > 0xfffffffe) throw container_size_overflow("container size overflow"); + if (size > 0xfffffffe) THROW container_size_overflow("container size overflow"); } } // namespace detail diff --git a/include/msgpack/v1/adaptor/complex.hpp b/include/msgpack/v1/adaptor/complex.hpp index d631b4ac..b8d86b30 100644 --- a/include/msgpack/v1/adaptor/complex.hpp +++ b/include/msgpack/v1/adaptor/complex.hpp @@ -30,9 +30,9 @@ template struct as, typename std::enable_if::value>::type> { std::complex operator()(msgpack::object const& o) const { if (o.type != msgpack::type::ARRAY) - throw msgpack::type_error(); + THROW msgpack::type_error(); if (o.via.array.size != 2) - throw msgpack::type_error(); + THROW msgpack::type_error(); return std::complex(o.via.array.ptr[0].as(), o.via.array.ptr[1].as()); } }; @@ -43,9 +43,9 @@ template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::complex& v) const { if(o.type != msgpack::type::ARRAY) - throw msgpack::type_error(); + THROW msgpack::type_error(); if(o.via.array.size != 2) - throw msgpack::type_error(); + THROW msgpack::type_error(); T real; T imag; o.via.array.ptr[0].convert(real); diff --git a/include/msgpack/v1/adaptor/cpp11/array.hpp b/include/msgpack/v1/adaptor/cpp11/array.hpp index e816d64a..fdb48543 100644 --- a/include/msgpack/v1/adaptor/cpp11/array.hpp +++ b/include/msgpack/v1/adaptor/cpp11/array.hpp @@ -75,8 +75,8 @@ struct as_impl { template struct as, typename std::enable_if::value>::type> { std::array operator()(msgpack::object const& o) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if(o.via.array.size > N) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if(o.via.array.size > N) { THROW msgpack::type_error(); } return detail::array::as_impl::as(o); } }; @@ -84,8 +84,8 @@ struct as, typename std::enable_if::value>:: template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::array& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if(o.via.array.size > N) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if(o.via.array.size > N) { THROW msgpack::type_error(); } if(o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; diff --git a/include/msgpack/v1/adaptor/cpp11/array_char.hpp b/include/msgpack/v1/adaptor/cpp11/array_char.hpp index 01888f5a..e5285290 100644 --- a/include/msgpack/v1/adaptor/cpp11/array_char.hpp +++ b/include/msgpack/v1/adaptor/cpp11/array_char.hpp @@ -31,15 +31,15 @@ struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::array& v) const { switch (o.type) { case msgpack::type::BIN: - if(o.via.bin.size > N) { throw msgpack::type_error(); } + if(o.via.bin.size > N) { THROW msgpack::type_error(); } std::memcpy(v.data(), o.via.bin.ptr, o.via.bin.size); break; case msgpack::type::STR: - if(o.via.str.size > N) { throw msgpack::type_error(); } + if(o.via.str.size > N) { THROW msgpack::type_error(); } std::memcpy(v.data(), o.via.str.ptr, N); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/cpp11/array_unsigned_char.hpp b/include/msgpack/v1/adaptor/cpp11/array_unsigned_char.hpp index 0c698f0c..e6d1d9ed 100644 --- a/include/msgpack/v1/adaptor/cpp11/array_unsigned_char.hpp +++ b/include/msgpack/v1/adaptor/cpp11/array_unsigned_char.hpp @@ -31,15 +31,15 @@ struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::array& v) const { switch (o.type) { case msgpack::type::BIN: - if(o.via.bin.size > N) { throw msgpack::type_error(); } + if(o.via.bin.size > N) { THROW msgpack::type_error(); } std::memcpy(v.data(), o.via.bin.ptr, o.via.bin.size); break; case msgpack::type::STR: - if(o.via.str.size > N) { throw msgpack::type_error(); } + if(o.via.str.size > N) { THROW msgpack::type_error(); } std::memcpy(v.data(), o.via.str.ptr, N); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/cpp11/chrono.hpp b/include/msgpack/v1/adaptor/cpp11/chrono.hpp index f545326d..e543c0ac 100644 --- a/include/msgpack/v1/adaptor/cpp11/chrono.hpp +++ b/include/msgpack/v1/adaptor/cpp11/chrono.hpp @@ -11,17 +11,14 @@ #ifndef MSGPACK_V1_TYPE_CPP11_CHRONO_HPP #define MSGPACK_V1_TYPE_CPP11_CHRONO_HPP -#if !defined(MSGPACK_NO_BOOST) - #include "msgpack/versioning.hpp" #include "msgpack/adaptor/adaptor_base.hpp" #include "msgpack/object.hpp" #include "msgpack/adaptor/check_container_size.hpp" +#include #include -#include - namespace msgpack { /// @cond @@ -30,11 +27,118 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { namespace adaptor { +namespace detail { +template < + typename Target, + typename Source, + bool target_is_signed = std::is_signed::value, + bool source_is_signed = std::is_signed::value, + typename = typename std::enable_if< + std::is_integral::value && + std::is_integral::value + >::type +> +struct would_underflow { + // The default case includes the cases that Source being unsigned, and since Source + // is unsigned, no underflow can happen + would_underflow(Source) : value{false} {} + bool value; +}; + +template +struct would_underflow { + // When Source is signed and Target is unsigned, we only need to compare with 0 to + // detect underflow, this works correctly and also avoids warnings from the compiler + would_underflow(Source source) : value{source < 0} {} + bool value; +}; +template +struct would_underflow { + // When Source and Target are signed, the promotion rules apply sensibly so we do + // not need to do anything + would_underflow(Source source) + : value{source < std::numeric_limits::min()} {} + bool value; +}; + +template < + typename Target, + typename Source, + bool target_is_signed = std::is_signed::value, + bool source_is_signed = std::is_signed::value, + typename = typename std::enable_if< + std::is_integral::value && + std::is_integral::value + >::type +> +struct would_overflow { + // The default case is Source and Target having the same signedness, the promotion + // rule also apply sensibly here so nothing special needs to be done + would_overflow(Source source) + : value{source > std::numeric_limits::max()} {} + bool value; +}; +template +struct would_overflow { + // When Target is unsigned and Source is signed, we cannot rely on the promotion + // rule. + would_overflow(Source source) + : value{ + sizeof(Target) >= sizeof(Source) + // Given Source is signed, Target being unsigned and having at least the + // same size makes impossible to overflow + ? false + // Source being larger than Target makes it safe to cast the maximum value + // of Target to Source + : source > static_cast(std::numeric_limits::max()) + } {} + bool value; +}; +template +struct would_overflow { + // When Target is signed and Source is unsigned, we cannot rely on the promotion + // rule. + would_overflow(Source source) + : value{ + sizeof(Target) > sizeof(Source) + // Target being larger than Source makes it impossible to overflow + ? false + // Source being unsigned and having at least the size of Target makes it + // safe to cast the maximum value of Target to Source + : source > static_cast(std::numeric_limits::max()) + } {} + bool value; +}; + +template < + typename Target, + typename Source, + typename = typename std::enable_if< + std::is_integral::value && + std::is_integral::value + >::type +> +Target integral_cast(Source source) { + if (would_underflow(source).value) { + THROW std::underflow_error{ + "casting from Source to Target causes an underflow error" + }; + } + if(would_overflow(source).value) { + THROW std::overflow_error{ + "casting from Source to Target causes an overflow error" + }; + } + + return static_cast(source); +} +} // namespace detail + template struct as> { typename std::chrono::time_point operator()(msgpack::object const& o) const { - if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); } - if(o.via.ext.type() != -1) { throw msgpack::type_error(); } + if(o.type != msgpack::type::EXT) { THROW msgpack::type_error(); } + if(o.via.ext.type() != -1) { THROW msgpack::type_error(); } std::chrono::time_point tp; switch(o.via.ext.size) { case 4: { @@ -45,7 +149,7 @@ struct as> { case 8: { uint64_t value; _msgpack_load64(uint64_t, o.via.ext.data(), &value); - uint32_t nanosec = boost::numeric_cast(value >> 34); + uint32_t nanosec = detail::integral_cast(value >> 34); uint64_t sec = value & 0x00000003ffffffffLL; tp += std::chrono::duration_cast( std::chrono::nanoseconds(nanosec)); @@ -69,14 +173,14 @@ struct as> { else { ++sec; tp += std::chrono::seconds(sec); - int64_t ns = boost::numeric_cast(nanosec) - 1000000000L; + int64_t ns = detail::integral_cast(nanosec) - 1000000000L; tp += std::chrono::duration_cast( std::chrono::nanoseconds(ns)); } } } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); } return tp; } @@ -85,8 +189,8 @@ struct as> { template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::chrono::time_point& v) const { - if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); } - if(o.via.ext.type() != -1) { throw msgpack::type_error(); } + if(o.type != msgpack::type::EXT) { THROW msgpack::type_error(); } + if(o.via.ext.type() != -1) { THROW msgpack::type_error(); } std::chrono::time_point tp; switch(o.via.ext.size) { case 4: { @@ -98,7 +202,7 @@ struct convert> { case 8: { uint64_t value; _msgpack_load64(uint64_t, o.via.ext.data(), &value); - uint32_t nanosec = boost::numeric_cast(value >> 34); + uint32_t nanosec = detail::integral_cast(value >> 34); uint64_t sec = value & 0x00000003ffffffffLL; tp += std::chrono::duration_cast( std::chrono::nanoseconds(nanosec)); @@ -123,7 +227,7 @@ struct convert> { else { ++sec; tp += std::chrono::seconds(sec); - int64_t ns = boost::numeric_cast(nanosec) - 1000000000L; + int64_t ns = detail::integral_cast(nanosec) - 1000000000L; tp += std::chrono::duration_cast( std::chrono::nanoseconds(ns)); } @@ -132,7 +236,7 @@ struct convert> { v = tp; } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); } return o; } @@ -142,7 +246,7 @@ template struct pack> { template msgpack::packer& operator()(msgpack::packer& o, std::chrono::time_point const& v) const { - int64_t count = boost::numeric_cast(v.time_since_epoch().count()); + int64_t count = detail::integral_cast(v.time_since_epoch().count()); int64_t nano_num = Duration::period::ratio::num * (1000000000L / Duration::period::ratio::den); @@ -158,11 +262,11 @@ struct pack> { / Duration::period::ratio::den; if ((sec >> 34) == 0) { - uint64_t data64 = (boost::numeric_cast(nanosec) << 34) | boost::numeric_cast(sec); + uint64_t data64 = (detail::integral_cast(nanosec) << 34) | detail::integral_cast(sec); if ((data64 & 0xffffffff00000000L) == 0) { // timestamp 32 o.pack_ext(4, -1); - uint32_t data32 = boost::numeric_cast(data64); + uint32_t data32 = detail::integral_cast(data64); char buf[4]; _msgpack_store32(buf, data32); o.pack_ext_body(buf, 4); @@ -181,7 +285,7 @@ struct pack> { char buf[12]; - _msgpack_store32(&buf[0], boost::numeric_cast(nanosec)); + _msgpack_store32(&buf[0], detail::integral_cast(nanosec)); _msgpack_store64(&buf[4], sec); o.pack_ext_body(buf, 12); } @@ -192,7 +296,7 @@ struct pack> { template struct object_with_zone> { void operator()(msgpack::object::with_zone& o, const std::chrono::time_point& v) const { - int64_t count = boost::numeric_cast(v.time_since_epoch().count()); + int64_t count = detail::integral_cast(v.time_since_epoch().count()); int64_t nano_num = Duration::period::ratio::num * @@ -208,14 +312,14 @@ struct object_with_zone> { * Duration::period::ratio::num / Duration::period::ratio::den; if ((sec >> 34) == 0) { - uint64_t data64 = (boost::numeric_cast(nanosec) << 34) | boost::numeric_cast(sec); + uint64_t data64 = (detail::integral_cast(nanosec) << 34) | detail::integral_cast(sec); if ((data64 & 0xffffffff00000000L) == 0) { // timestamp 32 o.type = msgpack::type::EXT; o.via.ext.size = 4; char* p = static_cast(o.zone.allocate_no_align(o.via.ext.size + 1)); p[0] = static_cast(-1); - uint32_t data32 = boost::numeric_cast(data64); + uint32_t data32 = detail::integral_cast(data64); _msgpack_store32(&p[1], data32); o.via.ext.ptr = p; } @@ -235,7 +339,7 @@ struct object_with_zone> { o.via.ext.size = 12; char* p = static_cast(o.zone.allocate_no_align(o.via.ext.size + 1)); p[0] = static_cast(-1); - _msgpack_store32(&p[1], boost::numeric_cast(nanosec)); + _msgpack_store32(&p[1], detail::integral_cast(nanosec)); _msgpack_store64(&p[1 + 4], sec); o.via.ext.ptr = p; } @@ -250,6 +354,4 @@ struct object_with_zone> { } // namespace msgpack -#endif // !defined(MSGPACK_NO_BOOST) - #endif // MSGPACK_V1_TYPE_CPP11_CHRONO_HPP diff --git a/include/msgpack/v1/adaptor/cpp11/forward_list.hpp b/include/msgpack/v1/adaptor/cpp11/forward_list.hpp index c615411d..1503868e 100644 --- a/include/msgpack/v1/adaptor/cpp11/forward_list.hpp +++ b/include/msgpack/v1/adaptor/cpp11/forward_list.hpp @@ -29,7 +29,7 @@ namespace adaptor { template struct as, typename std::enable_if::value>::type> { std::forward_list operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } std::forward_list v; msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pend = o.via.array.ptr; @@ -44,7 +44,7 @@ template template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::forward_list& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } v.resize(o.via.array.size); msgpack::object* p = o.via.array.ptr; for (auto &e : v) { diff --git a/include/msgpack/v1/adaptor/cpp11/timespec.hpp b/include/msgpack/v1/adaptor/cpp11/timespec.hpp index c68294de..e8810c07 100644 --- a/include/msgpack/v1/adaptor/cpp11/timespec.hpp +++ b/include/msgpack/v1/adaptor/cpp11/timespec.hpp @@ -28,8 +28,8 @@ namespace adaptor { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, timespec& v) const { - if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); } - if(o.via.ext.type() != -1) { throw msgpack::type_error(); } + if(o.type != msgpack::type::EXT) { THROW msgpack::type_error(); } + if(o.via.ext.type() != -1) { THROW msgpack::type_error(); } switch(o.via.ext.size) { case 4: { uint32_t sec; @@ -52,7 +52,7 @@ struct convert { v.tv_nsec = static_cast(nanosec); } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); } return o; } diff --git a/include/msgpack/v1/adaptor/cpp11/tuple.hpp b/include/msgpack/v1/adaptor/cpp11/tuple.hpp index fd1fc8e9..0e1f6755 100644 --- a/include/msgpack/v1/adaptor/cpp11/tuple.hpp +++ b/include/msgpack/v1/adaptor/cpp11/tuple.hpp @@ -10,6 +10,7 @@ #ifndef MSGPACK_V1_TYPE_CPP11_TUPLE_HPP #define MSGPACK_V1_TYPE_CPP11_TUPLE_HPP +#include "msgpack/v3/unpack_decl.hpp" #include "msgpack/versioning.hpp" #include "msgpack/adaptor/adaptor_base.hpp" #include "msgpack/object.hpp" @@ -113,7 +114,12 @@ template struct as, typename std::enable_if::value>::type> { std::tuple operator()( msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + // + if (o.via.array.size != sizeof...(Args)) { + THROW msgpack::unpack_error(std::string("tuple array not right size, got ") + std::to_string(o.via.array.size) + " but expected " + std::to_string(sizeof...(Args))); + } + // return StdTupleAs::as(o); } }; @@ -123,7 +129,7 @@ struct convert> { msgpack::object const& operator()( msgpack::object const& o, std::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } StdTupleConverter::convert(o, v); return o; } diff --git a/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp b/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp index 55183834..2e641222 100644 --- a/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp +++ b/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp @@ -30,7 +30,7 @@ struct as< std::unordered_map, typename std::enable_if::value || msgpack::has_as::value>::type> { std::unordered_map operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::unordered_map v; @@ -44,7 +44,7 @@ struct as< template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::unordered_map& v) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::unordered_map tmp; @@ -103,7 +103,7 @@ struct as< std::unordered_multimap, typename std::enable_if::value || msgpack::has_as::value>::type> { std::unordered_multimap operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::unordered_multimap v; @@ -117,7 +117,7 @@ struct as< template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::unordered_multimap& v) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::unordered_multimap tmp; diff --git a/include/msgpack/v1/adaptor/cpp11/unordered_set.hpp b/include/msgpack/v1/adaptor/cpp11/unordered_set.hpp index a8e9515a..9d14ed0c 100644 --- a/include/msgpack/v1/adaptor/cpp11/unordered_set.hpp +++ b/include/msgpack/v1/adaptor/cpp11/unordered_set.hpp @@ -28,7 +28,7 @@ namespace adaptor { template struct as, typename std::enable_if::value>::type> { std::unordered_set operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::unordered_set v; @@ -43,7 +43,7 @@ struct as, typename std::enable_if template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::unordered_set& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::unordered_set tmp; @@ -97,7 +97,7 @@ struct object_with_zone> { template struct as, typename std::enable_if::value>::type> { std::unordered_multiset operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::unordered_multiset v; @@ -112,7 +112,7 @@ struct as, typename std::enab template struct convert> { msgpack::object const& operator()(msgpack::object const& o, std::unordered_multiset& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::unordered_multiset tmp; diff --git a/include/msgpack/v1/adaptor/cpp17/array_byte.hpp b/include/msgpack/v1/adaptor/cpp17/array_byte.hpp index 783d37a9..8d6a557b 100644 --- a/include/msgpack/v1/adaptor/cpp17/array_byte.hpp +++ b/include/msgpack/v1/adaptor/cpp17/array_byte.hpp @@ -37,7 +37,7 @@ struct convert > { switch (o.type) { case msgpack::type::BIN: if (o.via.bin.size != N) - throw msgpack::type_error(); + THROW msgpack::type_error(); if (N != 0) { #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) #pragma GCC diagnostic push @@ -51,7 +51,7 @@ struct convert > { break; case msgpack::type::STR: if (o.via.bin.size != N) - throw msgpack::type_error(); + THROW msgpack::type_error(); if (N != 0) { #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) #pragma GCC diagnostic push @@ -64,7 +64,7 @@ struct convert > { } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/cpp17/carray_byte.hpp b/include/msgpack/v1/adaptor/cpp17/carray_byte.hpp index df4bbdc8..4f346eeb 100644 --- a/include/msgpack/v1/adaptor/cpp17/carray_byte.hpp +++ b/include/msgpack/v1/adaptor/cpp17/carray_byte.hpp @@ -35,16 +35,16 @@ struct convert { msgpack::object const& operator()(msgpack::object const& o, std::byte(&v)[N]) const { switch (o.type) { case msgpack::type::BIN: - if (o.via.bin.size > N) { throw msgpack::type_error(); } + if (o.via.bin.size > N) { THROW msgpack::type_error(); } std::memcpy(v, o.via.bin.ptr, o.via.bin.size); break; case msgpack::type::STR: - if (o.via.str.size > N) { throw msgpack::type_error(); } + if (o.via.str.size > N) { THROW msgpack::type_error(); } std::memcpy(v, o.via.str.ptr, o.via.str.size); if (o.via.str.size < N) v[o.via.str.size] = std::byte{'\0'}; break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/cpp17/string_view.hpp b/include/msgpack/v1/adaptor/cpp17/string_view.hpp index 060c259e..79852598 100644 --- a/include/msgpack/v1/adaptor/cpp17/string_view.hpp +++ b/include/msgpack/v1/adaptor/cpp17/string_view.hpp @@ -40,7 +40,7 @@ struct convert { v = std::string_view(o.via.str.ptr, o.via.str.size); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/cpp17/variant.hpp b/include/msgpack/v1/adaptor/cpp17/variant.hpp new file mode 100644 index 00000000..7c1b595e --- /dev/null +++ b/include/msgpack/v1/adaptor/cpp17/variant.hpp @@ -0,0 +1,151 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2023 Uy Ha +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef MSGPACK_V1_TYPE_VARIANT_HPP +#define MSGPACK_V1_TYPE_VARIANT_HPP + +#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) + +#include "msgpack/cpp_version.hpp" + +#if MSGPACK_CPP_VERSION >= 201703 + +#include "msgpack/adaptor/adaptor_base.hpp" +#include "msgpack/object.hpp" +#include "msgpack/versioning.hpp" + +#include + +namespace msgpack { +MSGPACK_API_VERSION_NAMESPACE(v1) { +namespace adaptor { +namespace detail { +template < + typename Variant, + typename T, + typename... Ts, + std::size_t current_index, + std::size_t... indices +> +Variant construct_variant( + std::size_t index, + msgpack::object& object, + std::index_sequence +) { + if constexpr(sizeof...(Ts) == 0) { + return object.as(); + } + else { + if (index == current_index) { + return object.as(); + } + return construct_variant( + index, + object, + std::index_sequence() + ); + } +} + +struct object_variant_overload { + object_variant_overload(msgpack::object& obj, msgpack::zone& zone) + : obj{obj} + , zone{zone} {} + + template + void operator()(T const& value) { + obj = msgpack::object(value, zone); + } + + msgpack::object& obj; + msgpack::zone& zone; +}; +} // namespace detail + +template +struct as, typename std::enable_if<(msgpack::has_as::value && ...)>::type> { + std::variant operator()(msgpack::object const& o) const { + if ( o.type != msgpack::type::ARRAY + || o.via.array.size != 2 + || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER + || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { + THROW msgpack::type_error{}; + } + + return detail::construct_variant, Ts...>( + o.via.array.ptr[0].as(), + o.via.array.ptr[1], + std::make_index_sequence() + ); + } +}; + +template +struct convert> { + msgpack::object const& operator()(msgpack::object const& o, std::variant& v) const { + if ( o.type != msgpack::type::ARRAY + || o.via.array.size != 2 + || o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER + || o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { + THROW msgpack::type_error{}; + } + + v = detail::construct_variant, Ts...>( + o.via.array.ptr[0].as(), + o.via.array.ptr[1], + std::make_index_sequence() + ); + return o; + } +}; + +template +struct pack>{ + template + msgpack::packer& operator()( + msgpack::packer& o, + std::variant const& v + ) const { + o.pack_array(2); + o.pack_uint64(v.index()); + std::visit([&o](auto const& value){o.pack(value);}, v); + return o; + } +}; + + +template +struct object_with_zone> { + void operator()( + msgpack::object::with_zone& o, + std::variant const& v + ) const { + msgpack::object *p = + static_cast( + o.zone.allocate_align( + sizeof(msgpack::object) * 2, + MSGPACK_ZONE_ALIGNOF(msgpack::object) + ) + ); + + o.type = msgpack::type::ARRAY; + o.via.array.size = 2; + o.via.array.ptr = p; + o.via.array.ptr[0]= msgpack::object(v.index(), o.zone); + std::visit(detail::object_variant_overload(o.via.array.ptr[1], o.zone), v); + } +}; +} // namespace adaptor +} +} // namespace msgpack + +#endif // MSGPACK_CPP_VERSION >= 201703 +#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) +#endif // MSGPACK_V1_TYPE_VARIANT_HPP diff --git a/include/msgpack/v1/adaptor/cpp17/vector_byte.hpp b/include/msgpack/v1/adaptor/cpp17/vector_byte.hpp index c486f7da..3420e9ed 100644 --- a/include/msgpack/v1/adaptor/cpp17/vector_byte.hpp +++ b/include/msgpack/v1/adaptor/cpp17/vector_byte.hpp @@ -62,7 +62,7 @@ struct convert > { } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/cpp20/span.hpp b/include/msgpack/v1/adaptor/cpp20/span.hpp index cd1cf1d5..35893a7d 100644 --- a/include/msgpack/v1/adaptor/cpp20/span.hpp +++ b/include/msgpack/v1/adaptor/cpp20/span.hpp @@ -43,7 +43,7 @@ namespace adaptor { v = std::span(reinterpret_cast(o.via.bin.ptr), o.via.bin.size); \ break; \ default: \ - throw msgpack::type_error(); \ + THROW msgpack::type_error(); \ break; \ } \ return o; \ diff --git a/include/msgpack/v1/adaptor/deque.hpp b/include/msgpack/v1/adaptor/deque.hpp index f4fe5e20..8cd8f3af 100644 --- a/include/msgpack/v1/adaptor/deque.hpp +++ b/include/msgpack/v1/adaptor/deque.hpp @@ -30,7 +30,7 @@ namespace adaptor { template struct as, typename std::enable_if::value>::type> { std::deque operator()(const msgpack::object& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } std::deque v; if (o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; @@ -49,7 +49,7 @@ struct as, typename std::enable_if::valu template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::deque& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } v.resize(o.via.array.size); msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; diff --git a/include/msgpack/v1/adaptor/detail/cpp03_define_array.hpp b/include/msgpack/v1/adaptor/detail/cpp03_define_array.hpp index c8158292..0bb3125d 100644 --- a/include/msgpack/v1/adaptor/detail/cpp03_define_array.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp03_define_array.hpp @@ -33,7 +33,7 @@ struct define_array<> { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } } void msgpack_object(msgpack::object* o, msgpack::zone&) const { @@ -60,7 +60,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -100,7 +100,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -146,7 +146,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -198,7 +198,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -256,7 +256,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -320,7 +320,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -390,7 +390,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -466,7 +466,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -548,7 +548,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -636,7 +636,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -730,7 +730,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -830,7 +830,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -936,7 +936,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1048,7 +1048,7 @@ struct define_array } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } const size_t size = o.via.array.size; if(size > 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1166,7 +1166,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1290,7 +1290,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1420,7 +1420,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1556,7 +1556,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1698,7 +1698,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -1846,7 +1846,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -2000,7 +2000,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -2160,7 +2160,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -2326,7 +2326,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -2498,7 +2498,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -2676,7 +2676,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -2860,7 +2860,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -3050,7 +3050,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -3246,7 +3246,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -3448,7 +3448,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -3656,7 +3656,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -3870,7 +3870,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; @@ -4090,7 +4090,7 @@ struct define_array 0) { msgpack::object *ptr = o.via.array.ptr; diff --git a/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp b/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp index 7869b98e..5349be9c 100644 --- a/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp @@ -33,7 +33,7 @@ struct define_map<> { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } } void msgpack_object(msgpack::object* o, msgpack::zone&) const { @@ -59,10 +59,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -112,10 +112,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -179,10 +179,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -260,10 +260,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -355,10 +355,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -464,10 +464,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -587,10 +587,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -724,10 +724,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -875,10 +875,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1040,10 +1040,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1219,10 +1219,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1412,10 +1412,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1619,10 +1619,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1840,10 +1840,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -2075,10 +2075,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -2324,10 +2324,10 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( diff --git a/include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp b/include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp index 7a980c4b..08d46b87 100644 --- a/include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp @@ -10615,7 +10615,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple<>&) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } return o; } }; @@ -10627,7 +10627,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10641,7 +10641,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10658,7 +10658,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10678,7 +10678,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10701,7 +10701,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10727,7 +10727,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10756,7 +10756,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10788,7 +10788,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10823,7 +10823,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10861,7 +10861,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10902,7 +10902,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10946,7 +10946,7 @@ struct convert > { msgpack::object const& operator()( msgpack::object const& o, type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -10993,7 +10993,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11043,7 +11043,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11096,7 +11096,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11152,7 +11152,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11211,7 +11211,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11273,7 +11273,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11338,7 +11338,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11406,7 +11406,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11477,7 +11477,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11551,7 +11551,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11628,7 +11628,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11708,7 +11708,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11791,7 +11791,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11877,7 +11877,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -11966,7 +11966,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -12058,7 +12058,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -12153,7 +12153,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -12251,7 +12251,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -12352,7 +12352,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) @@ -12456,7 +12456,7 @@ struct convert& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } // In order to avoid clang++'s invalid warning, msgpack::object:: has been added. if(o.via.array.size > 0) diff --git a/include/msgpack/v1/adaptor/detail/cpp11_define_array.hpp b/include/msgpack/v1/adaptor/detail/cpp11_define_array.hpp index bec4a053..297ff609 100644 --- a/include/msgpack/v1/adaptor/detail/cpp11_define_array.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp11_define_array.hpp @@ -71,7 +71,7 @@ struct define_array { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } define_array_imp, sizeof...(Args)>::unpack(o, a); } @@ -98,7 +98,7 @@ struct define_array<> { } void msgpack_unpack(msgpack::object const& o) { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } } void msgpack_object(msgpack::object* o, msgpack::zone&) const { diff --git a/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp b/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp index d1ab7015..d1989624 100644 --- a/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp @@ -12,6 +12,7 @@ #include "msgpack/v1/adaptor/detail/cpp11_define_map_decl.hpp" #include "msgpack/v1/adaptor/detail/cpp11_convert_helper.hpp" +#include "msgpack/v3/object_fwd_decl.hpp" #include #include @@ -36,6 +37,10 @@ struct define_map_imp { auto it = kvmap.find(std::get(t)); if (it != kvmap.end()) { convert_helper(*it->second, std::get(t)); + } else { + // + THROW msgpack::unpack_error(std::string("Missing field ") + std::get(t)); + // } } static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) { @@ -69,10 +74,10 @@ struct define_map { } void msgpack_unpack(msgpack::object const& o) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { - if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { THROW msgpack::type_error(); } kvmap.emplace( std::string( o.via.map.ptr[i].key.via.str.ptr, diff --git a/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp b/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp index 88d16291..4fc9e363 100644 --- a/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp @@ -149,7 +149,7 @@ template struct as, typename std::enable_if::value>::type> { msgpack::type::tuple operator()( msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } return MsgpackTupleAs::as(o); } }; @@ -159,7 +159,7 @@ struct convert> { msgpack::object const& operator()( msgpack::object const& o, msgpack::type::tuple& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } MsgpackTupleConverter::convert(o, v); return o; } diff --git a/include/msgpack/v1/adaptor/ext.hpp b/include/msgpack/v1/adaptor/ext.hpp index 11fed72f..5dc01b37 100644 --- a/include/msgpack/v1/adaptor/ext.hpp +++ b/include/msgpack/v1/adaptor/ext.hpp @@ -78,7 +78,7 @@ template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, msgpack::type::ext& v) const { if(o.type != msgpack::type::EXT) { - throw msgpack::type_error(); + THROW msgpack::type_error(); } v = msgpack::type::ext(o.via.ext.type(), o.via.ext.data(), o.via.ext.size); return o; @@ -189,7 +189,7 @@ namespace adaptor { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, msgpack::type::ext_ref& v) const { - if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); } + if(o.type != msgpack::type::EXT) { THROW msgpack::type_error(); } v = msgpack::type::ext_ref(o.via.ext.ptr, o.via.ext.size + 1); return o; } diff --git a/include/msgpack/v1/adaptor/float.hpp b/include/msgpack/v1/adaptor/float.hpp index ae075fc6..7742661d 100644 --- a/include/msgpack/v1/adaptor/float.hpp +++ b/include/msgpack/v1/adaptor/float.hpp @@ -37,7 +37,7 @@ struct convert { v = static_cast(o.via.i64); } else { - throw msgpack::type_error(); + THROW msgpack::type_error(); } return o; } @@ -66,7 +66,7 @@ struct convert { v = static_cast(o.via.i64); } else { - throw msgpack::type_error(); + THROW msgpack::type_error(); } return o; } diff --git a/include/msgpack/v1/adaptor/int.hpp b/include/msgpack/v1/adaptor/int.hpp index 11e73b0a..9ed3df4f 100644 --- a/include/msgpack/v1/adaptor/int.hpp +++ b/include/msgpack/v1/adaptor/int.hpp @@ -29,14 +29,14 @@ struct convert_integer_sign { static T convert(msgpack::object const& o) { if(o.type == msgpack::type::POSITIVE_INTEGER) { if(o.via.u64 > static_cast(std::numeric_limits::max())) - { throw msgpack::type_error(); } + { THROW msgpack::type_error(); } return static_cast(o.via.u64); } else if(o.type == msgpack::type::NEGATIVE_INTEGER) { if(o.via.i64 < static_cast(std::numeric_limits::min())) - { throw msgpack::type_error(); } + { THROW msgpack::type_error(); } return static_cast(o.via.i64); } - throw msgpack::type_error(); + THROW msgpack::type_error(); } }; @@ -45,10 +45,10 @@ struct convert_integer_sign { static T convert(msgpack::object const& o) { if(o.type == msgpack::type::POSITIVE_INTEGER) { if(o.via.u64 > static_cast(std::numeric_limits::max())) - { throw msgpack::type_error(); } + { THROW msgpack::type_error(); } return static_cast(o.via.u64); } - throw msgpack::type_error(); + THROW msgpack::type_error(); } }; diff --git a/include/msgpack/v1/adaptor/list.hpp b/include/msgpack/v1/adaptor/list.hpp index 73051af4..4e9fb0e3 100644 --- a/include/msgpack/v1/adaptor/list.hpp +++ b/include/msgpack/v1/adaptor/list.hpp @@ -30,7 +30,7 @@ namespace adaptor { template struct as, typename std::enable_if::value>::type> { std::list operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } std::list v; msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; @@ -46,7 +46,7 @@ struct as, typename std::enable_if::value template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::list& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } v.resize(o.via.array.size); msgpack::object* p = o.via.array.ptr; msgpack::object* const pend = o.via.array.ptr + o.via.array.size; diff --git a/include/msgpack/v1/adaptor/map.hpp b/include/msgpack/v1/adaptor/map.hpp index 3662a486..5a113c5a 100644 --- a/include/msgpack/v1/adaptor/map.hpp +++ b/include/msgpack/v1/adaptor/map.hpp @@ -52,7 +52,7 @@ struct as< type::assoc_vector, typename std::enable_if::value || msgpack::has_as::value>::type> { type::assoc_vector operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } type::assoc_vector v; v.reserve(o.via.map.size); msgpack::object_kv* p = o.via.map.ptr; @@ -70,7 +70,7 @@ struct as< template struct convert > { msgpack::object const& operator()(msgpack::object const& o, type::assoc_vector& v) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } v.resize(o.via.map.size); if (o.via.map.size != 0) { msgpack::object_kv* p = o.via.map.ptr; @@ -133,7 +133,7 @@ struct as< std::map, typename std::enable_if::value || msgpack::has_as::value>::type> { std::map operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::map v; @@ -149,7 +149,7 @@ struct as< template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::map& v) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::map tmp; @@ -226,7 +226,7 @@ struct as< std::multimap, typename std::enable_if::value || msgpack::has_as::value>::type> { std::multimap operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::multimap v; @@ -242,7 +242,7 @@ struct as< template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::multimap& v) const { - if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if (o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); std::multimap tmp; diff --git a/include/msgpack/v1/adaptor/nil.hpp b/include/msgpack/v1/adaptor/nil.hpp index f65972ce..263f4f83 100644 --- a/include/msgpack/v1/adaptor/nil.hpp +++ b/include/msgpack/v1/adaptor/nil.hpp @@ -37,7 +37,7 @@ namespace adaptor { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, type::nil_t&) const { - if(o.type != msgpack::type::NIL) { throw msgpack::type_error(); } + if(o.type != msgpack::type::NIL) { THROW msgpack::type_error(); } return o; } }; diff --git a/include/msgpack/v1/adaptor/pair.hpp b/include/msgpack/v1/adaptor/pair.hpp index 422454ac..61555112 100644 --- a/include/msgpack/v1/adaptor/pair.hpp +++ b/include/msgpack/v1/adaptor/pair.hpp @@ -31,8 +31,8 @@ template struct as, typename std::enable_if::value>::type> { std::pair operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if (o.via.array.size != 2) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if (o.via.array.size != 2) { THROW msgpack::type_error(); } return std::make_pair(o.via.array.ptr[0].as(), o.via.array.ptr[1].as()); } }; @@ -42,8 +42,8 @@ struct as, template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::pair& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } - if(o.via.array.size != 2) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } + if(o.via.array.size != 2) { THROW msgpack::type_error(); } o.via.array.ptr[0].convert(v.first); o.via.array.ptr[1].convert(v.second); return o; diff --git a/include/msgpack/v1/adaptor/raw.hpp b/include/msgpack/v1/adaptor/raw.hpp index e2f4cf42..3bb3fe6a 100644 --- a/include/msgpack/v1/adaptor/raw.hpp +++ b/include/msgpack/v1/adaptor/raw.hpp @@ -62,7 +62,7 @@ namespace adaptor { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, type::raw_ref& v) const { - if(o.type != msgpack::type::BIN) { throw msgpack::type_error(); } + if(o.type != msgpack::type::BIN) { THROW msgpack::type_error(); } v.ptr = o.via.bin.ptr; v.size = o.via.bin.size; return o; diff --git a/include/msgpack/v1/adaptor/set.hpp b/include/msgpack/v1/adaptor/set.hpp index 3c280c5c..e7b74576 100644 --- a/include/msgpack/v1/adaptor/set.hpp +++ b/include/msgpack/v1/adaptor/set.hpp @@ -30,7 +30,7 @@ namespace adaptor { template struct as, typename std::enable_if::value>::type> { std::set operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::set v; @@ -47,7 +47,7 @@ struct as, typename std::enable_if struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::set& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::set tmp; @@ -107,7 +107,7 @@ struct object_with_zone > { template struct as, typename std::enable_if::value>::type> { std::multiset operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::multiset v; @@ -124,7 +124,7 @@ struct as, typename std::enable_if struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::multiset& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; std::multiset tmp; diff --git a/include/msgpack/v1/adaptor/size_equal_only.hpp b/include/msgpack/v1/adaptor/size_equal_only.hpp index 39f6f343..d38f83e9 100644 --- a/include/msgpack/v1/adaptor/size_equal_only.hpp +++ b/include/msgpack/v1/adaptor/size_equal_only.hpp @@ -71,13 +71,13 @@ struct convert > { msgpack::object const& operator()(msgpack::object const& o, type::size_equal_only& v) const { switch(o.type) { case msgpack::type::ARRAY: - if (o.via.array.size != msgpack::type::size(v.m_t)) throw msgpack::type_error(); + if (o.via.array.size != msgpack::type::size(v.m_t)) THROW msgpack::type_error(); break; case msgpack::type::MAP: - if (o.via.map.size != msgpack::type::size(v.m_t)) throw msgpack::type_error(); + if (o.via.map.size != msgpack::type::size(v.m_t)) THROW msgpack::type_error(); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); } o >> v.m_t; return o; diff --git a/include/msgpack/v1/adaptor/string.hpp b/include/msgpack/v1/adaptor/string.hpp index 6e5c91d9..ccca1952 100644 --- a/include/msgpack/v1/adaptor/string.hpp +++ b/include/msgpack/v1/adaptor/string.hpp @@ -37,7 +37,7 @@ struct convert { v.assign(o.via.str.ptr, o.via.str.size); break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/tr1/unordered_map.hpp b/include/msgpack/v1/adaptor/tr1/unordered_map.hpp index b24ac231..468220f9 100644 --- a/include/msgpack/v1/adaptor/tr1/unordered_map.hpp +++ b/include/msgpack/v1/adaptor/tr1/unordered_map.hpp @@ -47,7 +47,7 @@ namespace adaptor { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_map& v) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); MSGPACK_STD_TR1::unordered_map tmp; @@ -103,7 +103,7 @@ struct object_with_zone template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_multimap& v) const { - if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } + if(o.type != msgpack::type::MAP) { THROW msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size); MSGPACK_STD_TR1::unordered_multimap tmp; diff --git a/include/msgpack/v1/adaptor/tr1/unordered_set.hpp b/include/msgpack/v1/adaptor/tr1/unordered_set.hpp index 9c92c210..339815a2 100644 --- a/include/msgpack/v1/adaptor/tr1/unordered_set.hpp +++ b/include/msgpack/v1/adaptor/tr1/unordered_set.hpp @@ -47,7 +47,7 @@ namespace adaptor { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_set& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; MSGPACK_STD_TR1::unordered_set tmp; @@ -101,7 +101,7 @@ struct object_with_zone template struct convert > { msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_multiset& v) const { - if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if(o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } msgpack::object* p = o.via.array.ptr + o.via.array.size; msgpack::object* const pbegin = o.via.array.ptr; MSGPACK_STD_TR1::unordered_multiset tmp; diff --git a/include/msgpack/v1/adaptor/v4raw.hpp b/include/msgpack/v1/adaptor/v4raw.hpp index ae660c61..0011179f 100644 --- a/include/msgpack/v1/adaptor/v4raw.hpp +++ b/include/msgpack/v1/adaptor/v4raw.hpp @@ -61,7 +61,7 @@ namespace adaptor { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, type::v4raw_ref& v) const { - if(o.type != msgpack::type::STR) { throw msgpack::type_error(); } + if(o.type != msgpack::type::STR) { THROW msgpack::type_error(); } v.ptr = o.via.str.ptr; v.size = o.via.str.size; return o; diff --git a/include/msgpack/v1/adaptor/vector.hpp b/include/msgpack/v1/adaptor/vector.hpp index 349def4a..b4b494d0 100644 --- a/include/msgpack/v1/adaptor/vector.hpp +++ b/include/msgpack/v1/adaptor/vector.hpp @@ -30,7 +30,7 @@ namespace adaptor { template struct as, typename std::enable_if::value>::type> { std::vector operator()(const msgpack::object& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } std::vector v; v.reserve(o.via.array.size); if (o.via.array.size > 0) { @@ -50,7 +50,7 @@ struct as, typename std::enable_if::val template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::vector& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } v.resize(o.via.array.size); if (o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; diff --git a/include/msgpack/v1/adaptor/vector_bool.hpp b/include/msgpack/v1/adaptor/vector_bool.hpp index d6ec2459..de51aa39 100644 --- a/include/msgpack/v1/adaptor/vector_bool.hpp +++ b/include/msgpack/v1/adaptor/vector_bool.hpp @@ -27,7 +27,7 @@ namespace adaptor { template struct convert > { msgpack::object const& operator()(msgpack::object const& o, std::vector& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } if (o.via.array.size > 0) { v.resize(o.via.array.size); msgpack::object* p = o.via.array.ptr; diff --git a/include/msgpack/v1/adaptor/vector_char.hpp b/include/msgpack/v1/adaptor/vector_char.hpp index fa958c87..38ce0a97 100644 --- a/include/msgpack/v1/adaptor/vector_char.hpp +++ b/include/msgpack/v1/adaptor/vector_char.hpp @@ -57,7 +57,7 @@ struct convert > { } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/vector_unsigned_char.hpp b/include/msgpack/v1/adaptor/vector_unsigned_char.hpp index f4611a79..83c8fbc0 100644 --- a/include/msgpack/v1/adaptor/vector_unsigned_char.hpp +++ b/include/msgpack/v1/adaptor/vector_unsigned_char.hpp @@ -57,7 +57,7 @@ struct convert > { } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } return o; diff --git a/include/msgpack/v1/adaptor/wstring.hpp b/include/msgpack/v1/adaptor/wstring.hpp index e57f6752..2fe44426 100644 --- a/include/msgpack/v1/adaptor/wstring.hpp +++ b/include/msgpack/v1/adaptor/wstring.hpp @@ -30,7 +30,7 @@ namespace adaptor { template <> struct as { std::wstring operator()(const msgpack::object& o) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } std::wstring v; v.reserve(o.via.array.size); if (o.via.array.size > 0) { @@ -50,7 +50,7 @@ struct as { template <> struct convert { msgpack::object const& operator()(msgpack::object const& o, std::wstring& v) const { - if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } + if (o.type != msgpack::type::ARRAY) { THROW msgpack::type_error(); } v.resize(o.via.array.size); if (o.via.array.size > 0) { msgpack::object* p = o.via.array.ptr; diff --git a/include/msgpack/v1/detail/cpp03_zone.hpp b/include/msgpack/v1/detail/cpp03_zone.hpp index 62def989..45a75a12 100644 --- a/include/msgpack/v1/detail/cpp03_zone.hpp +++ b/include/msgpack/v1/detail/cpp03_zone.hpp @@ -29,121 +29,101 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { class zone { struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} + finalizer(void (*func)(void*), void* data, finalizer* next): m_func(func), m_data(data), m_next(next) {} void operator()() { m_func(m_data); } void (*m_func)(void*); void* m_data; + finalizer* m_next; }; + struct finalizer_array { - finalizer_array():m_tail(MSGPACK_NULLPTR), m_end(MSGPACK_NULLPTR), m_array(MSGPACK_NULLPTR) {} - void call() { - finalizer* fin = m_tail; - for(; fin != m_array; --fin) (*(fin-1))(); - } + finalizer_array(): m_head(MSGPACK_NULLPTR) {} + ~finalizer_array() { - call(); - ::free(m_array); - } - void clear() { - call(); - m_tail = m_array; + clear(); } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; - if(fin == m_end) { - push_expand(func, data); - return; + void clear() { + finalizer* fin = m_head; + finalizer* tmp = MSGPACK_NULLPTR; + while(fin) { + (*fin)(); + tmp = fin; + fin = fin->m_next; + delete tmp; } + m_head = MSGPACK_NULLPTR; + } - fin->m_func = func; - fin->m_data = data; - - ++m_tail; + void push(void (*func)(void* data), void* data) { + m_head = new finalizer(func, data, m_head); } - void push_expand(void (*func)(void*), void* data) { - const size_t nused = static_cast(m_end - m_array); - size_t nnext; - if(nused == 0) { - nnext = (sizeof(finalizer) < 72/2) ? - 72 / sizeof(finalizer) : 8; - } else { - nnext = nused * 2; - } - finalizer* tmp = - static_cast(::realloc(m_array, sizeof(finalizer) * nnext)); - if(!tmp) { - throw std::bad_alloc(); - } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - ++m_tail; + void pop() { + finalizer* n = m_head->m_next; + delete m_head; + m_head = n; } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; + + finalizer* m_head; + private: + finalizer_array(const finalizer_array&); + finalizer_array& operator=(const finalizer_array&); }; + struct chunk { chunk* m_next; }; - struct chunk_list { - chunk_list(size_t chunk_size) - { - chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); - if(!c) { - throw std::bad_alloc(); - } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { + struct chunk_list { + chunk_list(size_t chunk_size, char* ptr): m_free(chunk_size), m_ptr(ptr), m_head(MSGPACK_NULLPTR) {} + ~chunk_list() { chunk* c = m_head; while(c) { chunk* n = c->m_next; ::free(c); c = n; } + m_head = MSGPACK_NULLPTR; } - void clear(size_t chunk_size) - { + + void clear(size_t chunk_size, char* ptr) { chunk* c = m_head; - while(true) { + while(c) { chunk* n = c->m_next; - if(n) { - ::free(c); - c = n; - } else { - m_head = c; - break; - } + ::free(c); + c = n; } - m_head->m_next = MSGPACK_NULLPTR; + m_head = MSGPACK_NULLPTR; m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); + m_ptr = ptr; } + size_t m_free; char* m_ptr; chunk* m_head; + + private: + chunk_list(const chunk_list&); + chunk_list& operator=(const chunk_list&); }; + size_t m_chunk_size; - chunk_list m_chunk_list; + chunk_list* m_chunk_list; finalizer_array m_finalizer_array; public: zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); -public: void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); + void* allocate_no_align(size_t size); + bool allocated() { + return m_chunk_list != MSGPACK_NULLPTR; + } + void push_finalizer(void (*func)(void*), void* data); template @@ -152,24 +132,23 @@ class zone { void clear(); void swap(zone& o); - static void* operator new(std::size_t size) - { + + static void* operator new(std::size_t size) { void* p = ::malloc(size); - if (!p) throw std::bad_alloc(); + if (!p) THROW std::bad_alloc(); return p; } - static void operator delete(void *p) /* throw() */ - { + + static void operator delete(void *p) /* throw() */ { ::free(p); } - static void* operator new(std::size_t size, void* place) /* throw() */ - { - return ::operator new(size, place); - } - static void operator delete(void* p, void* place) /* throw() */ - { - ::operator delete(p, place); + + static void* operator new(std::size_t /*size*/, void* mem) /* throw() */ { + return mem; } + + static void operator delete(void * /*p*/, void* /*mem*/) /* throw() */ {} + /// @cond template @@ -233,18 +212,26 @@ class zone { static char* get_aligned(char* ptr, size_t align); + chunk_list& get_chank_lst(); + char* allocate_expand(size_t size); private: zone(const zone&); zone& operator=(const zone&); }; -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ +inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(MSGPACK_NULLPTR) {} + +inline zone::~zone() { + m_finalizer_array.~finalizer_array(); + if(m_chunk_list) { + m_chunk_list->~chunk_list(); + ::free(m_chunk_list); + m_chunk_list = MSGPACK_NULLPTR; + } } -inline char* zone::get_aligned(char* ptr, size_t align) -{ +inline char* zone::get_aligned(char* ptr, size_t align) { MSGPACK_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( @@ -252,37 +239,45 @@ inline char* zone::get_aligned(char* ptr, size_t align) ); } -inline void* zone::allocate_align(size_t size, size_t align) -{ - char* aligned = get_aligned(m_chunk_list.m_ptr, align); - size_t adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); - if (m_chunk_list.m_free < adjusted_size) { +inline zone::chunk_list& zone::get_chank_lst() { + if (!m_chunk_list) { + void* ptr = ::malloc(sizeof(chunk_list) + m_chunk_size); + if (!ptr) + THROW std::bad_alloc(); + m_chunk_list = new (ptr) chunk_list(m_chunk_size, reinterpret_cast(ptr) + sizeof(chunk_list)); + } + return *m_chunk_list; +} + +inline void* zone::allocate_align(size_t size, size_t align) { + chunk_list& chank_lst = get_chank_lst(); + char* aligned = get_aligned(chank_lst.m_ptr, align); + size_t adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); + if (chank_lst.m_free < adjusted_size) { size_t enough_size = size + align - 1; char* ptr = allocate_expand(enough_size); aligned = get_aligned(ptr, align); - adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); + adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; return aligned; } -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { +inline void* zone::allocate_no_align(size_t size) { + chunk_list& chank_lst = get_chank_lst(); + char* ptr = chank_lst.m_ptr; + if(chank_lst.m_free < size) { ptr = allocate_expand(size); } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + chank_lst.m_free -= size; + chank_lst.m_ptr += size; return ptr; } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; - +inline char* zone::allocate_expand(size_t size) { + chunk_list& cl = get_chank_lst(); size_t sz = m_chunk_size; while(sz < size) { @@ -295,64 +290,58 @@ inline char* zone::allocate_expand(size_t size) } chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); - if (!c) throw std::bad_alloc(); + if (!c) THROW std::bad_alloc(); char* ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; return ptr; } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ +inline void zone::push_finalizer(void (*func)(void*), void* data) { m_finalizer_array.push(func, data); } template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ +inline void zone::push_finalizer(msgpack::unique_ptr obj) { m_finalizer_array.push(&zone::object_delete, obj.release()); } -inline void zone::clear() -{ +inline void zone::clear() { m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -inline void zone::swap(zone& o) -{ +inline void zone::swap(zone& o) { using std::swap; swap(m_chunk_size, o.m_chunk_size); swap(m_chunk_list, o.m_chunk_list); - swap(m_finalizer_array, o.m_finalizer_array); + swap(m_finalizer_array.m_head, o.m_finalizer_array.m_head); } template -void zone::object_destruct(void* obj) -{ - static_cast(obj)->~T(); +void zone::object_delete(void* obj) { + delete static_cast(obj); } template -void zone::object_delete(void* obj) -{ - delete static_cast(obj); +void zone::object_destruct(void* obj) { + static_cast(obj)->~T(); } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; +inline void zone::undo_allocate(size_t size) { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { +inline std::size_t aligned_size(std::size_t size, std::size_t align) { return (size + align - 1) / align * align; } @@ -366,14 +355,14 @@ T* zone::allocate() m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -385,14 +374,14 @@ T* zone::allocate(A1 a1) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -404,14 +393,14 @@ T* zone::allocate(A1 a1, A2 a2) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -423,14 +412,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -442,14 +431,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -461,14 +450,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -480,14 +469,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -499,14 +488,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -518,14 +507,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -537,14 +526,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -556,14 +545,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -575,14 +564,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -594,14 +583,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -613,14 +602,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -632,14 +621,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } @@ -651,14 +640,14 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } diff --git a/include/msgpack/v1/detail/cpp11_zone.hpp b/include/msgpack/v1/detail/cpp11_zone.hpp index 2586f275..16469aa8 100644 --- a/include/msgpack/v1/detail/cpp11_zone.hpp +++ b/include/msgpack/v1/detail/cpp11_zone.hpp @@ -29,154 +29,113 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { class zone { private: struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} + finalizer(void (*func)(void*), void* data, finalizer* next): m_func(func), m_data(data), m_next(next) {} void operator()() { m_func(m_data); } void (*m_func)(void*); void* m_data; + finalizer* m_next; }; + struct finalizer_array { - finalizer_array():m_tail(MSGPACK_NULLPTR), m_end(MSGPACK_NULLPTR), m_array(MSGPACK_NULLPTR) {} - void call() { - finalizer* fin = m_tail; - for(; fin != m_array; --fin) (*(fin-1))(); - } + finalizer_array(): m_head(MSGPACK_NULLPTR) {} + ~finalizer_array() { - call(); - ::free(m_array); + clear(); } - void clear() { - call(); - m_tail = m_array; - } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; - if(fin == m_end) { - push_expand(func, data); - return; + void clear() { + finalizer* fin = m_head; + finalizer* tmp = MSGPACK_NULLPTR; + while(fin) { + (*fin)(); + tmp = fin; + fin = fin->m_next; + delete tmp; } + m_head = MSGPACK_NULLPTR; + } - fin->m_func = func; - fin->m_data = data; - - ++m_tail; + void push(void (*func)(void* data), void* data) { + m_head = new finalizer(func, data, m_head); } - void push_expand(void (*func)(void*), void* data) { - const size_t nused = static_cast(m_end - m_array); - size_t nnext; - if(nused == 0) { - nnext = (sizeof(finalizer) < 72/2) ? - 72 / sizeof(finalizer) : 8; - } else { - nnext = nused * 2; - } - finalizer* tmp = - static_cast(::realloc(m_array, sizeof(finalizer) * nnext)); - if(!tmp) { - throw std::bad_alloc(); - } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - ++m_tail; + void pop() { + auto n = m_head->m_next; + delete m_head; + m_head = n; } - finalizer_array(finalizer_array&& other) noexcept - :m_tail(other.m_tail), m_end(other.m_end), m_array(other.m_array) - { - other.m_tail = MSGPACK_NULLPTR; - other.m_end = MSGPACK_NULLPTR; - other.m_array = MSGPACK_NULLPTR; + + finalizer_array(finalizer_array&& other) noexcept: m_head(other.m_head) { + other.m_head = MSGPACK_NULLPTR; } - finalizer_array& operator=(finalizer_array&& other) noexcept - { - this->~finalizer_array(); - new (this) finalizer_array(std::move(other)); + + finalizer_array& operator=(finalizer_array&& other) noexcept { + m_head = other.m_head; + other.m_head = MSGPACK_NULLPTR; return *this; } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; - private: + finalizer* m_head; finalizer_array(const finalizer_array&); finalizer_array& operator=(const finalizer_array&); }; + struct chunk { chunk* m_next; }; - struct chunk_list { - chunk_list(size_t chunk_size) - { - chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); - if(!c) { - throw std::bad_alloc(); - } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { + struct chunk_list { + chunk_list(size_t chunk_size, char* ptr): m_free(chunk_size), m_ptr(ptr), m_head(MSGPACK_NULLPTR) {} + ~chunk_list() { chunk* c = m_head; while(c) { chunk* n = c->m_next; ::free(c); c = n; } + m_head = MSGPACK_NULLPTR; } - void clear(size_t chunk_size) - { + + void clear(size_t chunk_size, char* ptr) { chunk* c = m_head; - while(true) { + while(c) { chunk* n = c->m_next; - if(n) { - ::free(c); - c = n; - } else { - m_head = c; - break; - } + ::free(c); + c = n; } - m_head->m_next = MSGPACK_NULLPTR; + m_head = MSGPACK_NULLPTR; m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); - } - chunk_list(chunk_list&& other) noexcept - :m_free(other.m_free), m_ptr(other.m_ptr), m_head(other.m_head) - { - other.m_head = MSGPACK_NULLPTR; - } - chunk_list& operator=(chunk_list&& other) noexcept - { - this->~chunk_list(); - new (this) chunk_list(std::move(other)); - return *this; + m_ptr = ptr; } size_t m_free; char* m_ptr; chunk* m_head; + private: + chunk_list(chunk_list&& other) noexcept = delete; + chunk_list& operator=(chunk_list&& other) noexcept = delete; chunk_list(const chunk_list&); chunk_list& operator=(const chunk_list&); }; + size_t m_chunk_size; - chunk_list m_chunk_list; + chunk_list* m_chunk_list{}; finalizer_array m_finalizer_array; public: zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); -public: void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); + void* allocate_no_align(size_t size); + bool allocated() { + return m_chunk_list != MSGPACK_NULLPTR; + } + void push_finalizer(void (*func)(void*), void* data); template @@ -186,23 +145,21 @@ class zone { void swap(zone& o); - static void* operator new(std::size_t size) - { + static void* operator new(std::size_t size) { void* p = ::malloc(size); - if (!p) throw std::bad_alloc(); + if (!p) THROW std::bad_alloc(); return p; } - static void operator delete(void *p) noexcept - { + + static void operator delete(void *p) noexcept { ::free(p); } - static void* operator new(std::size_t /*size*/, void* mem) noexcept - { + + static void* operator new(std::size_t /*size*/, void* mem) noexcept { return mem; } - static void operator delete(void * /*p*/, void* /*mem*/) noexcept - { - } + + static void operator delete(void * /*p*/, void* /*mem*/) noexcept {} template T* allocate(Args... args); @@ -223,15 +180,23 @@ class zone { static char* get_aligned(char* ptr, size_t align); + chunk_list& get_chank_lst(); + char* allocate_expand(size_t size); }; -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ +inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(MSGPACK_NULLPTR) {} + +inline zone::~zone() { + m_finalizer_array.clear(); + if(m_chunk_list) { + m_chunk_list->~chunk_list(); + ::free(m_chunk_list); + m_chunk_list = MSGPACK_NULLPTR; + } } -inline char* zone::get_aligned(char* ptr, size_t align) -{ +inline char* zone::get_aligned(char* ptr, size_t align) { MSGPACK_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( @@ -239,37 +204,45 @@ inline char* zone::get_aligned(char* ptr, size_t align) ); } -inline void* zone::allocate_align(size_t size, size_t align) -{ - char* aligned = get_aligned(m_chunk_list.m_ptr, align); - size_t adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); - if (m_chunk_list.m_free < adjusted_size) { +inline zone::chunk_list& zone::get_chank_lst() { + if (!m_chunk_list) { + auto ptr = ::malloc(sizeof(chunk_list) + m_chunk_size); + if (!ptr) + THROW std::bad_alloc(); + m_chunk_list = new (ptr) chunk_list(m_chunk_size, reinterpret_cast(ptr) + sizeof(chunk_list)); + } + return *m_chunk_list; +} + +inline void* zone::allocate_align(size_t size, size_t align) { + chunk_list& chank_lst = get_chank_lst(); + char* aligned = get_aligned(chank_lst.m_ptr, align); + size_t adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); + if (chank_lst.m_free < adjusted_size) { size_t enough_size = size + align - 1; char* ptr = allocate_expand(enough_size); aligned = get_aligned(ptr, align); - adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); + adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; return aligned; } -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { +inline void* zone::allocate_no_align(size_t size) { + chunk_list& chank_lst = get_chank_lst(); + char* ptr = chank_lst.m_ptr; + if(chank_lst.m_free < size) { ptr = allocate_expand(size); } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + chank_lst.m_free -= size; + chank_lst.m_ptr += size; return ptr; } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; - +inline char* zone::allocate_expand(size_t size) { + chunk_list& cl = get_chank_lst(); size_t sz = m_chunk_size; while(sz < size) { @@ -282,84 +255,76 @@ inline char* zone::allocate_expand(size_t size) } chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); - if (!c) throw std::bad_alloc(); + if (!c) THROW std::bad_alloc(); char* ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; return ptr; } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ +inline void zone::push_finalizer(void (*func)(void*), void* data) { m_finalizer_array.push(func, data); } template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ +inline void zone::push_finalizer(msgpack::unique_ptr obj) { m_finalizer_array.push(&zone::object_delete, obj.release()); } -inline void zone::clear() -{ +inline void zone::clear() { m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -inline void zone::swap(zone& o) -{ +inline void zone::swap(zone& o) { std::swap(*this, o); } template -void zone::object_delete(void* obj) -{ +void zone::object_delete(void* obj) { delete static_cast(obj); } template -void zone::object_destruct(void* obj) -{ +void zone::object_destruct(void* obj) { static_cast(obj)->~T(); } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; +inline void zone::undo_allocate(size_t size) { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } +inline std::size_t aligned_size(std::size_t size, std::size_t align) { + return (size + align - 1) / align * align; +} template -T* zone::allocate(Args... args) -{ +T* zone::allocate(Args... args) { void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); try { m_finalizer_array.push(&zone::object_destruct, x); } catch (...) { undo_allocate(sizeof(T)); - throw; + RETHROW; } try { return new (x) T(args...); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); - throw; + RETHROW; } } -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { - return (size + align - 1) / align * align; -} - /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond diff --git a/include/msgpack/v1/fbuffer.hpp b/include/msgpack/v1/fbuffer.hpp index fd006baa..2a420807 100644 --- a/include/msgpack/v1/fbuffer.hpp +++ b/include/msgpack/v1/fbuffer.hpp @@ -32,7 +32,7 @@ class fbuffer { MSGPACK_ASSERT(buf || len == 0); if (!buf) return; if (1 != fwrite(buf, len, 1, m_file)) { - throw std::runtime_error("fwrite() failed"); + THROW std::runtime_error("fwrite() failed"); } } diff --git a/include/msgpack/v1/object.hpp b/include/msgpack/v1/object.hpp index fda54be9..cd1a00b0 100644 --- a/include/msgpack/v1/object.hpp +++ b/include/msgpack/v1/object.hpp @@ -284,7 +284,7 @@ class object_parser { } break; default: - throw msgpack::type_error(); + THROW msgpack::type_error(); break; } if (m_ctx.empty()) return; @@ -466,8 +466,16 @@ struct object_stringize_visitor { m_os << '"'; return true; } - bool visit_bin(const char* /*v*/, uint32_t size) { - m_os << "\"BIN(size:" << size << ")\""; + bool visit_bin(const char* v, uint32_t size) { + // print binary strings in hex + m_os << "\"BIN(size:" << size << "): 0x"; + for (uint32_t i = 0; i < size; ++i) { + std::ios::fmtflags flags(m_os.flags()); + m_os << std::hex << std::setw(2) << std::setfill('0') << static_cast(static_cast(v[i])); + m_os.flags(flags); + } + m_os << "\""; + // return true; } bool visit_ext(const char* v, uint32_t size) { @@ -633,6 +641,8 @@ namespace detail { template struct packer_serializer { static msgpack::packer& pack(msgpack::packer& o, const T& v) { + // Note, if this is failing, the associated type probably wants + // a MSGPACK_FIELDS(...fields...); macro call v.msgpack_pack(o); return o; } @@ -1040,11 +1050,13 @@ inline bool operator==(const msgpack::object& x, const msgpack::object& y) template inline bool operator==(const msgpack::object& x, const T& y) +{ try { return x == msgpack::object(y); } catch (msgpack::type_error&) { return false; } +} inline bool operator!=(const msgpack::object& x, const msgpack::object& y) { return !(x == y); } diff --git a/include/msgpack/v1/object_fwd.hpp b/include/msgpack/v1/object_fwd.hpp index 2562b416..951dca0e 100644 --- a/include/msgpack/v1/object_fwd.hpp +++ b/include/msgpack/v1/object_fwd.hpp @@ -53,16 +53,16 @@ template struct has_as { private: template - static auto check(U*) -> + static auto check_(U*) -> // Check v1 specialization typename std::is_same< decltype(adaptor::as()(std::declval())), T >::type; template - static std::false_type check(...); + static std::false_type check_(...); public: - using type = decltype(check(MSGPACK_NULLPTR)); + using type = decltype(check_(MSGPACK_NULLPTR)); static constexpr bool value = type::value; }; diff --git a/include/msgpack/v1/pack.hpp b/include/msgpack/v1/pack.hpp index 7c1d0f6b..41cff85f 100644 --- a/include/msgpack/v1/pack.hpp +++ b/include/msgpack/v1/pack.hpp @@ -1138,11 +1138,11 @@ inline packer& packer::pack_unsigned_long_long(unsigned long lon template inline packer& packer::pack_float(float d) { - if(d == d) { // check for nan + if(d == d) { // check for nan // compare d to limits to avoid undefined behaviour if(d >= 0 && d <= float(std::numeric_limits::max()) && d == float(uint64_t(d))) { pack_imp_uint64(uint64_t(d)); - return *this; + return *this; } else if(d < 0 && d >= float(std::numeric_limits::min()) && d == float(int64_t(d))) { pack_imp_int64(int64_t(d)); return *this; @@ -1160,11 +1160,11 @@ inline packer& packer::pack_float(float d) template inline packer& packer::pack_double(double d) { - if(d == d) { // check for nan + if(d == d) { // check for nan // compare d to limits to avoid undefined behaviour if(d >= 0 && d <= double(std::numeric_limits::max()) && d == double(uint64_t(d))) { pack_imp_uint64(uint64_t(d)); - return *this; + return *this; } else if(d < 0 && d >= double(std::numeric_limits::min()) && d == double(int64_t(d))) { pack_imp_int64(int64_t(d)); return *this; diff --git a/include/msgpack/v1/sbuffer.hpp b/include/msgpack/v1/sbuffer.hpp index 7ab30310..dbec5b3b 100644 --- a/include/msgpack/v1/sbuffer.hpp +++ b/include/msgpack/v1/sbuffer.hpp @@ -32,7 +32,7 @@ class sbuffer { } else { m_data = (char*)::malloc(initsz); if(!m_data) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } } } @@ -127,7 +127,7 @@ class sbuffer { void* tmp = ::realloc(m_data, nsize); if(!tmp) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_data = static_cast(tmp); diff --git a/include/msgpack/v1/unpack.hpp b/include/msgpack/v1/unpack.hpp index 489c285d..e8089e53 100644 --- a/include/msgpack/v1/unpack.hpp +++ b/include/msgpack/v1/unpack.hpp @@ -23,7 +23,7 @@ #include -#if !defined(MSGPACK_USE_CPP03) +#if !defined(MSGPACK_USE_CPP03) && !defined(__wasm__) #include #endif @@ -111,13 +111,13 @@ inline void unpack_false(msgpack::object& o) struct unpack_array { void operator()(unpack_user& u, uint32_t n, msgpack::object& o) const { - if (n > u.limit().array()) throw msgpack::array_size_overflow("array size overflow"); + if (n > u.limit().array()) THROW msgpack::array_size_overflow("array size overflow"); o.type = msgpack::type::ARRAY; o.via.array.size = 0; #if SIZE_MAX == UINT_MAX if (n > SIZE_MAX/sizeof(msgpack::object)) - throw msgpack::array_size_overflow("array size overflow"); + THROW msgpack::array_size_overflow("array size overflow"); #endif // SIZE_MAX == UINT_MAX size_t size = n*sizeof(msgpack::object); @@ -137,13 +137,13 @@ inline void unpack_array_item(msgpack::object& c, msgpack::object const& o) struct unpack_map { void operator()(unpack_user& u, uint32_t n, msgpack::object& o) const { - if (n > u.limit().map()) throw msgpack::map_size_overflow("map size overflow"); + if (n > u.limit().map()) THROW msgpack::map_size_overflow("map size overflow"); o.type = msgpack::type::MAP; o.via.map.size = 0; #if SIZE_MAX == UINT_MAX if (n > SIZE_MAX/sizeof(msgpack::object_kv)) - throw msgpack::map_size_overflow("map size overflow"); + THROW msgpack::map_size_overflow("map size overflow"); #endif // SIZE_MAX == UINT_MAX size_t size = n*sizeof(msgpack::object_kv); @@ -171,7 +171,7 @@ inline void unpack_str(unpack_user& u, const char* p, uint32_t l, msgpack::objec u.set_referenced(true); } else if (l > 0) { - if (l > u.limit().str()) throw msgpack::str_size_overflow("str size overflow"); + if (l > u.limit().str()) THROW msgpack::str_size_overflow("str size overflow"); char* tmp = static_cast(u.zone().allocate_align(l, MSGPACK_ZONE_ALIGNOF(char))); std::memcpy(tmp, p, l); o.via.str.ptr = tmp; @@ -190,7 +190,7 @@ inline void unpack_bin(unpack_user& u, const char* p, uint32_t l, msgpack::objec u.set_referenced(true); } else if (l > 0) { - if (l > u.limit().bin()) throw msgpack::bin_size_overflow("bin size overflow"); + if (l > u.limit().bin()) THROW msgpack::bin_size_overflow("bin size overflow"); char* tmp = static_cast(u.zone().allocate_align(l, MSGPACK_ZONE_ALIGNOF(char))); std::memcpy(tmp, p, l); o.via.bin.ptr = tmp; @@ -209,7 +209,7 @@ inline void unpack_ext(unpack_user& u, const char* p, std::size_t l, msgpack::ob u.set_referenced(true); } else { - if (l > u.limit().ext()) throw msgpack::ext_size_overflow("ext size overflow"); + if (l > u.limit().ext()) THROW msgpack::ext_size_overflow("ext size overflow"); char* tmp = static_cast(u.zone().allocate_align(l, MSGPACK_ZONE_ALIGNOF(char))); std::memcpy(tmp, p, l); o.via.ext.ptr = tmp; @@ -239,46 +239,46 @@ class unpack_stack { inline void init_count(void* buffer) { -#if defined(MSGPACK_USE_CPP03) +#if defined(MSGPACK_USE_CPP03) || defined(__wasm__) *reinterpret_cast(buffer) = 1; -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) || defined(__wasm__) new (buffer) std::atomic(1); -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) || defined(__wasm__) } inline void decr_count(void* buffer) { -#if defined(MSGPACK_USE_CPP03) +#if defined(MSGPACK_USE_CPP03) || defined(__wasm__) if(_msgpack_sync_decr_and_fetch(reinterpret_cast(buffer)) == 0) { free(buffer); } -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) || defined(__wasm__) if (--*reinterpret_cast*>(buffer) == 0) { free(buffer); } -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) || defined(__wasm__) } inline void incr_count(void* buffer) { -#if defined(MSGPACK_USE_CPP03) +#if defined(MSGPACK_USE_CPP03) || defined(__wasm__) _msgpack_sync_incr_and_fetch(reinterpret_cast(buffer)); -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) || defined(__wasm__) ++*reinterpret_cast*>(buffer); -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) || defined(__wasm__) } -#if defined(MSGPACK_USE_CPP03) +#if defined(MSGPACK_USE_CPP03) || defined(__wasm__) inline _msgpack_atomic_counter_t get_count(void* buffer) { return *reinterpret_cast(buffer); } -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) || defined(__wasm__) inline std::atomic const& get_count(void* buffer) { return *reinterpret_cast*>(buffer); } -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) || defined(__wasm__) template struct value { @@ -377,7 +377,7 @@ class context { m_stack.push_back(unpack_stack()); } else { - throw msgpack::depth_size_overflow("depth size overflow"); + THROW msgpack::depth_size_overflow("depth size overflow"); } m_cs = MSGPACK_CS_HEADER; ++m_current; @@ -460,7 +460,7 @@ class context { template <> inline void context::check_ext_size<4>(std::size_t size) { - if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow"); + if (size == 0xffffffff) THROW msgpack::ext_size_overflow("ext size overflow"); } inline int context::execute(const char* data, std::size_t len, std::size_t& off) @@ -873,10 +873,10 @@ class unpacker { std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE, unpack_limit const& limit = unpack_limit()); -#if !defined(MSGPACK_USE_CPP03) +#if !defined(MSGPACK_USE_CPP03) && !defined(__wasm__) unpacker(unpacker&& other); unpacker& operator=(unpacker&& other); -#endif // !defined(MSGPACK_USE_CPP03) +#endif // !defined(MSGPACK_USE_CPP03) && !defined(__wasm__) ~unpacker(); @@ -1049,14 +1049,14 @@ class unpacker { std::size_t m_initial_buffer_size; detail::context m_ctx; -#if defined(MSGPACK_USE_CPP03) +#if defined(MSGPACK_USE_CPP03) || defined(__wasm__) private: unpacker(const unpacker&); unpacker& operator=(const unpacker&); -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) || defined(__wasm__) unpacker(const unpacker&) = delete; unpacker& operator=(const unpacker&) = delete; -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) || defined(__wasm__) }; inline unpacker::unpacker(unpack_reference_func f, @@ -1071,7 +1071,7 @@ inline unpacker::unpacker(unpack_reference_func f, char* buffer = static_cast(::malloc(initial_buffer_size)); if(!buffer) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_buffer = buffer; @@ -1088,7 +1088,7 @@ inline unpacker::unpacker(unpack_reference_func f, m_ctx.user().set_referenced(false); } -#if !defined(MSGPACK_USE_CPP03) +#if !defined(MSGPACK_USE_CPP03) && !defined(__wasm__) // Move constructor and move assignment operator inline unpacker::unpacker(unpacker&& other) @@ -1109,7 +1109,7 @@ inline unpacker& unpacker::operator=(unpacker&& other) { return *this; } -#endif // !defined(MSGPACK_USE_CPP03) +#endif // !defined(MSGPACK_USE_CPP03) && !defined(__wasm__) inline unpacker::~unpacker() @@ -1150,7 +1150,7 @@ inline void unpacker::expand_buffer(std::size_t size) char* tmp = static_cast(::realloc(m_buffer, next_size)); if(!tmp) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_buffer = tmp; @@ -1170,7 +1170,7 @@ inline void unpacker::expand_buffer(std::size_t size) char* tmp = static_cast(::malloc(next_size)); if(!tmp) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } detail::init_count(tmp); @@ -1183,7 +1183,7 @@ inline void unpacker::expand_buffer(std::size_t size) } catch (...) { ::free(tmp); - throw; + RETHROW; } m_ctx.user().set_referenced(false); } else { @@ -1218,7 +1218,7 @@ inline bool unpacker::next(msgpack::object_handle& result, bool& referenced) referenced = false; int ret = execute_imp(); if(ret < 0) { - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } if(ret == 0) { @@ -1251,7 +1251,7 @@ inline bool unpacker::execute() { int ret = execute_imp(); if(ret < 0) { - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } else if(ret == 0) { return false; } else { @@ -1414,10 +1414,10 @@ inline msgpack::object_handle unpack( off = noff; return msgpack::object_handle(obj, msgpack::move(z)); case PARSE_CONTINUE: - throw msgpack::insufficient_bytes("insufficient bytes"); + THROW msgpack::insufficient_bytes("insufficient bytes"); case PARSE_PARSE_ERROR: default: - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } return msgpack::object_handle(); } @@ -1475,10 +1475,10 @@ inline void unpack( result.zone() = msgpack::move(z); return; case PARSE_CONTINUE: - throw msgpack::insufficient_bytes("insufficient bytes"); + THROW msgpack::insufficient_bytes("insufficient bytes"); case PARSE_PARSE_ERROR: default: - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } } @@ -1534,10 +1534,10 @@ inline msgpack::object unpack( off = noff; return obj; case PARSE_CONTINUE: - throw msgpack::insufficient_bytes("insufficient bytes"); + THROW msgpack::insufficient_bytes("insufficient bytes"); case PARSE_PARSE_ERROR: default: - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } return obj; } diff --git a/include/msgpack/v1/unpack_decl.hpp b/include/msgpack/v1/unpack_decl.hpp index 0a103fea..5a423fbd 100644 --- a/include/msgpack/v1/unpack_decl.hpp +++ b/include/msgpack/v1/unpack_decl.hpp @@ -21,7 +21,7 @@ #include #include -#if !defined(MSGPACK_USE_CPP03) +#if !defined(MSGPACK_USE_CPP03) && !defined(__wasm__) #include #endif @@ -167,15 +167,15 @@ void decr_count(void* buffer); void incr_count(void* buffer); -#if defined(MSGPACK_USE_CPP03) +#if defined(MSGPACK_USE_CPP03) || defined(__wasm__) _msgpack_atomic_counter_t get_count(void* buffer); -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) || defined(__wasm__) std::atomic const& get_count(void* buffer); -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) || defined(__wasm__) struct fix_tag { char f1[65]; // FIXME unique size is required. or use is_same meta function. diff --git a/include/msgpack/v1/vrefbuffer.hpp b/include/msgpack/v1/vrefbuffer.hpp index 1887a5c3..c589ec0a 100644 --- a/include/msgpack/v1/vrefbuffer.hpp +++ b/include/msgpack/v1/vrefbuffer.hpp @@ -65,7 +65,7 @@ class vrefbuffer { m_chunk_size(chunk_size) { if((sizeof(chunk) + chunk_size) < chunk_size) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } size_t nfirst = (sizeof(iovec) < 72/2) ? @@ -74,7 +74,7 @@ class vrefbuffer { iovec* array = static_cast(::malloc( sizeof(iovec) * nfirst)); if(!array) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_tail = array; @@ -84,7 +84,7 @@ class vrefbuffer { chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); if(!c) { ::free(array); - throw std::bad_alloc(); + THROW std::bad_alloc(); } inner_buffer* const ib = &m_inner_buffer; @@ -133,7 +133,7 @@ class vrefbuffer { iovec* nvec = static_cast(::realloc( m_array, sizeof(iovec)*nnext)); if(!nvec) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_array = nvec; @@ -157,12 +157,12 @@ class vrefbuffer { } if(sizeof(chunk) + sz < sz){ - throw std::bad_alloc(); + THROW std::bad_alloc(); } chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); if(!c) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } c->next = ib->head; @@ -202,12 +202,12 @@ class vrefbuffer { size_t sz = m_chunk_size; if((sizeof(chunk) + sz) < sz){ - throw std::bad_alloc(); + THROW std::bad_alloc(); } chunk* empty = static_cast(::malloc(sizeof(chunk) + sz)); if(!empty) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } empty->next = MSGPACK_NULLPTR; @@ -230,7 +230,7 @@ class vrefbuffer { to->m_array, sizeof(iovec)*nnext)); if(!nvec) { ::free(empty); - throw std::bad_alloc(); + THROW std::bad_alloc(); } to->m_array = nvec; diff --git a/include/msgpack/v1/zbuffer.hpp b/include/msgpack/v1/zbuffer.hpp index 96b0af30..772d06d8 100644 --- a/include/msgpack/v1/zbuffer.hpp +++ b/include/msgpack/v1/zbuffer.hpp @@ -34,7 +34,7 @@ class zbuffer { m_stream.next_out = Z_NULL; m_stream.avail_out = 0; if(deflateInit(&m_stream, level) != Z_OK) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } } @@ -56,12 +56,12 @@ class zbuffer { while(m_stream.avail_in > 0) { if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) { if(!expand()) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } } if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } } } @@ -75,11 +75,11 @@ class zbuffer { case Z_OK: case Z_BUF_ERROR: if(!expand()) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } break; default: - throw std::bad_alloc(); + THROW std::bad_alloc(); } } } @@ -102,7 +102,7 @@ class zbuffer { void reset() { if(deflateReset(&m_stream) != Z_OK) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } reset_buffer(); } diff --git a/include/msgpack/v2/create_object_visitor.hpp b/include/msgpack/v2/create_object_visitor.hpp index d0e234e9..b6838ce4 100644 --- a/include/msgpack/v2/create_object_visitor.hpp +++ b/include/msgpack/v2/create_object_visitor.hpp @@ -108,7 +108,7 @@ class create_object_visitor : public msgpack::v2::null_visitor { } bool visit_str(const char* v, uint32_t size) { MSGPACK_ASSERT(v || size == 0); - if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow"); + if (size > m_limit.str()) THROW msgpack::str_size_overflow("str size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::STR; if (m_func && m_func(obj->type, size, m_user_data)) { @@ -132,7 +132,7 @@ class create_object_visitor : public msgpack::v2::null_visitor { } bool visit_bin(const char* v, uint32_t size) { MSGPACK_ASSERT(v || size == 0); - if (size > m_limit.bin()) throw msgpack::bin_size_overflow("bin size overflow"); + if (size > m_limit.bin()) THROW msgpack::bin_size_overflow("bin size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::BIN; if (m_func && m_func(obj->type, size, m_user_data)) { @@ -156,7 +156,7 @@ class create_object_visitor : public msgpack::v2::null_visitor { } bool visit_ext(const char* v, uint32_t size) { MSGPACK_ASSERT(v || size == 0); - if (size > m_limit.ext()) throw msgpack::ext_size_overflow("ext size overflow"); + if (size > m_limit.ext()) THROW msgpack::ext_size_overflow("ext size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::EXT; if (m_func && m_func(obj->type, size, m_user_data)) { @@ -179,8 +179,8 @@ class create_object_visitor : public msgpack::v2::null_visitor { return true; } bool start_array(uint32_t num_elements) { - if (num_elements > m_limit.array()) throw msgpack::array_size_overflow("array size overflow"); - if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow"); + if (num_elements > m_limit.array()) THROW msgpack::array_size_overflow("array size overflow"); + if (m_stack.size() > m_limit.depth()) THROW msgpack::depth_size_overflow("depth size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::ARRAY; obj->via.array.size = num_elements; @@ -191,7 +191,7 @@ class create_object_visitor : public msgpack::v2::null_visitor { #if SIZE_MAX == UINT_MAX if (num_elements > SIZE_MAX/sizeof(msgpack::object)) - throw msgpack::array_size_overflow("array size overflow"); + THROW msgpack::array_size_overflow("array size overflow"); #endif // SIZE_MAX == UINT_MAX size_t size = num_elements*sizeof(msgpack::object); @@ -213,8 +213,8 @@ class create_object_visitor : public msgpack::v2::null_visitor { return true; } bool start_map(uint32_t num_kv_pairs) { - if (num_kv_pairs > m_limit.map()) throw msgpack::map_size_overflow("map size overflow"); - if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow"); + if (num_kv_pairs > m_limit.map()) THROW msgpack::map_size_overflow("map size overflow"); + if (m_stack.size() > m_limit.depth()) THROW msgpack::depth_size_overflow("depth size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::MAP; obj->via.map.size = num_kv_pairs; @@ -225,7 +225,7 @@ class create_object_visitor : public msgpack::v2::null_visitor { #if SIZE_MAX == UINT_MAX if (num_kv_pairs > SIZE_MAX/sizeof(msgpack::object_kv)) - throw msgpack::map_size_overflow("map size overflow"); + THROW msgpack::map_size_overflow("map size overflow"); #endif // SIZE_MAX == UINT_MAX size_t size = num_kv_pairs*sizeof(msgpack::object_kv); obj->via.map.ptr = @@ -253,10 +253,10 @@ class create_object_visitor : public msgpack::v2::null_visitor { return true; } void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { - throw msgpack::insufficient_bytes("insufficient bytes"); + THROW msgpack::insufficient_bytes("insufficient bytes"); } private: public: diff --git a/include/msgpack/v2/object_fwd.hpp b/include/msgpack/v2/object_fwd.hpp index 9c44aae6..45a63118 100644 --- a/include/msgpack/v2/object_fwd.hpp +++ b/include/msgpack/v2/object_fwd.hpp @@ -79,7 +79,7 @@ template struct has_as { private: template - static auto check(U*) -> + static auto check_(U*) -> typename std::enable_if< // check v2 specialization std::is_same< @@ -92,9 +92,9 @@ struct has_as { std::true_type >::type; template - static std::false_type check(...); + static std::false_type check_(...); public: - using type = decltype(check(MSGPACK_NULLPTR)); + using type = decltype(check_(MSGPACK_NULLPTR)); static constexpr bool value = type::value; }; diff --git a/include/msgpack/v2/parse.hpp b/include/msgpack/v2/parse.hpp index bfdd1d00..6e5572ab 100644 --- a/include/msgpack/v2/parse.hpp +++ b/include/msgpack/v2/parse.hpp @@ -229,7 +229,7 @@ inline void check_ext_size(std::size_t /*size*/) { template <> inline void check_ext_size<4>(std::size_t size) { - if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow"); + if (size == 0xffffffff) THROW msgpack::ext_size_overflow("ext size overflow"); } template @@ -793,7 +793,7 @@ inline parser::parser( char* buffer = static_cast(::malloc(initial_buffer_size)); if(!buffer) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_buffer = buffer; @@ -877,7 +877,7 @@ inline void parser::expand_buffer(std::size char* tmp = static_cast(::realloc(m_buffer, next_size)); if(!tmp) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } m_buffer = tmp; @@ -897,7 +897,7 @@ inline void parser::expand_buffer(std::size char* tmp = static_cast(::malloc(next_size)); if(!tmp) { - throw std::bad_alloc(); + THROW std::bad_alloc(); } detail::init_count(tmp); @@ -910,7 +910,7 @@ inline void parser::expand_buffer(std::size } catch (...) { ::free(tmp); - throw; + RETHROW; } static_cast(*this).set_referenced(false); } else { diff --git a/include/msgpack/v2/x3_unpack.hpp b/include/msgpack/v2/x3_unpack.hpp index 54ca98ea..9bd6954e 100644 --- a/include/msgpack/v2/x3_unpack.hpp +++ b/include/msgpack/v2/x3_unpack.hpp @@ -41,7 +41,7 @@ unpack_imp(Iterator&& begin, Iterator&& end, referenced = false; v.set_referenced(referenced); if (!parse(std::forward(begin), std::forward(end), v)) { - throw msgpack::parse_error("parse error"); + THROW msgpack::parse_error("parse error"); } referenced = v.referenced(); result = v.data(); diff --git a/include/msgpack/v3/object_fwd.hpp b/include/msgpack/v3/object_fwd.hpp index d282f1b4..a0ed44ae 100644 --- a/include/msgpack/v3/object_fwd.hpp +++ b/include/msgpack/v3/object_fwd.hpp @@ -36,7 +36,7 @@ template struct has_as { private: template - static auto check(U*) -> + static auto check_(U*) -> typename std::enable_if< // check v3 specialization std::is_same< @@ -52,9 +52,9 @@ struct has_as { std::true_type >::type; template - static std::false_type check(...); + static std::false_type check_(...); public: - using type = decltype(check(MSGPACK_NULLPTR)); + using type = decltype(check_(MSGPACK_NULLPTR)); static constexpr bool value = type::value; }; diff --git a/include/msgpack/v3/parse.hpp b/include/msgpack/v3/parse.hpp index 8a4b338a..11b4d253 100644 --- a/include/msgpack/v3/parse.hpp +++ b/include/msgpack/v3/parse.hpp @@ -238,7 +238,7 @@ inline void check_ext_size(std::size_t /*size*/) { template <> inline void check_ext_size<4>(std::size_t size) { - if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow"); + if (size == 0xffffffff) THROW msgpack::ext_size_overflow("ext size overflow"); } template diff --git a/include/msgpack/version_master.hpp b/include/msgpack/version_master.hpp index fdab0889..ad76f737 100644 --- a/include/msgpack/version_master.hpp +++ b/include/msgpack/version_master.hpp @@ -1,3 +1,3 @@ #define MSGPACK_VERSION_MAJOR 6 -#define MSGPACK_VERSION_MINOR 0 +#define MSGPACK_VERSION_MINOR 1 #define MSGPACK_VERSION_REVISION 0 diff --git a/preprocess.rb b/preprocess.rb new file mode 100644 index 00000000..95cd313b --- /dev/null +++ b/preprocess.rb @@ -0,0 +1,17 @@ +require 'erb' + +files = { + "erb/v1/cpp03_msgpack_tuple_decl.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple_decl.hpp", + "erb/v1/cpp03_msgpack_tuple.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp", + "erb/v1/cpp03_define_array_decl.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_array_decl.hpp", + "erb/v1/cpp03_define_array.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_array.hpp", + "erb/v1/cpp03_define_map_decl.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_map_decl.hpp", + "erb/v1/cpp03_define_map.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp", + "erb/v1/cpp03_zone_decl.hpp" => "include/msgpack/v1/detail/cpp03_zone_decl.hpp", + "erb/v1/cpp03_zone.hpp" => "include/msgpack/v1/detail/cpp03_zone.hpp" +} + +files.map { |erb, hpp| + res = ERB.new(File.open(erb+".erb").read).result + File.write(hpp, res) +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e0ebf258..a0233dd3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ FIND_PACKAGE (Threads REQUIRED) FIND_PACKAGE (ZLIB) -FIND_PACKAGE (Boost REQUIRED COMPONENTS unit_test_framework system) +FIND_PACKAGE (Boost REQUIRED COMPONENTS unit_test_framework) LIST (APPEND check_PROGRAMS array_ref.cpp @@ -79,7 +79,6 @@ FOREACH (source_file ${check_PROGRAMS}) TARGET_LINK_LIBRARIES (${source_file_we} msgpack-cxx - Boost::system Boost::unit_test_framework Threads::Threads ZLIB::ZLIB diff --git a/test/boost_variant.cpp b/test/boost_variant.cpp index 9ab3dc61..647fa474 100644 --- a/test/boost_variant.cpp +++ b/test/boost_variant.cpp @@ -264,7 +264,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_float) BOOST_CHECK(val2.is_double()); BOOST_CHECK(fabs(12.34 - val2.as_double()) <= kEPS); BOOST_CHECK_NO_THROW(boost::get(val2)); - BOOST_CHECK(fabs(val2.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(fabs(val1.as_double() - val2.as_double()) <= kEPS); } BOOST_AUTO_TEST_CASE(object_variant_float) @@ -277,7 +277,8 @@ BOOST_AUTO_TEST_CASE(object_variant_float) BOOST_CHECK(val2.is_double()); BOOST_CHECK(fabs(12.34 - val2.as_double()) <= kEPS); BOOST_CHECK_NO_THROW(boost::get(val2)); - BOOST_CHECK(fabs(val2.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(fabs(val1.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(object_with_zone_variant_float) @@ -291,7 +292,116 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_float) BOOST_CHECK(val2.is_double()); BOOST_CHECK(fabs(12.34 - val2.as_double()) <= kEPS); BOOST_CHECK_NO_THROW(boost::get(val2)); - BOOST_CHECK(fabs(val2.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(fabs(val1.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(pack_convert_variant_float_zero_atdp_positive) +{ + std::stringstream ss; + msgpack::type::variant val1 = 12.0; + BOOST_CHECK(val1.is_uint64_t()); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), 12); + BOOST_CHECK(fabs(12.0 - val1.as_double()) <= kEPS); + + msgpack::pack(ss, val1); + + std::string const& str = ss.str(); + msgpack::object_handle oh = + msgpack::unpack(str.data(), str.size()); + msgpack::type::variant val2 = oh.get().as(); + BOOST_CHECK(val2.is_uint64_t()); + BOOST_CHECK_EQUAL(val2.as_uint64_t(), 12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), val2.as_uint64_t()); +} + +BOOST_AUTO_TEST_CASE(object_variant_float_zero_atdp_positive) +{ + msgpack::type::variant val1 = 12.0; + BOOST_CHECK(val1.is_uint64_t()); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), 12); + BOOST_CHECK(fabs(12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_uint64_t()); + BOOST_CHECK_EQUAL(val2.as_uint64_t(), 12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), val2.as_uint64_t()); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(object_with_zone_variant_float_zero_atdp_positive) +{ + msgpack::zone z; + msgpack::type::variant val1 = 12.0; + BOOST_CHECK(val1.is_uint64_t()); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), 12); + BOOST_CHECK(fabs(12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1, z); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_uint64_t()); + BOOST_CHECK_EQUAL(val2.as_uint64_t(), 12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), val2.as_uint64_t()); + BOOST_CHECK(fabs(12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(pack_convert_variant_float_zero_atdp_negative) +{ + std::stringstream ss; + msgpack::type::variant val1 = -12.0; + BOOST_CHECK(val1.is_int64_t()); + BOOST_CHECK_EQUAL(val1.as_int64_t(), -12); + BOOST_CHECK(fabs(-12.0 - val1.as_double()) <= kEPS); + + msgpack::pack(ss, val1); + + std::string const& str = ss.str(); + msgpack::object_handle oh = + msgpack::unpack(str.data(), str.size()); + msgpack::type::variant val2 = oh.get().as(); + BOOST_CHECK(val2.is_int64_t()); + BOOST_CHECK_EQUAL(val2.as_int64_t(), -12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(-12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_int64_t(), val2.as_int64_t()); +} + +BOOST_AUTO_TEST_CASE(object_variant_float_zero_atdp_negative) +{ + msgpack::type::variant val1 = -12.0; + BOOST_CHECK(val1.is_int64_t()); + BOOST_CHECK_EQUAL(val1.as_int64_t(), -12); + BOOST_CHECK(fabs(-12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_int64_t()); + BOOST_CHECK_EQUAL(val2.as_int64_t(), -12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(-12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_int64_t(), val2.as_int64_t()); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(object_with_zone_variant_float_zero_atdp_negative) +{ + msgpack::zone z; + msgpack::type::variant val1 = -12.0; + BOOST_CHECK(val1.is_int64_t()); + BOOST_CHECK_EQUAL(val1.as_int64_t(), -12); + BOOST_CHECK(fabs(-12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1, z); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_int64_t()); + BOOST_CHECK_EQUAL(val2.as_int64_t(), -12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(-12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_int64_t(), val2.as_int64_t()); + BOOST_CHECK(val1 == val2); } // str diff --git a/test/msgpack_cpp11.cpp b/test/msgpack_cpp11.cpp index 2a856cb8..3375a27b 100644 --- a/test/msgpack_cpp11.cpp +++ b/test/msgpack_cpp11.cpp @@ -252,6 +252,9 @@ struct equal_to : std::equal_to { template struct set_allocator : std::allocator { using std::allocator::allocator; + + template + struct rebind { using other = set_allocator; }; }; // C++ named requirement Allocator implies that the first template type @@ -262,6 +265,9 @@ struct set_allocator : std::allocator { template struct map_allocator_impl : std::allocator { using std::allocator::allocator; + + template + struct rebind { using other = map_allocator_impl; }; }; template @@ -270,6 +276,9 @@ using map_allocator = map_allocator_impl>; template struct allocator : std::allocator { using std::allocator::allocator; + + template + struct rebind { using other = allocator; }; }; } // namespace test @@ -861,8 +870,6 @@ BOOST_AUTO_TEST_CASE(no_def_con_array_simple_buffer) BOOST_CHECK(val1 == val2); } -#if !defined(MSGPACK_NO_BOOST) - BOOST_AUTO_TEST_CASE(system_clock) { std::chrono::system_clock::time_point val1; @@ -1428,8 +1435,6 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_impl_now) BOOST_CHECK(val1 == val3); } -#endif // !defined(MSGPACK_NO_BOOST) - BOOST_AUTO_TEST_CASE(timespec_pack_convert_zero) { diff --git a/test/msgpack_cpp17.cpp b/test/msgpack_cpp17.cpp index c90a55ad..b20c04cc 100644 --- a/test/msgpack_cpp17.cpp +++ b/test/msgpack_cpp17.cpp @@ -461,4 +461,30 @@ BOOST_AUTO_TEST_CASE(carray_byte_object_with_zone) } } +#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) + +BOOST_AUTO_TEST_CASE(variant_pack_unpack_as) { + std::stringstream ss; + std::variant val1{1.0}; + msgpack::pack(ss, val1); + std::string const& str = ss.str(); + msgpack::object_handle oh = + msgpack::unpack(str.data(), str.size()); + std::variant val2 = + oh.get().as >(); + BOOST_CHECK(val1 == val2); + BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); +} + +BOOST_AUTO_TEST_CASE(variant_with_zone) { + msgpack::zone z; + std::variant val1{1.0}; + msgpack::object obj(val1, z); + std::variant val2 = obj.as>(); + BOOST_CHECK(val1 == val2); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); +} + +#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) + #endif // MSGPACK_CPP_VERSION >= 201703