diff --git a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp index cd2d7c5c3480..1c4ac87b03cc 100644 --- a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp @@ -6,6 +6,7 @@ #include #include #define MSGPACK_NO_BOOST +#define MSGPACK_USE_STD_VARIANT_ADAPTOR #include "concepts.hpp" #include "drop_keys.hpp" #include diff --git a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/variant_impl.hpp b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/variant_impl.hpp deleted file mode 100644 index 8b5d466b5374..000000000000 --- a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/variant_impl.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -// Note: Meant to only be included in compilation units that need msgpack -#define MSGPACK_NO_BOOST -#include -#include - -namespace msgpack::adaptor { -// writes std::variant to msgpack format (TODO should we read std::variant?) -template struct pack> { - auto& operator()(auto& o, std::variant const& variant) const - { - std::visit([&o](const auto& arg) { o.pack(arg); }, variant); - return o; - } -}; -} // namespace msgpack::adaptor diff --git a/barretenberg/cpp/src/msgpack-c/.clang-format b/barretenberg/cpp/src/msgpack-c/.clang-format deleted file mode 100644 index a43d914ec38d..000000000000 --- a/barretenberg/cpp/src/msgpack-c/.clang-format +++ /dev/null @@ -1,2 +0,0 @@ -DisableFormat: true -SortIncludes: false \ No newline at end of file diff --git a/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh b/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh index fce05159c700..3c2c8b25989d 100755 --- a/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh +++ b/barretenberg/cpp/src/msgpack-c/.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/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml b/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml index 8c3bf1eda1f0..e286d279d721 100644 --- a/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml +++ b/barretenberg/cpp/src/msgpack-c/.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/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml b/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml index 1fbb5ebc593d..29fd31bf8e91 100644 --- a/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml +++ b/barretenberg/cpp/src/msgpack-c/.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/barretenberg/cpp/src/msgpack-c/.gitignore b/barretenberg/cpp/src/msgpack-c/.gitignore index 40f5e0af3be8..7b96d6dfc33d 100644 --- a/barretenberg/cpp/src/msgpack-c/.gitignore +++ b/barretenberg/cpp/src/msgpack-c/.gitignore @@ -49,3 +49,8 @@ Makefile /test/streaming_c /test/version /test/zone + +build +*-build +.cache +compile_commands.json diff --git a/barretenberg/cpp/src/msgpack-c/CHANGELOG.md b/barretenberg/cpp/src/msgpack-c/CHANGELOG.md index f29bffe8ad90..73199b017cc2 100644 --- a/barretenberg/cpp/src/msgpack-c/CHANGELOG.md +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/CMakeLists.txt index d44df4e21cf8..8dc6d610abe8 100644 --- a/barretenberg/cpp/src/msgpack-c/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/Files.cmake b/barretenberg/cpp/src/msgpack-c/Files.cmake index 5cdf80afd864..721a31fbf500 100644 --- a/barretenberg/cpp/src/msgpack-c/Files.cmake +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/README.md b/barretenberg/cpp/src/msgpack-c/README.md index 9f6a459b631d..cf578c5409db 100644 --- a/barretenberg/cpp/src/msgpack-c/README.md +++ b/barretenberg/cpp/src/msgpack-c/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/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) [![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/barretenberg/cpp/src/msgpack-c/appveyor.yml b/barretenberg/cpp/src/msgpack-c/appveyor.yml index 4218f3a3d916..6f5034b8c0e3 100644 --- a/barretenberg/cpp/src/msgpack-c/appveyor.yml +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh b/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh index a89c44641f1d..b6ad78c50a20 100755 --- a/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb b/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb index 1081f72457fe..86bc83286312 100644 --- a/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb +++ b/barretenberg/cpp/src/msgpack-c/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; } @@ -326,7 +315,7 @@ T* zone::allocate(<%=(1..i).map{|j|"A#{j} a#{j}"}.join(', ')%>) 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)); RETHROW; } diff --git a/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt index ad9c979ce472..6464513b326d 100644 --- a/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt index 5fc6c1afa409..963d6da4f4b2 100644 --- a/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt index 819130ed2fd0..9779687417d4 100644 --- a/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/include/msgpack/adaptor/cpp17/variant.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/adaptor/cpp17/variant.hpp new file mode 100644 index 000000000000..bd73ff9ae5e8 --- /dev/null +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp index 180e345c7a9a..77feae6ec989 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp @@ -27,7 +27,7 @@ struct AbortStream { void operator<< [[noreturn]] (const auto& error) { - (void)error; // TODO how to print this? + info(error.what()); std::abort(); } }; diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp index e48bdade9a56..0e9aa221bd29 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp @@ -1,45 +1,43 @@ -#include "cpp_config.hpp" #include "adaptor/array_ref.hpp" #include "adaptor/bool.hpp" #include "adaptor/carray.hpp" #include "adaptor/char_ptr.hpp" +#include "adaptor/complex.hpp" +#include "adaptor/define.hpp" #include "adaptor/deque.hpp" #include "adaptor/ext.hpp" #include "adaptor/fixint.hpp" #include "adaptor/float.hpp" #include "adaptor/int.hpp" -#include "adaptor/complex.hpp" #include "adaptor/list.hpp" #include "adaptor/map.hpp" +#include "adaptor/msgpack_tuple.hpp" #include "adaptor/nil.hpp" #include "adaptor/pair.hpp" #include "adaptor/raw.hpp" -#include "adaptor/v4raw.hpp" #include "adaptor/set.hpp" #include "adaptor/size_equal_only.hpp" #include "adaptor/string.hpp" +#include "adaptor/v4raw.hpp" #include "adaptor/vector.hpp" #include "adaptor/vector_bool.hpp" #include "adaptor/vector_char.hpp" #include "adaptor/vector_unsigned_char.hpp" #include "adaptor/wstring.hpp" -#include "adaptor/msgpack_tuple.hpp" -#include "adaptor/define.hpp" +#include "cpp_config.hpp" #if defined(MSGPACK_USE_CPP03) #include "adaptor/tr1/unordered_map.hpp" #include "adaptor/tr1/unordered_set.hpp" -#else // defined(MSGPACK_USE_CPP03) +#else // defined(MSGPACK_USE_CPP03) #include "adaptor/cpp11/array.hpp" #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/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp index a87a4e445133..531ea3c1d2cc 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp @@ -12,20 +12,19 @@ #include "msgpack/v1/adaptor/boost/msgpack_variant_decl.hpp" -#include "msgpack/adaptor/check_container_size.hpp" #include "msgpack/adaptor/boost/string_ref.hpp" +#include "msgpack/adaptor/check_container_size.hpp" -#include "msgpack/adaptor/nil.hpp" #include "msgpack/adaptor/bool.hpp" -#include "msgpack/adaptor/int.hpp" +#include "msgpack/adaptor/ext.hpp" #include "msgpack/adaptor/float.hpp" -#include "msgpack/adaptor/string.hpp" -#include "msgpack/adaptor/vector_char.hpp" +#include "msgpack/adaptor/int.hpp" +#include "msgpack/adaptor/map.hpp" +#include "msgpack/adaptor/nil.hpp" #include "msgpack/adaptor/raw.hpp" -#include "msgpack/adaptor/ext.hpp" +#include "msgpack/adaptor/string.hpp" #include "msgpack/adaptor/vector.hpp" -#include "msgpack/adaptor/map.hpp" - +#include "msgpack/adaptor/vector_char.hpp" #if defined(__GNUC__) #pragma GCC diagnostic push @@ -43,406 +42,359 @@ namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v1) { -/// @endcond - -namespace type { - - -template -struct basic_variant : - boost::variant< - nil_t, // NIL - bool, // BOOL - int64_t, // NEGATIVE_INTEGER - uint64_t, // POSITIVE_INTEGER - double, // FLOAT32, FLOAT64 - std::string, // STR +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + /// @endcond + + namespace type { + + template + struct basic_variant + : boost::variant< + nil_t, // NIL + bool, // BOOL + int64_t, // NEGATIVE_INTEGER + uint64_t, // POSITIVE_INTEGER + double, // FLOAT32, FLOAT64 + std::string, // STR #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - boost::string_ref, // STR -#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - std::vector, // BIN - msgpack::type::raw_ref, // BIN - msgpack::type::ext, // EXT - msgpack::type::ext_ref, // EXT - boost::recursive_wrapper > >, // ARRAY - boost::recursive_wrapper, basic_variant > >, // MAP - boost::recursive_wrapper, basic_variant > >// MAP - >, - private boost::totally_ordered > { - typedef boost::variant< - nil_t, // NIL - bool, // BOOL - int64_t, // NEGATIVE_INTEGER - uint64_t, // POSITIVE_INTEGER - double, // FLOAT32, FLOAT64 - std::string, // STR + boost::string_ref, // STR +#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + std::vector, // BIN + msgpack::type::raw_ref, // BIN + msgpack::type::ext, // EXT + msgpack::type::ext_ref, // EXT + boost::recursive_wrapper>>, // ARRAY + boost::recursive_wrapper, basic_variant>>, // MAP + boost::recursive_wrapper, basic_variant>> // MAP + >, + private boost::totally_ordered> { + typedef boost::variant< + nil_t, // NIL + bool, // BOOL + int64_t, // NEGATIVE_INTEGER + uint64_t, // POSITIVE_INTEGER + double, // FLOAT32, FLOAT64 + std::string, // STR #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - boost::string_ref, // STR -#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - std::vector, // BIN - msgpack::type::raw_ref, // BIN - msgpack::type::ext, // EXT - msgpack::type::ext_ref, // EXT - boost::recursive_wrapper > >, // ARRAY - boost::recursive_wrapper, basic_variant > >, // MAP - boost::recursive_wrapper, basic_variant > >// MAP - > base; - basic_variant() {} - template - basic_variant(T const& t):base(t) {} + boost::string_ref, // STR +#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + std::vector, // BIN + msgpack::type::raw_ref, // BIN + msgpack::type::ext, // EXT + msgpack::type::ext_ref, // EXT + boost::recursive_wrapper>>, // ARRAY + boost::recursive_wrapper, basic_variant>>, // MAP + boost::recursive_wrapper, basic_variant>> // MAP + > + base; + basic_variant() {} + template + basic_variant(T const& t) + : base(t) + {} #if defined(_MSC_VER) && _MSC_VER < 1700 - // The following redundant functions are required to avoid MSVC - // See https://svn.boost.org/trac/boost/ticket/592 - basic_variant(basic_variant const& other):base(static_cast(other)) {} - basic_variant& operator=(basic_variant const& other) { - *static_cast(this) = static_cast(other); - return *this; - } + // The following redundant functions are required to avoid MSVC + // See https://svn.boost.org/trac/boost/ticket/592 + basic_variant(basic_variant const& other) + : base(static_cast(other)) + {} + basic_variant& operator=(basic_variant const& other) + { + *static_cast(this) = static_cast(other); + return *this; + } #endif // defined(_MSC_VER) && _MSC_VER < 1700 - basic_variant(char const* p):base(std::string(p)) {} - basic_variant(char v) { - int_init(v); - } - basic_variant(signed char v) { - int_init(v); - } - basic_variant(unsigned char v):base(uint64_t(v)) {} - basic_variant(signed int v) { - int_init(v); - } - basic_variant(unsigned int v):base(uint64_t(v)) {} - basic_variant(signed long v) { - int_init(v); - } - basic_variant(unsigned long v):base(uint64_t(v)) {} - basic_variant(signed long long v) { - int_init(v); - } - basic_variant(unsigned long long v):base(uint64_t(v)) {} - - bool is_nil() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_bool() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_int64_t() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_uint64_t() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_double() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_string() const { - return boost::get(this) != MSGPACK_NULLPTR; - } + basic_variant(char const* p) + : base(std::string(p)) + {} + basic_variant(char v) { int_init(v); } + basic_variant(signed char v) { int_init(v); } + basic_variant(unsigned char v) + : base(uint64_t(v)) + {} + basic_variant(signed int v) { int_init(v); } + basic_variant(unsigned int v) + : base(uint64_t(v)) + {} + basic_variant(signed long v) { int_init(v); } + basic_variant(unsigned long v) + : base(uint64_t(v)) + {} + basic_variant(signed long long v) { 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; } + bool is_bool() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_int64_t() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_uint64_t() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_double() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_string() const { return boost::get(this) != MSGPACK_NULLPTR; } #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - bool is_boost_string_ref() const { - return boost::get(this) != MSGPACK_NULLPTR; - } + bool is_boost_string_ref() const { return boost::get(this) != MSGPACK_NULLPTR; } #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - bool is_vector_char() const { - return boost::get >(this) != MSGPACK_NULLPTR; - } - bool is_vector_char() { - return boost::get >(this) != MSGPACK_NULLPTR; - } - bool is_raw_ref() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_ext() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_ext_ref() const { - return boost::get(this) != MSGPACK_NULLPTR; - } - bool is_vector() const { - return boost::get > >(this) != MSGPACK_NULLPTR; - } - bool is_map() const { - return boost::get, basic_variant > >(this) != MSGPACK_NULLPTR; - } - bool is_multimap() const { - return boost::get, basic_variant > >(this) != MSGPACK_NULLPTR; - } + bool is_vector_char() const { return boost::get>(this) != MSGPACK_NULLPTR; } + bool is_vector_char() { return boost::get>(this) != MSGPACK_NULLPTR; } + bool is_raw_ref() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_ext() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_ext_ref() const { return boost::get(this) != MSGPACK_NULLPTR; } + bool is_vector() const + { + return boost::get>>(this) != MSGPACK_NULLPTR; + } + bool is_map() const + { + return boost::get, basic_variant>>(this) != + MSGPACK_NULLPTR; + } + bool is_multimap() const + { + return boost::get, basic_variant>>(this) != + MSGPACK_NULLPTR; + } - bool as_bool() const { - return boost::get(*this); - } - 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); - } - std::string const& as_string() const { - return boost::get(*this); - } - std::string& as_string() { - return boost::get(*this); - } + bool as_bool() const { return boost::get(*this); } + int64_t as_int64_t() const { return boost::get(*this); } + uint64_t as_uint64_t() const { return boost::get(*this); } + double as_double() const + { + 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); } #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); - } + boost::string_ref const& as_boost_string_ref() const { 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) { - if (v < 0) { - static_cast(*this) = int64_t(v); + std::vector const& as_vector_char() const { 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_ref const& as_ext_ref() const { return boost::get(*this); } + std::vector> const& as_vector() const + { + return boost::get>>(*this); } - else { - static_cast(*this) = uint64_t(v); + std::map, basic_variant> const& as_map() const + { + return boost::get, basic_variant>>(*this); } - } -}; - -template -inline bool operator<(basic_variant const& lhs, basic_variant const& rhs) { - return - static_cast::base const&>(lhs) < - static_cast::base const&>(rhs); -} - -template -inline bool operator==(basic_variant const& lhs, basic_variant const& rhs) { - return - static_cast::base const&>(lhs) == - static_cast::base const&>(rhs); -} - -typedef basic_variant, ext> variant; -typedef basic_variant< -#if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - boost::string_ref, -#else // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - std::string, -#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - raw_ref, ext_ref> variant_ref; - -} // namespace type - -namespace adaptor { - -#if !defined (MSGPACK_USE_CPP03) - -template -struct as > { - type::basic_variant operator()(msgpack::object const& o) const { - switch(o.type) { - case type::NIL: - return o.as(); - case type::BOOLEAN: - return o.as(); - case type::POSITIVE_INTEGER: - return o.as(); - case type::NEGATIVE_INTEGER: - return o.as(); - case type::FLOAT32: - case type::FLOAT64: - return o.as(); - case type::STR: - return o.as(); - case type::BIN: - return o.as(); - case type::EXT: - return o.as(); - case type::ARRAY: - return o.as > >(); - case type::MAP: - return o.as, type::basic_variant > >(); - default: - break; + std::multimap, basic_variant> const& as_multimap() const + { + return boost::get, basic_variant>>(*this); } - return type::basic_variant(); - } -}; - -#endif // !defined (MSGPACK_USE_CPP03) - -template -struct convert > { - msgpack::object const& operator()( - msgpack::object const& o, - type::basic_variant& v) const { - switch(o.type) { - case type::NIL: - v = o.as(); - break; - case type::BOOLEAN: - v = o.as(); - break; - case type::POSITIVE_INTEGER: - v = o.as(); - break; - case type::NEGATIVE_INTEGER: - v = o.as(); - break; - case type::FLOAT32: - case type::FLOAT64: - v = o.as(); - break; - case type::STR: - v = o.as(); - break; - case type::BIN: - v = o.as(); - break; - case type::EXT: - v = o.as(); - break; - case type::ARRAY: - v = o.as > >(); - break; - case type::MAP: - v = o.as, type::basic_variant > >(); - break; - default: - break; + private: + template void int_init(T v) + { + if (v < 0) { + static_cast(*this) = int64_t(v); + } else { + static_cast(*this) = uint64_t(v); + } } - return o; - } -}; - -namespace detail { + 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 -struct pack_imp : boost::static_visitor { - template - void operator()(T const& value) const { - pack()(o_, value); + template + inline bool operator<(basic_variant const& lhs, basic_variant const& rhs) + { + return static_cast::base const&>(lhs) < + static_cast::base const&>(rhs); } - pack_imp(packer& o):o_(o) {} - packer& o_; -}; - -} // namespace detail - -template -struct pack > { - template - msgpack::packer& operator()(msgpack::packer& o, const type::basic_variant& v) const { - boost::apply_visitor(detail::pack_imp(o), v); - return o; - } -}; - -namespace detail { -struct object_imp : boost::static_visitor { - void operator()(msgpack::type::nil_t const& v) const { - object()(o_, v); - } - void operator()(bool const& v) const { - object()(o_, v); + template + inline bool operator==(basic_variant const& lhs, basic_variant const& rhs) + { + return static_cast::base const&>(lhs) == + static_cast::base const&>(rhs); } - void operator()(uint64_t const& v) const { - object()(o_, v); - } - void operator()(int64_t const& v) const { - object()(o_, v); - } - void operator()(double const& v) const { - object()(o_, v); - } - template - void operator()(T const&) const { - THROW msgpack::type_error(); - } - object_imp(msgpack::object& o):o_(o) {} - msgpack::object& o_; -}; -} // namespace detail + typedef basic_variant, ext> variant; + typedef basic_variant< +#if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + boost::string_ref, +#else // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + std::string, +#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + raw_ref, + ext_ref> + variant_ref; + + } // namespace type + + namespace adaptor { + +#if !defined(MSGPACK_USE_CPP03) + + template struct as> { + type::basic_variant operator()(msgpack::object const& o) const + { + switch (o.type) { + case type::NIL: + return o.as(); + case type::BOOLEAN: + return o.as(); + case type::POSITIVE_INTEGER: + return o.as(); + case type::NEGATIVE_INTEGER: + return o.as(); + case type::FLOAT32: + case type::FLOAT64: + return o.as(); + case type::STR: + return o.as(); + case type::BIN: + return o.as(); + case type::EXT: + return o.as(); + case type::ARRAY: + return o.as>>(); + case type::MAP: + return o.as, type::basic_variant>>(); + default: + break; + } + return type::basic_variant(); + } + }; -template -struct object > { - void operator()(msgpack::object& o, const type::basic_variant& v) const { - boost::apply_visitor(detail::object_imp(o), v); - } -}; +#endif // !defined (MSGPACK_USE_CPP03) + + template struct convert> { + msgpack::object const& operator()(msgpack::object const& o, type::basic_variant& v) const + { + switch (o.type) { + case type::NIL: + v = o.as(); + break; + case type::BOOLEAN: + v = o.as(); + break; + case type::POSITIVE_INTEGER: + v = o.as(); + break; + case type::NEGATIVE_INTEGER: + v = o.as(); + break; + case type::FLOAT32: + case type::FLOAT64: + v = o.as(); + break; + case type::STR: + v = o.as(); + break; + case type::BIN: + v = o.as(); + break; + case type::EXT: + v = o.as(); + break; + case type::ARRAY: + v = o.as>>(); + break; + case type::MAP: + v = o.as, type::basic_variant>>(); + break; + default: + break; + } + return o; + } + }; + + namespace detail { + + template struct pack_imp : boost::static_visitor { + template void operator()(T const& value) const { pack()(o_, value); } + pack_imp(packer& o) + : o_(o) + {} + packer& o_; + }; + + } // namespace detail + + template struct pack> { + template + msgpack::packer& operator()(msgpack::packer& o, + const type::basic_variant& v) const + { + boost::apply_visitor(detail::pack_imp(o), v); + return o; + } + }; + + namespace detail { + + struct object_imp : boost::static_visitor { + void operator()(msgpack::type::nil_t const& v) const { object()(o_, v); } + void operator()(bool const& v) const { object()(o_, v); } + void operator()(uint64_t const& v) const { object()(o_, v); } + void operator()(int64_t const& v) const { object()(o_, v); } + void operator()(double const& v) const { object()(o_, v); } + template void operator()(T const&) const { THROW msgpack::type_error(); } + object_imp(msgpack::object& o) + : o_(o) + {} + msgpack::object& o_; + }; + + } // namespace detail + + template struct object> { + void operator()(msgpack::object& o, const type::basic_variant& v) const + { + boost::apply_visitor(detail::object_imp(o), v); + } + }; -namespace detail { + namespace detail { -struct object_with_zone_imp : boost::static_visitor { - template - void operator()(T const& v) const { - object_with_zone()(o_, v); - } - object_with_zone_imp(msgpack::object::with_zone& o):o_(o) {} - msgpack::object::with_zone& o_; -}; + struct object_with_zone_imp : boost::static_visitor { + template void operator()(T const& v) const { object_with_zone()(o_, v); } + object_with_zone_imp(msgpack::object::with_zone& o) + : o_(o) + {} + msgpack::object::with_zone& o_; + }; -} // namespace detail + } // namespace detail -template -struct object_with_zone > { - void operator()(msgpack::object::with_zone& o, const type::basic_variant& v) const { - boost::apply_visitor(detail::object_with_zone_imp(o), v); - } -}; + template struct object_with_zone> { + void operator()(msgpack::object::with_zone& o, const type::basic_variant& v) const + { + boost::apply_visitor(detail::object_with_zone_imp(o), v); + } + }; -} // namespace adaptor + } // namespace adaptor -/// @cond + /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp index 9607022daca6..d94524ad8c5b 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp @@ -11,245 +11,321 @@ #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 "msgpack/object.hpp" +#include "msgpack/versioning.hpp" #include - -#include +#include namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v1) { -/// @endcond +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + /// @endcond -namespace adaptor { - -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(); } - std::chrono::time_point tp; - switch(o.via.ext.size) { - case 4: { - uint32_t sec; - _msgpack_load32(uint32_t, o.via.ext.data(), &sec); - tp += std::chrono::seconds(sec); - } break; - case 8: { - uint64_t value; - _msgpack_load64(uint64_t, o.via.ext.data(), &value); - uint32_t nanosec = boost::numeric_cast(value >> 34); - uint64_t sec = value & 0x00000003ffffffffLL; - tp += std::chrono::duration_cast( - std::chrono::nanoseconds(nanosec)); - tp += std::chrono::seconds(sec); - } break; - case 12: { - uint32_t nanosec; - _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec); - int64_t sec; - _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec); - - if (sec > 0) { - tp += std::chrono::seconds(sec); - tp += std::chrono::duration_cast( - std::chrono::nanoseconds(nanosec)); + namespace adaptor { + + namespace detail { + template ::value, + bool source_is_signed = std::is_signed::value, + typename = + typename std::enable_if::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 ::value, + bool source_is_signed = std::is_signed::value, + typename = + typename std::enable_if::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::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(); } - else { - if (nanosec == 0) { - tp += std::chrono::seconds(sec); - } - else { - ++sec; + if (o.via.ext.type() != -1) { + THROW msgpack::type_error(); + } + std::chrono::time_point tp; + switch (o.via.ext.size) { + case 4: { + uint32_t sec; + _msgpack_load32(uint32_t, o.via.ext.data(), &sec); + tp += std::chrono::seconds(sec); + } break; + case 8: { + uint64_t value; + _msgpack_load64(uint64_t, o.via.ext.data(), &value); + uint32_t nanosec = detail::integral_cast(value >> 34); + uint64_t sec = value & 0x00000003ffffffffLL; + tp += std::chrono::duration_cast(std::chrono::nanoseconds(nanosec)); + tp += std::chrono::seconds(sec); + } break; + case 12: { + uint32_t nanosec; + _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec); + int64_t sec; + _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec); + + if (sec > 0) { tp += std::chrono::seconds(sec); - int64_t ns = boost::numeric_cast(nanosec) - 1000000000L; - tp += std::chrono::duration_cast( - std::chrono::nanoseconds(ns)); + tp += std::chrono::duration_cast(std::chrono::nanoseconds(nanosec)); + } else { + if (nanosec == 0) { + tp += std::chrono::seconds(sec); + } else { + ++sec; + tp += std::chrono::seconds(sec); + int64_t ns = detail::integral_cast(nanosec) - 1000000000L; + tp += std::chrono::duration_cast(std::chrono::nanoseconds(ns)); + } } + } break; + default: + THROW msgpack::type_error(); } - } break; - default: - THROW msgpack::type_error(); + return tp; } - return tp; - } -}; - -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(); } - std::chrono::time_point tp; - switch(o.via.ext.size) { - case 4: { - uint32_t sec; - _msgpack_load32(uint32_t, o.via.ext.data(), &sec); - tp += std::chrono::seconds(sec); - v = tp; - } break; - case 8: { - uint64_t value; - _msgpack_load64(uint64_t, o.via.ext.data(), &value); - uint32_t nanosec = boost::numeric_cast(value >> 34); - uint64_t sec = value & 0x00000003ffffffffLL; - tp += std::chrono::duration_cast( - std::chrono::nanoseconds(nanosec)); - tp += std::chrono::seconds(sec); - v = tp; - } break; - case 12: { - uint32_t nanosec; - _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec); - int64_t sec; - _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec); - - if (sec > 0) { - tp += std::chrono::seconds(sec); - tp += std::chrono::duration_cast( - std::chrono::nanoseconds(nanosec)); + }; + + 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(); } - else { - if (nanosec == 0) { - tp += std::chrono::seconds(sec); - } - else { - ++sec; + if (o.via.ext.type() != -1) { + THROW msgpack::type_error(); + } + std::chrono::time_point tp; + switch (o.via.ext.size) { + case 4: { + uint32_t sec; + _msgpack_load32(uint32_t, o.via.ext.data(), &sec); + tp += std::chrono::seconds(sec); + v = tp; + } break; + case 8: { + uint64_t value; + _msgpack_load64(uint64_t, o.via.ext.data(), &value); + uint32_t nanosec = detail::integral_cast(value >> 34); + uint64_t sec = value & 0x00000003ffffffffLL; + tp += std::chrono::duration_cast(std::chrono::nanoseconds(nanosec)); + tp += std::chrono::seconds(sec); + v = tp; + } break; + case 12: { + uint32_t nanosec; + _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec); + int64_t sec; + _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec); + + if (sec > 0) { tp += std::chrono::seconds(sec); - int64_t ns = boost::numeric_cast(nanosec) - 1000000000L; - tp += std::chrono::duration_cast( - std::chrono::nanoseconds(ns)); + tp += std::chrono::duration_cast(std::chrono::nanoseconds(nanosec)); + } else { + if (nanosec == 0) { + tp += std::chrono::seconds(sec); + } else { + ++sec; + tp += std::chrono::seconds(sec); + int64_t ns = detail::integral_cast(nanosec) - 1000000000L; + tp += std::chrono::duration_cast(std::chrono::nanoseconds(ns)); + } } - } - v = tp; - } break; - default: - THROW msgpack::type_error(); - } - return o; - } -}; - -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 nano_num = - Duration::period::ratio::num * - (1000000000L / Duration::period::ratio::den); - - int64_t nanosec = count % (1000000000L / nano_num) * nano_num; - int64_t sec = 0; - if (nanosec < 0) { - nanosec = 1000000000L + nanosec; - --sec; + v = tp; + } break; + default: + THROW msgpack::type_error(); + } + return o; } - sec += count - * Duration::period::ratio::num - / Duration::period::ratio::den; - - if ((sec >> 34) == 0) { - uint64_t data64 = (boost::numeric_cast(nanosec) << 34) | boost::numeric_cast(sec); - if ((data64 & 0xffffffff00000000L) == 0) { - // timestamp 32 - o.pack_ext(4, -1); - uint32_t data32 = boost::numeric_cast(data64); - char buf[4]; - _msgpack_store32(buf, data32); - o.pack_ext_body(buf, 4); + }; + + template struct pack> { + template + msgpack::packer& operator()(msgpack::packer& o, + std::chrono::time_point const& v) const + { + int64_t count = detail::integral_cast(v.time_since_epoch().count()); + int64_t nano_num = Duration::period::ratio::num * (1000000000L / Duration::period::ratio::den); + + int64_t nanosec = count % (1000000000L / nano_num) * nano_num; + int64_t sec = 0; + if (nanosec < 0) { + nanosec = 1000000000L + nanosec; + --sec; } - else { - // timestamp 64 - o.pack_ext(8, -1); - char buf[8]; - _msgpack_store64(buf, data64); - o.pack_ext_body(buf, 8); + sec += count * Duration::period::ratio::num / Duration::period::ratio::den; + + if ((sec >> 34) == 0) { + 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 = detail::integral_cast(data64); + char buf[4]; + _msgpack_store32(buf, data32); + o.pack_ext_body(buf, 4); + } else { + // timestamp 64 + o.pack_ext(8, -1); + char buf[8]; + _msgpack_store64(buf, data64); + o.pack_ext_body(buf, 8); + } + } else { + // timestamp 96 + o.pack_ext(12, -1); + char buf[12]; + + _msgpack_store32(&buf[0], detail::integral_cast(nanosec)); + _msgpack_store64(&buf[4], sec); + o.pack_ext_body(buf, 12); } + return o; } - else { - // timestamp 96 - o.pack_ext(12, -1); - char buf[12]; + }; + template struct object_with_zone> { + void operator()(msgpack::object::with_zone& o, const std::chrono::time_point& v) const + { + int64_t count = detail::integral_cast(v.time_since_epoch().count()); - _msgpack_store32(&buf[0], boost::numeric_cast(nanosec)); - _msgpack_store64(&buf[4], sec); - o.pack_ext_body(buf, 12); - } - return o; - } -}; - -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 nano_num = - Duration::period::ratio::num * - (1000000000L / Duration::period::ratio::den); - - int64_t nanosec = count % (1000000000L / nano_num) * nano_num; - int64_t sec = 0; - if (nanosec < 0) { - nanosec = 1000000000L + nanosec; - --sec; - } - sec += count - * Duration::period::ratio::num - / Duration::period::ratio::den; - if ((sec >> 34) == 0) { - uint64_t data64 = (boost::numeric_cast(nanosec) << 34) | boost::numeric_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); - _msgpack_store32(&p[1], data32); - o.via.ext.ptr = p; + int64_t nano_num = Duration::period::ratio::num * (1000000000L / Duration::period::ratio::den); + + int64_t nanosec = count % (1000000000L / nano_num) * nano_num; + int64_t sec = 0; + if (nanosec < 0) { + nanosec = 1000000000L + nanosec; + --sec; } - else { - // timestamp 64 + sec += count * Duration::period::ratio::num / Duration::period::ratio::den; + if ((sec >> 34) == 0) { + 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 = detail::integral_cast(data64); + _msgpack_store32(&p[1], data32); + o.via.ext.ptr = p; + } else { + // timestamp 64 + o.type = msgpack::type::EXT; + o.via.ext.size = 8; + char* p = static_cast(o.zone.allocate_no_align(o.via.ext.size + 1)); + p[0] = static_cast(-1); + _msgpack_store64(&p[1], data64); + o.via.ext.ptr = p; + } + } else { + // timestamp 96 o.type = msgpack::type::EXT; - o.via.ext.size = 8; + 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_store64(&p[1], data64); + _msgpack_store32(&p[1], detail::integral_cast(nanosec)); + _msgpack_store64(&p[1 + 4], sec); o.via.ext.ptr = p; } } - else { - // timestamp 96 - o.type = msgpack::type::EXT; - 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_store64(&p[1 + 4], sec); - o.via.ext.ptr = p; - } - } -}; + }; -} // namespace adaptor + } // namespace adaptor -/// @cond + /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond } // namespace msgpack -#endif // !defined(MSGPACK_NO_BOOST) - #endif // MSGPACK_V1_TYPE_CPP11_CHRONO_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp17/variant.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp17/variant.hpp new file mode 100644 index 000000000000..be98e7233eb0 --- /dev/null +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp17/variant.hpp @@ -0,0 +1,119 @@ +// +// 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 + 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/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp index 75ad3af3f867..0a7246d5ac75 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp @@ -10,664 +10,877 @@ #ifndef MSGPACK_V1_CPP03_ZONE_HPP #define MSGPACK_V1_CPP03_ZONE_HPP -#include "msgpack/versioning.hpp" +#include "msgpack/assert.hpp" #include "msgpack/cpp_config.hpp" +#include "msgpack/versioning.hpp" #include "msgpack/zone_decl.hpp" -#include "msgpack/assert.hpp" -#include #include #include +#include #include - namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v1) { -/// @endcond - -class zone { - struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} - void operator()() { m_func(m_data); } - void (*m_func)(void*); - void* m_data; - }; - 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() { - call(); - ::free(m_array); - } - void clear() { - call(); - m_tail = m_array; - } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + /// @endcond - if(fin == m_end) { - push_expand(func, data); - return; + class zone { + struct finalizer { + 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_head(MSGPACK_NULLPTR) + {} + + ~finalizer_array() { clear(); } + + 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; + void push(void (*func)(void* data), void* data) { m_head = new finalizer(func, data, m_head); } - ++m_tail; - } - 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; + void pop() + { + finalizer* n = m_head->m_next; + delete m_head; + m_head = n; } - 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; - } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_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(); + 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, 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; } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { - chunk* c = m_head; - while(c) { - chunk* n = c->m_next; - ::free(c); - c = n; - } - } - void clear(size_t chunk_size) - { - chunk* c = m_head; - while(true) { - chunk* n = c->m_next; - if(n) { + void clear(size_t chunk_size, char* ptr) + { + chunk* c = m_head; + while (c) { + chunk* n = c->m_next; ::free(c); c = n; - } else { - m_head = c; - break; } + m_head = MSGPACK_NULLPTR; + m_free = chunk_size; + m_ptr = ptr; } - m_head->m_next = MSGPACK_NULLPTR; - m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); - } - size_t m_free; - char* m_ptr; - chunk* m_head; - }; - size_t m_chunk_size; - chunk_list m_chunk_list; - finalizer_array m_finalizer_array; -public: - zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + size_t m_free; + char* m_ptr; + chunk* m_head; -public: - void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); - void* allocate_no_align(size_t size); + private: + chunk_list(const chunk_list&); + chunk_list& operator=(const chunk_list&); + }; - void push_finalizer(void (*func)(void*), void* data); + size_t m_chunk_size; + chunk_list* m_chunk_list; + finalizer_array m_finalizer_array; - template - void push_finalizer(msgpack::unique_ptr obj); + public: + zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); - void clear(); + void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); - void swap(zone& o); - 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() */ - { - ::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); - } - /// @cond - - template - T* allocate(); - - template - T* allocate(A1 a1); - - template - T* allocate(A1 a1, A2 a2); - - template - T* allocate(A1 a1, A2 a2, A3 a3); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14); - - template - T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14, A15 a15); - - /// @endcond + void* allocate_no_align(size_t size); -private: - void undo_allocate(size_t size); + bool allocated() { return m_chunk_list != MSGPACK_NULLPTR; } - template - static void object_destruct(void* obj); + void push_finalizer(void (*func)(void*), void* data); - template - static void object_delete(void* obj); + template void push_finalizer(msgpack::unique_ptr obj); - static char* get_aligned(char* ptr, size_t align); + void clear(); - char* allocate_expand(size_t size); -private: - zone(const zone&); - zone& operator=(const zone&); -}; + void swap(zone& o); -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ -} - -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( - reinterpret_cast(ptr + (align - 1)) & ~static_cast(align - 1) - ); -} - -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) { - 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); - } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.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) { - ptr = allocate_expand(size); - } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; - - return ptr; -} + static void* operator new(std::size_t size) + { + void* p = ::malloc(size); + if (!p) + throw std::bad_alloc(); + return p; + } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; + static void operator delete(void* p) /* throw() */ { ::free(p); } + + static void* operator new(std::size_t /*size*/, void* mem) /* throw() */ { return mem; } + + static void operator delete(void* /*p*/, void* /*mem*/) /* throw() */ {} + + /// @cond + + template T* allocate(); + + template T* allocate(A1 a1); + + template T* allocate(A1 a1, A2 a2); + + template T* allocate(A1 a1, A2 a2, A3 a3); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12); + + template + T* allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13); + + template + T* allocate( + A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14); + + template + T* allocate(A1 a1, + A2 a2, + A3 a3, + A4 a4, + A5 a5, + A6 a6, + A7 a7, + A8 a8, + A9 a9, + A10 a10, + A11 a11, + A12 a12, + A13 a13, + A14 a14, + A15 a15); + + /// @endcond + + private: + void undo_allocate(size_t size); + + template static void object_destruct(void* obj); + + template static void object_delete(void* obj); + + 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&); + }; - size_t sz = m_chunk_size; + inline zone::zone(size_t chunk_size) + : m_chunk_size(chunk_size) + , m_chunk_list(MSGPACK_NULLPTR) + {} - while(sz < size) { - size_t tmp_sz = sz * 2; - if (tmp_sz <= sz) { - sz = size; - break; + 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; } - sz = tmp_sz; } - chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); - 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; - - return ptr; -} + 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(reinterpret_cast(ptr + (align - 1)) & + ~static_cast(align - 1)); + } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ - m_finalizer_array.push(func, data); -} + 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; + } -template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ - m_finalizer_array.push(&zone::object_delete, obj.release()); -} + 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 - chank_lst.m_ptr); + } + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; + return aligned; + } -inline void zone::clear() -{ - m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_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); + } + chank_lst.m_free -= size; + chank_lst.m_ptr += size; -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); -} - -template -void zone::object_destruct(void* obj) -{ - static_cast(obj)->~T(); -} + return ptr; + } -template -void zone::object_delete(void* obj) -{ - delete static_cast(obj); -} + inline char* zone::allocate_expand(size_t size) + { + chunk_list& cl = get_chank_lst(); + size_t sz = m_chunk_size; + + while (sz < size) { + size_t tmp_sz = sz * 2; + if (tmp_sz <= sz) { + sz = size; + break; + } + sz = tmp_sz; + } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; -} + chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); + if (!c) + throw std::bad_alloc(); -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { - return (size + align - 1) / align * align; -} + char* ptr = reinterpret_cast(c) + sizeof(chunk); -/// @cond + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; -template -T* zone::allocate() -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + return ptr; } -} -template -T* zone::allocate(A1 a1) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(a1); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + inline void zone::push_finalizer(void (*func)(void*), void* data) + { + m_finalizer_array.push(func, data); } -} -template -T* zone::allocate(A1 a1, A2 a2) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(a1, a2); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + template inline void zone::push_finalizer(msgpack::unique_ptr obj) + { + m_finalizer_array.push(&zone::object_delete, obj.release()); } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(a1, a2, a3); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + inline void zone::clear() + { + m_finalizer_array.clear(); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(a1, a2, a3, a4); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + 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.m_head, o.m_finalizer_array.m_head); } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(a1, a2, a3, a4, a5); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + template void zone::object_delete(void* obj) + { + delete static_cast(obj); } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; - } - try { - return new (x) T(a1, a2, a3, a4, a5, a6); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + template void zone::object_destruct(void* obj) + { + static_cast(obj)->~T(); } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + inline void zone::undo_allocate(size_t size) + { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + inline std::size_t aligned_size(std::size_t size, std::size_t align) + { + return (size + align - 1) / align * align; } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + /// @cond + + template T* zone::allocate() + { + 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; + } + try { + return new (x) T(); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + template T* zone::allocate(A1 a1) + { + 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; + } + try { + return new (x) T(a1); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template T* zone::allocate(A1 a1, A2 a2) + { + 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; + } + try { + return new (x) T(a1, a2); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + template T* zone::allocate(A1 a1, A2 a2, A3 a3) + { + 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; + } + try { + return new (x) T(a1, a2, a3); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - try { - return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - 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; - undo_allocate(sizeof(T)); - RETHROW; + + template + T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -template -T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14, A15 a15) -{ - void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); - try { - m_finalizer_array.push(&zone::object_destruct, x); - } catch (...) { - undo_allocate(sizeof(T)); - RETHROW; + template + T* zone::allocate( + A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } - 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; - undo_allocate(sizeof(T)); - RETHROW; + + template + T* zone::allocate(A1 a1, + A2 a2, + A3 a3, + A4 a4, + A5 a5, + A6 a6, + A7 a7, + A8 a8, + A9 a9, + A10 a10, + A11 a11, + A12 a12, + A13 a13, + A14 a14, + A15 a15) + { + 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; + } + try { + return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } } -} -/// @endcond + /// @endcond -/// @cond -} // MSGPACK_API_VERSION_NAMESPACE(v1) + /// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond -} // namespace msgpack +} // namespace msgpack #endif // MSGPACK_V1_CPP03_ZONE_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp index 0a0658e69da9..e475798c34ea 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp @@ -10,10 +10,10 @@ #ifndef MSGPACK_CPP11_ZONE_HPP #define MSGPACK_CPP11_ZONE_HPP -#include "msgpack/versioning.hpp" +#include "msgpack/assert.hpp" #include "msgpack/cpp_config.hpp" +#include "msgpack/versioning.hpp" #include "msgpack/zone_decl.hpp" -#include "msgpack/assert.hpp" #include #include @@ -23,347 +23,331 @@ namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v1) { -/// @endcond - -class zone { -private: - struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} - void operator()() { m_func(m_data); } - void (*m_func)(void*); - void* m_data; - }; - 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() { - call(); - ::free(m_array); - } - 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; +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + /// @endcond + + class zone { + private: + struct finalizer { + 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_head(MSGPACK_NULLPTR) + {} + + ~finalizer_array() { clear(); } + + 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; + void push(void (*func)(void* data), void* data) { m_head = new finalizer(func, data, m_head); } - ++m_tail; - } - 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(); + void pop() + { + auto n = m_head->m_next; + delete m_head; + m_head = n; } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - - ++m_tail; - } - 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& operator=(finalizer_array&& other) noexcept - { - this->~finalizer_array(); - new (this) finalizer_array(std::move(other)); - return *this; - } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; + finalizer_array(finalizer_array&& other) noexcept + : m_head(other.m_head) + { + other.m_head = MSGPACK_NULLPTR; + } - 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(); + finalizer_array& operator=(finalizer_array&& other) noexcept + { + m_head = other.m_head; + other.m_head = MSGPACK_NULLPTR; + return *this; } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { - chunk* c = m_head; - while(c) { - chunk* n = c->m_next; - ::free(c); - c = n; + 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, 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) - { - chunk* c = m_head; - while(true) { - chunk* n = c->m_next; - if(n) { + + void clear(size_t chunk_size, char* ptr) + { + chunk* c = m_head; + while (c) { + chunk* n = c->m_next; ::free(c); c = n; - } else { - m_head = c; - break; } + m_head = MSGPACK_NULLPTR; + m_free = chunk_size; + m_ptr = ptr; } - m_head->m_next = 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; - } - 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; - finalizer_array m_finalizer_array; + size_t m_free; + char* m_ptr; + chunk* m_head; -public: - zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + 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&); + }; -public: - void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); - void* allocate_no_align(size_t size); + size_t m_chunk_size; + chunk_list* m_chunk_list{}; + finalizer_array m_finalizer_array; - void push_finalizer(void (*func)(void*), void* data); + public: + zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); - template - void push_finalizer(msgpack::unique_ptr obj); + void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); - void clear(); + void* allocate_no_align(size_t size); - void swap(zone& o); + bool allocated() { return m_chunk_list != MSGPACK_NULLPTR; } - 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) noexcept - { - ::free(p); - } - static void* operator new(std::size_t /*size*/, void* mem) noexcept - { - return mem; - } - static void operator delete(void * /*p*/, void* /*mem*/) noexcept - { - } + void push_finalizer(void (*func)(void*), void* data); - template - T* allocate(Args... args); + template void push_finalizer(msgpack::unique_ptr obj); - zone(zone&&) = default; - zone& operator=(zone&&) = default; - zone(const zone&) = delete; - zone& operator=(const zone&) = delete; + void clear(); -private: - void undo_allocate(size_t size); + void swap(zone& o); - template - static void object_destruct(void* obj); + static void* operator new(std::size_t size) + { + void* p = ::malloc(size); + if (!p) + throw std::bad_alloc(); + return p; + } - template - static void object_delete(void* obj); + static void operator delete(void* p) noexcept { ::free(p); } - static char* get_aligned(char* ptr, size_t align); + static void* operator new(std::size_t /*size*/, void* mem) noexcept { return mem; } - char* allocate_expand(size_t size); -}; + static void operator delete(void* /*p*/, void* /*mem*/) noexcept {} -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ -} + template T* allocate(Args... args); -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( - reinterpret_cast(ptr + (align - 1)) & ~static_cast(align - 1) - ); -} - -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) { - 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); - } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; - return aligned; -} + zone(zone&&) = default; + zone& operator=(zone&&) = default; + zone(const zone&) = delete; + zone& operator=(const zone&) = delete; -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { - ptr = allocate_expand(size); - } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + private: + void undo_allocate(size_t size); - return ptr; -} + template static void object_destruct(void* obj); -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; + template static void object_delete(void* obj); - size_t sz = m_chunk_size; + static char* get_aligned(char* ptr, size_t align); - while(sz < size) { - size_t tmp_sz = sz * 2; - if (tmp_sz <= sz) { - sz = size; - break; + 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(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; } - sz = tmp_sz; } - chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); - if (!c) THROW std::bad_alloc(); + 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(reinterpret_cast(ptr + (align - 1)) & + ~static_cast(align - 1)); + } - char* ptr = reinterpret_cast(c) + sizeof(chunk); + 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; + } - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + 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 - chank_lst.m_ptr); + } + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; + return aligned; + } - return ptr; -} + 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); + } + chank_lst.m_free -= size; + chank_lst.m_ptr += size; -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ - m_finalizer_array.push(func, data); -} + return ptr; + } -template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ - m_finalizer_array.push(&zone::object_delete, obj.release()); -} + inline char* zone::allocate_expand(size_t size) + { + chunk_list& cl = get_chank_lst(); + size_t sz = m_chunk_size; + + while (sz < size) { + size_t tmp_sz = sz * 2; + if (tmp_sz <= sz) { + sz = size; + break; + } + sz = tmp_sz; + } -inline void zone::clear() -{ - m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); -} + chunk* c = static_cast(::malloc(sizeof(chunk) + sz)); + if (!c) + throw std::bad_alloc(); -inline void zone::swap(zone& o) -{ - std::swap(*this, o); -} + char* ptr = reinterpret_cast(c) + sizeof(chunk); -template -void zone::object_delete(void* obj) -{ - delete static_cast(obj); -} + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; -template -void zone::object_destruct(void* obj) -{ - static_cast(obj)->~T(); -} + return ptr; + } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; -} + 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) + { + m_finalizer_array.push(&zone::object_delete, obj.release()); + } -template -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)); - RETHROW; + inline void zone::clear() + { + m_finalizer_array.clear(); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } - try { - return new (x) T(args...); - } catch (...) { - --m_finalizer_array.m_tail; - undo_allocate(sizeof(T)); - RETHROW; + + inline void zone::swap(zone & o) + { + std::swap(*this, o); } -} -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { - return (size + align - 1) / align * align; -} + template void zone::object_delete(void* obj) + { + delete static_cast(obj); + } -/// @cond -} // MSGPACK_API_VERSION_NAMESPACE(v1) + template void zone::object_destruct(void* obj) + { + static_cast(obj)->~T(); + } + + 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) + { + 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; + } + try { + return new (x) T(args...); + } catch (...) { + m_finalizer_array.pop(); + undo_allocate(sizeof(T)); + throw; + } + } + + /// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond -} // namespace msgpack +} // namespace msgpack #endif // MSGPACK_CPP11_ZONE_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp index 2562b41642b7..83a9880c55ac 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp @@ -16,232 +16,216 @@ namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v1) { -/// @endcond - -struct object_array { - uint32_t size; - msgpack::object* ptr; -}; +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + /// @endcond -struct object_map { - uint32_t size; - msgpack::object_kv* ptr; -}; + struct object_array { + uint32_t size; + msgpack::object* ptr; + }; -struct object_str { - uint32_t size; - const char* ptr; -}; + struct object_map { + uint32_t size; + msgpack::object_kv* ptr; + }; -struct object_bin { - uint32_t size; - const char* ptr; -}; + struct object_str { + uint32_t size; + const char* ptr; + }; -struct object_ext { - int8_t type() const { return static_cast(ptr[0]); } - const char* data() const { return &ptr[1]; } - uint32_t size; - const char* ptr; -}; + struct object_bin { + uint32_t size; + const char* ptr; + }; + struct object_ext { + int8_t type() const { return static_cast(ptr[0]); } + const char* data() const { return &ptr[1]; } + uint32_t size; + const char* ptr; + }; #if !defined(MSGPACK_USE_CPP03) -template -struct has_as { -private: - template - static auto check(U*) -> - // Check v1 specialization - typename std::is_same< - decltype(adaptor::as()(std::declval())), - T - >::type; - template - static std::false_type check(...); -public: - using type = decltype(check(MSGPACK_NULLPTR)); - static constexpr bool value = type::value; -}; - -#endif // !defined(MSGPACK_USE_CPP03) - -/// Object class that corresponding to MessagePack format object -/** - * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object - */ -struct object { - union union_type { - bool boolean; - uint64_t u64; - int64_t i64; -#if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT) - MSGPACK_DEPRECATED("please use f64 instead") - double dec; // obsolete -#endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT - double f64; - msgpack::object_array array; - msgpack::object_map map; - msgpack::object_str str; - msgpack::object_bin bin; - msgpack::object_ext ext; + template struct has_as { + private: + template + static auto check_(U*) -> + // Check v1 specialization + typename std::is_same()(std::declval())), T>::type; + template static std::false_type check_(...); + + public: + using type = decltype(check_(MSGPACK_NULLPTR)); + static constexpr bool value = type::value; }; - msgpack::type::object_type type; - union_type via; +#endif // !defined(MSGPACK_USE_CPP03) - /// Cheking nil + /// Object class that corresponding to MessagePack format object /** - * @return If the object is nil, then return true, else return false. + * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object */ - bool is_nil() const { return type == msgpack::type::NIL; } + struct object { + union union_type { + bool boolean; + uint64_t u64; + int64_t i64; +#if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT) + MSGPACK_DEPRECATED("please use f64 instead") + double dec; // obsolete +#endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT + double f64; + msgpack::object_array array; + msgpack::object_map map; + msgpack::object_str str; + msgpack::object_bin bin; + msgpack::object_ext ext; + }; + + msgpack::type::object_type type; + union_type via; + + /// Cheking nil + /** + * @return If the object is nil, then return true, else return false. + */ + bool is_nil() const { return type == msgpack::type::NIL; } #if defined(MSGPACK_USE_CPP03) - /// Get value as T - /** - * If the object can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type you want to get. - * @return The converted object. - */ - template - T as() const; - -#else // defined(MSGPACK_USE_CPP03) - - /// Get value as T - /** - * If the object can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type you want to get. - * @return The converted object. - */ - template - typename std::enable_if::value, T>::type as() const; - - /// Get value as T - /** - * If the object can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type you want to get. - * @return The converted object. - */ - template - typename std::enable_if::value, T>::type as() const; + /// Get value as T + /** + * If the object can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type you want to get. + * @return The converted object. + */ + template T as() const; + +#else // defined(MSGPACK_USE_CPP03) + + /// Get value as T + /** + * If the object can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type you want to get. + * @return The converted object. + */ + template typename std::enable_if::value, T>::type as() const; + + /// Get value as T + /** + * If the object can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type you want to get. + * @return The converted object. + */ + template typename std::enable_if::value, T>::type as() const; #endif // defined(MSGPACK_USE_CPP03) - /// Convert the object - /** - * If the object can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type of v. - * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object. - * @return The reference of `v`. - */ - template - typename msgpack::enable_if< - !msgpack::is_array::value && !msgpack::is_pointer::value, - T& - >::type - convert(T& v) const; - - template - T (&convert(T(&v)[N]) const)[N]; + /// Convert the object + /** + * If the object can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type of v. + * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the + * object. + * @return The reference of `v`. + */ + template + typename msgpack::enable_if::value && !msgpack::is_pointer::value, T&>::type convert( + T& v) const; + template T (&convert(T (&v)[N]) const)[N]; #if !defined(MSGPACK_DISABLE_LEGACY_CONVERT) - /// Convert the object (obsolete) - /** - * If the object can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type of v. - * @param v The pointer of the value you want to get. `v` is output parameter. `*v` is overwritten by converted value from the object. - * @return The pointer of `v`. - */ - template - MSGPACK_DEPRECATED("please use reference version instead") - typename msgpack::enable_if< - msgpack::is_pointer::value, - T - >::type - convert(T v) const; + /// Convert the object (obsolete) + /** + * If the object can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type of v. + * @param v The pointer of the value you want to get. `v` is output parameter. `*v` is overwritten by converted + * value from the object. + * @return The pointer of `v`. + */ + template + MSGPACK_DEPRECATED("please use reference version instead") + typename msgpack::enable_if::value, T>::type convert(T v) const; #endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT) - /// Convert the object if not nil - /** - * If the object is not nil and can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type of v. - * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object if the object is not nil. - * @return If the object is nil, then return false, else return true. - */ - template - bool convert_if_not_nil(T& v) const; - - /// Default constructor. The object is set to nil. - object(); - - /// Construct object from T - /** - * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map, - * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor. - * - * @tparam T The type of `v`. - * @param v The value you want to convert. - */ - template - explicit object(const T& v); - - /// Construct object from T - /** - * The object is constructed on the zone `z`. - * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object - * - * @tparam T The type of `v`. - * @param v The value you want to convert. - * @param z The zone that is used by the object. - */ - template - object(const T& v, msgpack::zone& z); - - /// Construct object from T (obsolete) - /** - * The object is constructed on the zone `z`. - * Use `object(const T& v, msgpack::zone& z)` instead of this constructor. - * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object - * - * @tparam T The type of `v`. - * @param v The value you want to convert. - * @param z The pointer to the zone that is used by the object. - */ - template - MSGPACK_DEPRECATED("please use zone reference version instead of the pointer version") - object(const T& v, msgpack::zone* z); - - template - object& operator=(const T& v); - - struct with_zone; - -protected: - struct implicit_type; - -public: - implicit_type convert() const; -}; + /// Convert the object if not nil + /** + * If the object is not nil and can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type of v. + * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the + * object if the object is not nil. + * @return If the object is nil, then return false, else return true. + */ + template bool convert_if_not_nil(T& v) const; + + /// Default constructor. The object is set to nil. + object(); + + /// Construct object from T + /** + * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map, + * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor. + * + * @tparam T The type of `v`. + * @param v The value you want to convert. + */ + template explicit object(const T& v); + + /// Construct object from T + /** + * The object is constructed on the zone `z`. + * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object + * + * @tparam T The type of `v`. + * @param v The value you want to convert. + * @param z The zone that is used by the object. + */ + template object(const T& v, msgpack::zone& z); + + /// Construct object from T (obsolete) + /** + * The object is constructed on the zone `z`. + * Use `object(const T& v, msgpack::zone& z)` instead of this constructor. + * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object + * + * @tparam T The type of `v`. + * @param v The value you want to convert. + * @param z The pointer to the zone that is used by the object. + */ + template + MSGPACK_DEPRECATED("please use zone reference version instead of the pointer version") + object(const T& v, msgpack::zone* z); + + template object& operator=(const T& v); + + struct with_zone; + + protected: + struct implicit_type; + + public: + implicit_type convert() const; + }; -class type_error : public std::bad_cast { }; + class type_error : public std::bad_cast {}; -struct object::implicit_type { - implicit_type(object const& o) : obj(o) { } - ~implicit_type() { } + struct object::implicit_type { + implicit_type(object const& o) + : obj(o) + {} + ~implicit_type() {} - template - operator T(); + template operator T(); -private: - object const& obj; -}; + private: + object const& obj; + }; -/// @cond + /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp index 7c1d0f6b6e8b..4a3c5aa9e9ff 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp @@ -12,1648 +12,1630 @@ #include "msgpack/v1/pack_decl.hpp" -#include -#include -#include #include +#include +#include #include +#include namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v1) { -/// @endcond - -/// The class template that supports continuous packing. -/** - * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)` - * - */ -template -class packer { -public: - /// Constructor - /** - * This constructor is left for compatibility. - * Use `packer(Stream& s)` instead of the constructor. - * - * @param s A pointer to packing destination stream object. - */ - packer(Stream* s); - /// Constructor - /** - * @param s Packing destination stream object. - */ - packer(Stream& s); - -public: - /// Packing function template - /** - * @tparam T The type of packing object. - * - * @param v a packing object. - * - * @return The reference of `*this`. - */ - template - packer& pack(const T& v); - - /// Packing uint8 - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum or uint8. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_uint8(uint8_t d); - - /// Packing uint16 - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, uint8 or uint16. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_uint16(uint16_t d); - - /// Packing uint32 - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, uint8, uint16 or uint32. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_uint32(uint32_t d); - - /// Packing uint16 - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, uint8, uint16, uint32 or uint64. - * The minimum byte size expression is used. - * positive fixnum, uint8, uint16, or uint32 is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_uint64(uint64_t d); - - /// Packing int8 - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint8, - * else the packed type is negative fixnum, or int8 - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_int8(int8_t d); - - /// Packing int16 - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, uint8, or uint16, - * else the packed type is negative fixnum, int8, or int16. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_int16(int16_t d); - - /// Packing int32 - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, uint8, uint16, or uint32, - * else the packed type is negative fixnum, int8, int16, or int32. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_int32(int32_t d); - - /// Packing int32 - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, uint8, uint16, uint32, or uint64, - * else the packed type is negative fixnum, int8, int16, int32, or int64. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_int64(int64_t d); - - - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always uint8. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_uint8(uint8_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always uint16. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_uint16(uint16_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always uint32. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_uint32(uint32_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always uint64. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_uint64(uint64_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always int8. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_int8(int8_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always int16. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_int16(int16_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always int32. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_int32(int32_t d); - - /// Packing uint8 (fixed packed type). - /** - * The packed type is always int64. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_fix_int64(int64_t d); - - - /// Packing char - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_char(char d); - - /// Packing wchar_t - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_wchar(wchar_t d); - - /// Packing signed char - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_signed_char(signed char d); - - /// Packing short - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_short(short d); - - /// Packing int - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_int(int d); - - /// Packing long - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_long(long d); - - /// Packing long long - /** - * The byte size of the packed data depends on `d`. - * If `d` is zero or positive, the packed type is positive fixnum, or uint*, - * else the packed type is negative fixnum, or int* - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_long_long(long long d); - - - /// Packing unsigned char - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, or uint*. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_unsigned_char(unsigned char d); - - /// Packing unsigned short - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, or uint*. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_unsigned_short(unsigned short d); - - /// Packing unsigned int - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, or uint*. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_unsigned_int(unsigned int d); - - /// Packing unsigned long - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, or uint*. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_unsigned_long(unsigned long d); - - /// Packing unsigned long long - /** - * The byte size of the packed data depends on `d`. - * The packed type is positive fixnum, or uint*. - * The minimum byte size expression is used. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_unsigned_long_long(unsigned long long d); - - /// Packing float - /** - * The packed type is float32. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_float(float d); - - /// Packing double - /** - * The packed type is float64. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float - * - * @param d a packing object. - * - * @return The reference of `*this`. - */ - packer& pack_double(double d); - - - /// Packing nil - /** - * The packed type is nil. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-nil - * - * @return The reference of `*this`. - */ - packer& pack_nil(); - - /// Packing true - /** - * The packed type is bool, value is true. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family - * - * @return The reference of `*this`. - */ - packer& pack_true(); - - /// Packing false - /** - * The packed type is bool, value is false. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family - * - * @return The reference of `*this`. - */ - packer& pack_false(); - - /// Packing array header and size - /** - * The packed type is array header and array size. - * You need to pack `n` msgpack objects following this header and size. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family - * - * @param n The number of array elements (array size). - * - * @return The reference of `*this`. - */ - packer& pack_array(uint32_t n); - - /// Packing map header and size - /** - * The packed type is map header and map size. - * You need to pack `n` pairs of msgpack objects following this header and size. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family - * - * @param n The number of array elements (array size). - * - * @return The reference of `*this`. - */ - packer& pack_map(uint32_t n); - - - /// Packing str header and length - /** - * The packed type is str header and length. - * The minimum byte size length expression is used. - * You need to call `pack_str_body(const char* b, uint32_t l)` after this function calling with the same `l` value. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - * - * @param l The length of string. - * - * @return The reference of `*this`. - */ - packer& pack_str(uint32_t l); - - /// Packing str body - /** - * You need to call this function just after `pack_str(uint32_t l)` calling. - * The value `l` should be the same as `pack_str(uint32_t l)` argument `l`. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - * - * @param l The length of string. - * - * @return The reference of `*this`. - */ - packer& pack_str_body(const char* b, uint32_t l); - - /// Packing raw (v4) header and length - /** - * The packed type is raw header and length. - * The minimum byte size length expression is used. - * The format raw (v4) is old MessagePack version4 format. - * You need to call `pack_v4raw_body(const char* b, uint32_t l)` after this function calling with the same `l` value. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - * - * @param l The length of string. - * - * @return The reference of `*this`. - */ - packer& pack_v4raw(uint32_t l); +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + /// @endcond - /// Packing raw (v4) body + /// The class template that supports continuous packing. /** - * The format raw (v4) is old MessagePack version4 format. - * You need to call this function just after `pack_v4raw(uint32_t l)` calling. - * The value `l` should be the same as `pack_v4raw(uint32_t l)` argument `l`. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - * - * @param l The length of string. + * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)` * - * @return The reference of `*this`. */ - packer& pack_v4raw_body(const char* b, uint32_t l); + template class packer { + public: + /// Constructor + /** + * This constructor is left for compatibility. + * Use `packer(Stream& s)` instead of the constructor. + * + * @param s A pointer to packing destination stream object. + */ + packer(Stream* s); + /// Constructor + /** + * @param s Packing destination stream object. + */ + packer(Stream& s); + + public: + /// Packing function template + /** + * @tparam T The type of packing object. + * + * @param v a packing object. + * + * @return The reference of `*this`. + */ + template packer& pack(const T& v); + + /// Packing uint8 + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum or uint8. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_uint8(uint8_t d); + + /// Packing uint16 + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, uint8 or uint16. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_uint16(uint16_t d); + + /// Packing uint32 + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, uint8, uint16 or uint32. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_uint32(uint32_t d); + + /// Packing uint16 + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, uint8, uint16, uint32 or uint64. + * The minimum byte size expression is used. + * positive fixnum, uint8, uint16, or uint32 is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_uint64(uint64_t d); + + /// Packing int8 + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint8, + * else the packed type is negative fixnum, or int8 + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_int8(int8_t d); + + /// Packing int16 + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, uint8, or uint16, + * else the packed type is negative fixnum, int8, or int16. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_int16(int16_t d); + + /// Packing int32 + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, uint8, uint16, or uint32, + * else the packed type is negative fixnum, int8, int16, or int32. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_int32(int32_t d); + + /// Packing int32 + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, uint8, uint16, uint32, or uint64, + * else the packed type is negative fixnum, int8, int16, int32, or int64. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_int64(int64_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always uint8. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_uint8(uint8_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always uint16. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_uint16(uint16_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always uint32. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_uint32(uint32_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always uint64. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_uint64(uint64_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always int8. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_int8(int8_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always int16. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_int16(int16_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always int32. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_int32(int32_t d); + + /// Packing uint8 (fixed packed type). + /** + * The packed type is always int64. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_fix_int64(int64_t d); + + /// Packing char + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_char(char d); + + /// Packing wchar_t + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_wchar(wchar_t d); + + /// Packing signed char + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_signed_char(signed char d); + + /// Packing short + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_short(short d); + + /// Packing int + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_int(int d); + + /// Packing long + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_long(long d); + + /// Packing long long + /** + * The byte size of the packed data depends on `d`. + * If `d` is zero or positive, the packed type is positive fixnum, or uint*, + * else the packed type is negative fixnum, or int* + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_long_long(long long d); + + /// Packing unsigned char + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, or uint*. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_unsigned_char(unsigned char d); + + /// Packing unsigned short + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, or uint*. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_unsigned_short(unsigned short d); + + /// Packing unsigned int + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, or uint*. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_unsigned_int(unsigned int d); + + /// Packing unsigned long + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, or uint*. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_unsigned_long(unsigned long d); + + /// Packing unsigned long long + /** + * The byte size of the packed data depends on `d`. + * The packed type is positive fixnum, or uint*. + * The minimum byte size expression is used. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_unsigned_long_long(unsigned long long d); + + /// Packing float + /** + * The packed type is float32. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_float(float d); + + /// Packing double + /** + * The packed type is float64. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float + * + * @param d a packing object. + * + * @return The reference of `*this`. + */ + packer& pack_double(double d); + + /// Packing nil + /** + * The packed type is nil. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-nil + * + * @return The reference of `*this`. + */ + packer& pack_nil(); + + /// Packing true + /** + * The packed type is bool, value is true. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family + * + * @return The reference of `*this`. + */ + packer& pack_true(); + + /// Packing false + /** + * The packed type is bool, value is false. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family + * + * @return The reference of `*this`. + */ + packer& pack_false(); + + /// Packing array header and size + /** + * The packed type is array header and array size. + * You need to pack `n` msgpack objects following this header and size. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family + * + * @param n The number of array elements (array size). + * + * @return The reference of `*this`. + */ + packer& pack_array(uint32_t n); + + /// Packing map header and size + /** + * The packed type is map header and map size. + * You need to pack `n` pairs of msgpack objects following this header and size. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family + * + * @param n The number of array elements (array size). + * + * @return The reference of `*this`. + */ + packer& pack_map(uint32_t n); + + /// Packing str header and length + /** + * The packed type is str header and length. + * The minimum byte size length expression is used. + * You need to call `pack_str_body(const char* b, uint32_t l)` after this function calling with the same `l` + * value. See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_str(uint32_t l); + + /// Packing str body + /** + * You need to call this function just after `pack_str(uint32_t l)` calling. + * The value `l` should be the same as `pack_str(uint32_t l)` argument `l`. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_str_body(const char* b, uint32_t l); + + /// Packing raw (v4) header and length + /** + * The packed type is raw header and length. + * The minimum byte size length expression is used. + * The format raw (v4) is old MessagePack version4 format. + * You need to call `pack_v4raw_body(const char* b, uint32_t l)` after this function calling with the same `l` + * value. See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_v4raw(uint32_t l); + + /// Packing raw (v4) body + /** + * The format raw (v4) is old MessagePack version4 format. + * You need to call this function just after `pack_v4raw(uint32_t l)` calling. + * The value `l` should be the same as `pack_v4raw(uint32_t l)` argument `l`. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_v4raw_body(const char* b, uint32_t l); + + /// Packing bin header and length + /** + * The packed type is bin header and length. + * The minimum byte size length expression is used. + * You need to call `pack_bin_body(const char* b, uint32_t l)` after this function calling with the same `l` + * value. See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_bin(uint32_t l); + + /// Packing bin body + /** + * You need to call this function just after `pack_bin(uint32_t l)` calling. + * The value `l` should be the same as `pack_bin(uint32_t l)` argument `l`. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_bin_body(const char* b, uint32_t l); + + /// Packing ext header, type, and length + /** + * The packed type is ext. + * The minimum byte size length expression is used. + * The length 1, 2, 4, 8, and 16 can be encoded in the header. + * You need to call `pack_ext_body(const char* b, uint32_t l)` after this function calling with the same `l` + * value. See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-ext + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_ext(size_t l, int8_t type); + + /// Packing ext body + /** + * You need to call this function just after `pack_ext(size_t l, int8_t type)` calling. + * The value `l` should be the same as `pack_ext(size_t l, int8_t type)` argument `l`. + * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family + * + * @param l The length of string. + * + * @return The reference of `*this`. + */ + packer& pack_ext_body(const char* b, uint32_t l); + + private: + template void pack_imp_uint8(T d); + template void pack_imp_uint16(T d); + template void pack_imp_uint32(T d); + template void pack_imp_uint64(T d); + template void pack_imp_int8(T d); + template void pack_imp_int16(T d); + template void pack_imp_int32(T d); + template void pack_imp_int64(T d); + + void append_buffer(const char* buf, size_t len) { append_buffer(&Stream::write, buf, len); } + + template + void append_buffer(Ret (Cls::*)(const char*, SizeType), const char* buf, size_t len) + { + m_stream.write(buf, static_cast(len)); + } - /// Packing bin header and length - /** - * The packed type is bin header and length. - * The minimum byte size length expression is used. - * You need to call `pack_bin_body(const char* b, uint32_t l)` after this function calling with the same `l` value. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family - * - * @param l The length of string. - * - * @return The reference of `*this`. - */ - packer& pack_bin(uint32_t l); + private: + Stream& m_stream; - /// Packing bin body - /** - * You need to call this function just after `pack_bin(uint32_t l)` calling. - * The value `l` should be the same as `pack_bin(uint32_t l)` argument `l`. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family - * - * @param l The length of string. - * - * @return The reference of `*this`. - */ - packer& pack_bin_body(const char* b, uint32_t l); +#if defined(MSGPACK_USE_CPP03) + private: + packer(const packer&); + packer& operator=(const packer&); + packer(); +#else // defined(MSGPACK_USE_CPP03) + public: + packer(const packer&) = delete; + packer& operator=(const packer&) = delete; + packer() = delete; +#endif // defined(MSGPACK_USE_CPP03) + }; - /// Packing ext header, type, and length + /// Pack the value as MessagePack format into the stream /** - * The packed type is ext. - * The minimum byte size length expression is used. - * The length 1, 2, 4, 8, and 16 can be encoded in the header. - * You need to call `pack_ext_body(const char* b, uint32_t l)` after this function calling with the same `l` value. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-ext + * This function template is left for compatibility. + * Use `void pack(Stream& s, const T& v)` instead of the function template. * - * @param l The length of string. - * - * @return The reference of `*this`. + * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)` + * @tparam T Any type that is adapted to MessagePack + * @param s The pointer to packing destination stream + * @param v Packing value */ - packer& pack_ext(size_t l, int8_t type); + template inline void pack(Stream * s, const T& v) + { + packer(*s).pack(v); + } - /// Packing ext body + /// Pack the value as MessagePack format into the stream /** - * You need to call this function just after `pack_ext(size_t l, int8_t type)` calling. - * The value `l` should be the same as `pack_ext(size_t l, int8_t type)` argument `l`. - * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family - * - * @param l The length of string. - * - * @return The reference of `*this`. + * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)` + * @tparam T Any type that is adapted to MessagePack + * @param s Packing destination stream + * @param v Packing value */ - packer& pack_ext_body(const char* b, uint32_t l); - -private: - template - void pack_imp_uint8(T d); - template - void pack_imp_uint16(T d); - template - void pack_imp_uint32(T d); - template - void pack_imp_uint64(T d); - template - void pack_imp_int8(T d); - template - void pack_imp_int16(T d); - template - void pack_imp_int32(T d); - template - void pack_imp_int64(T d); - - void append_buffer(const char* buf, size_t len) + template inline void pack(Stream & s, const T& v) { - append_buffer(&Stream::write, buf, len); + packer(s).pack(v); } - template - void append_buffer(Ret (Cls::*)(const char*, SizeType), const char* buf, size_t len) +#if MSGPACK_ENDIAN_LITTLE_BYTE + template inline char take8_8(T d) { - m_stream.write(buf, static_cast(len)); + return static_cast(reinterpret_cast(&d)[0]); + } + template inline char take8_16(T d) + { + return static_cast(reinterpret_cast(&d)[0]); + } + template inline char take8_32(T d) + { + return static_cast(reinterpret_cast(&d)[0]); + } + template inline char take8_64(T d) + { + return static_cast(reinterpret_cast(&d)[0]); } - -private: - Stream& m_stream; - -#if defined(MSGPACK_USE_CPP03) -private: - packer(const packer&); - packer& operator=(const packer&); - packer(); -#else // defined(MSGPACK_USE_CPP03) -public: - packer(const packer&) = delete; - packer& operator=(const packer&) = delete; - packer() = delete; -#endif // defined(MSGPACK_USE_CPP03) -}; - - -/// Pack the value as MessagePack format into the stream -/** - * This function template is left for compatibility. - * Use `void pack(Stream& s, const T& v)` instead of the function template. - * - * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)` - * @tparam T Any type that is adapted to MessagePack - * @param s The pointer to packing destination stream - * @param v Packing value - */ -template -inline void pack(Stream* s, const T& v) -{ - packer(*s).pack(v); -} - -/// Pack the value as MessagePack format into the stream -/** - * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)` - * @tparam T Any type that is adapted to MessagePack - * @param s Packing destination stream - * @param v Packing value - */ -template -inline void pack(Stream& s, const T& v) -{ - packer(s).pack(v); -} - - -#if MSGPACK_ENDIAN_LITTLE_BYTE -template -inline char take8_8(T d) { - return static_cast(reinterpret_cast(&d)[0]); -} -template -inline char take8_16(T d) { - return static_cast(reinterpret_cast(&d)[0]); -} -template -inline char take8_32(T d) { - return static_cast(reinterpret_cast(&d)[0]); -} -template -inline char take8_64(T d) { - return static_cast(reinterpret_cast(&d)[0]); -} #elif MSGPACK_ENDIAN_BIG_BYTE -template -inline char take8_8(T d) { - return static_cast(reinterpret_cast(&d)[0]); -} -template -inline char take8_16(T d) { - return static_cast(reinterpret_cast(&d)[1]); -} -template -inline char take8_32(T d) { - return static_cast(reinterpret_cast(&d)[3]); -} -template -inline char take8_64(T d) { - return static_cast(reinterpret_cast(&d)[7]); -} + template inline char take8_8(T d) + { + return static_cast(reinterpret_cast(&d)[0]); + } + template inline char take8_16(T d) + { + return static_cast(reinterpret_cast(&d)[1]); + } + template inline char take8_32(T d) + { + return static_cast(reinterpret_cast(&d)[3]); + } + template inline char take8_64(T d) + { + return static_cast(reinterpret_cast(&d)[7]); + } #else #error msgpack-c supports only big endian and little endian #endif -template -inline packer::packer(Stream* s) : m_stream(*s) { } + template + inline packer::packer(Stream * s) + : m_stream(*s) + {} -template -inline packer::packer(Stream& s) : m_stream(s) { } + template + inline packer::packer(Stream & s) + : m_stream(s) + {} + template inline packer& packer::pack_uint8(uint8_t d) + { + pack_imp_uint8(d); + return *this; + } -template -inline packer& packer::pack_uint8(uint8_t d) -{ pack_imp_uint8(d); return *this; } + template inline packer& packer::pack_uint16(uint16_t d) + { + pack_imp_uint16(d); + return *this; + } -template -inline packer& packer::pack_uint16(uint16_t d) -{ pack_imp_uint16(d); return *this; } + template inline packer& packer::pack_uint32(uint32_t d) + { + pack_imp_uint32(d); + return *this; + } -template -inline packer& packer::pack_uint32(uint32_t d) -{ pack_imp_uint32(d); return *this; } + template inline packer& packer::pack_uint64(uint64_t d) + { + pack_imp_uint64(d); + return *this; + } -template -inline packer& packer::pack_uint64(uint64_t d) -{ pack_imp_uint64(d); return *this; } + template inline packer& packer::pack_int8(int8_t d) + { + pack_imp_int8(d); + return *this; + } -template -inline packer& packer::pack_int8(int8_t d) -{ pack_imp_int8(d); return *this; } + template inline packer& packer::pack_int16(int16_t d) + { + pack_imp_int16(d); + return *this; + } -template -inline packer& packer::pack_int16(int16_t d) -{ pack_imp_int16(d); return *this; } + template inline packer& packer::pack_int32(int32_t d) + { + pack_imp_int32(d); + return *this; + } -template -inline packer& packer::pack_int32(int32_t d) -{ pack_imp_int32(d); return *this; } + template inline packer& packer::pack_int64(int64_t d) + { + pack_imp_int64(d); + return *this; + } -template -inline packer& packer::pack_int64(int64_t d) -{ pack_imp_int64(d); return *this;} + template inline packer& packer::pack_fix_uint8(uint8_t d) + { + char buf[2] = { static_cast(0xccu), take8_8(d) }; + append_buffer(buf, 2); + return *this; + } + template inline packer& packer::pack_fix_uint16(uint16_t d) + { + char buf[3]; + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], d); + append_buffer(buf, 3); + return *this; + } -template -inline packer& packer::pack_fix_uint8(uint8_t d) -{ - char buf[2] = {static_cast(0xccu), take8_8(d)}; - append_buffer(buf, 2); - return *this; -} + template inline packer& packer::pack_fix_uint32(uint32_t d) + { + char buf[5]; + buf[0] = static_cast(0xceu); + _msgpack_store32(&buf[1], d); + append_buffer(buf, 5); + return *this; + } -template -inline packer& packer::pack_fix_uint16(uint16_t d) -{ - char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], d); - append_buffer(buf, 3); - return *this; -} - -template -inline packer& packer::pack_fix_uint32(uint32_t d) -{ - char buf[5]; - buf[0] = static_cast(0xceu); _msgpack_store32(&buf[1], d); - append_buffer(buf, 5); - return *this; -} - -template -inline packer& packer::pack_fix_uint64(uint64_t d) -{ - char buf[9]; - buf[0] = static_cast(0xcfu); _msgpack_store64(&buf[1], d); - append_buffer(buf, 9); - return *this; -} - -template -inline packer& packer::pack_fix_int8(int8_t d) -{ - char buf[2] = {static_cast(0xd0u), take8_8(d)}; - append_buffer(buf, 2); - return *this; -} + template inline packer& packer::pack_fix_uint64(uint64_t d) + { + char buf[9]; + buf[0] = static_cast(0xcfu); + _msgpack_store64(&buf[1], d); + append_buffer(buf, 9); + return *this; + } -template -inline packer& packer::pack_fix_int16(int16_t d) -{ - char buf[3]; - buf[0] = static_cast(0xd1u); _msgpack_store16(&buf[1], (uint16_t)d); - append_buffer(buf, 3); - return *this; -} - -template -inline packer& packer::pack_fix_int32(int32_t d) -{ - char buf[5]; - buf[0] = static_cast(0xd2u); _msgpack_store32(&buf[1], (uint32_t)d); - append_buffer(buf, 5); - return *this; -} - -template -inline packer& packer::pack_fix_int64(int64_t d) -{ - char buf[9]; - buf[0] = static_cast(0xd3u); _msgpack_store64(&buf[1], d); - append_buffer(buf, 9); - return *this; -} + template inline packer& packer::pack_fix_int8(int8_t d) + { + char buf[2] = { static_cast(0xd0u), take8_8(d) }; + append_buffer(buf, 2); + return *this; + } + template inline packer& packer::pack_fix_int16(int16_t d) + { + char buf[3]; + buf[0] = static_cast(0xd1u); + _msgpack_store16(&buf[1], (uint16_t)d); + append_buffer(buf, 3); + return *this; + } -template -inline packer& packer::pack_char(char d) -{ + template inline packer& packer::pack_fix_int32(int32_t d) + { + char buf[5]; + buf[0] = static_cast(0xd2u); + _msgpack_store32(&buf[1], (uint32_t)d); + append_buffer(buf, 5); + return *this; + } + + template inline packer& packer::pack_fix_int64(int64_t d) + { + char buf[9]; + buf[0] = static_cast(0xd3u); + _msgpack_store64(&buf[1], d); + append_buffer(buf, 9); + return *this; + } + + template inline packer& packer::pack_char(char d) + { #if defined(CHAR_MIN) #if CHAR_MIN < 0 - pack_imp_int8(d); + pack_imp_int8(d); #else - pack_imp_uint8(d); + pack_imp_uint8(d); #endif #else #error CHAR_MIN is not defined #endif - return *this; -} - -template -inline packer& packer::pack_wchar(wchar_t d) -{ - if (d < 0) { - pack_imp_int64(static_cast(d)); + return *this; } - else { - pack_imp_uint64(static_cast(d)); + + template inline packer& packer::pack_wchar(wchar_t d) + { + if (d < 0) { + pack_imp_int64(static_cast(d)); + } else { + pack_imp_uint64(static_cast(d)); + } + return *this; } - return *this; -} -template -inline packer& packer::pack_signed_char(signed char d) -{ - pack_imp_int8(d); - return *this; -} + template inline packer& packer::pack_signed_char(signed char d) + { + pack_imp_int8(d); + return *this; + } -template -inline packer& packer::pack_short(short d) -{ + template inline packer& packer::pack_short(short d) + { #if defined(SIZEOF_SHORT) #if SIZEOF_SHORT == 2 - pack_imp_int16(d); + pack_imp_int16(d); #elif SIZEOF_SHORT == 4 - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #elif defined(SHRT_MAX) #if SHRT_MAX == 0x7fff - pack_imp_int16(d); + pack_imp_int16(d); #elif SHRT_MAX == 0x7fffffff - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #else - if(sizeof(short) == 2) { - pack_imp_int16(d); - } else if(sizeof(short) == 4) { - pack_imp_int32(d); - } else { - pack_imp_int64(d); - } + if (sizeof(short) == 2) { + pack_imp_int16(d); + } else if (sizeof(short) == 4) { + pack_imp_int32(d); + } else { + pack_imp_int64(d); + } #endif - return *this; -} + return *this; + } -template -inline packer& packer::pack_int(int d) -{ + template inline packer& packer::pack_int(int d) + { #if defined(SIZEOF_INT) #if SIZEOF_INT == 2 - pack_imp_int16(d); + pack_imp_int16(d); #elif SIZEOF_INT == 4 - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #elif defined(INT_MAX) #if INT_MAX == 0x7fff - pack_imp_int16(d); + pack_imp_int16(d); #elif INT_MAX == 0x7fffffff - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #else - if(sizeof(int) == 2) { - pack_imp_int16(d); - } else if(sizeof(int) == 4) { - pack_imp_int32(d); - } else { - pack_imp_int64(d); - } + if (sizeof(int) == 2) { + pack_imp_int16(d); + } else if (sizeof(int) == 4) { + pack_imp_int32(d); + } else { + pack_imp_int64(d); + } #endif - return *this; -} + return *this; + } -template -inline packer& packer::pack_long(long d) -{ + template inline packer& packer::pack_long(long d) + { #if defined(SIZEOF_LONG) #if SIZEOF_LONG == 2 - pack_imp_int16(d); + pack_imp_int16(d); #elif SIZEOF_LONG == 4 - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #elif defined(LONG_MAX) #if LONG_MAX == 0x7fffL - pack_imp_int16(d); + pack_imp_int16(d); #elif LONG_MAX == 0x7fffffffL - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #else - if(sizeof(long) == 2) { - pack_imp_int16(d); - } else if(sizeof(long) == 4) { - pack_imp_int32(d); - } else { - pack_imp_int64(d); - } + if (sizeof(long) == 2) { + pack_imp_int16(d); + } else if (sizeof(long) == 4) { + pack_imp_int32(d); + } else { + pack_imp_int64(d); + } #endif - return *this; -} + return *this; + } -template -inline packer& packer::pack_long_long(long long d) -{ + template inline packer& packer::pack_long_long(long long d) + { #if defined(SIZEOF_LONG_LONG) #if SIZEOF_LONG_LONG == 2 - pack_imp_int16(d); + pack_imp_int16(d); #elif SIZEOF_LONG_LONG == 4 - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #elif defined(LLONG_MAX) #if LLONG_MAX == 0x7fffL - pack_imp_int16(d); + pack_imp_int16(d); #elif LLONG_MAX == 0x7fffffffL - pack_imp_int32(d); + pack_imp_int32(d); #else - pack_imp_int64(d); + pack_imp_int64(d); #endif #else - if(sizeof(long long) == 2) { - pack_imp_int16(d); - } else if(sizeof(long long) == 4) { - pack_imp_int32(d); - } else { - pack_imp_int64(d); - } + if (sizeof(long long) == 2) { + pack_imp_int16(d); + } else if (sizeof(long long) == 4) { + pack_imp_int32(d); + } else { + pack_imp_int64(d); + } #endif - return *this; -} - + return *this; + } -template -inline packer& packer::pack_unsigned_char(unsigned char d) -{ - pack_imp_uint8(d); - return *this; -} + template inline packer& packer::pack_unsigned_char(unsigned char d) + { + pack_imp_uint8(d); + return *this; + } -template -inline packer& packer::pack_unsigned_short(unsigned short d) -{ + template inline packer& packer::pack_unsigned_short(unsigned short d) + { #if defined(SIZEOF_SHORT) #if SIZEOF_SHORT == 2 - pack_imp_uint16(d); + pack_imp_uint16(d); #elif SIZEOF_SHORT == 4 - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #elif defined(USHRT_MAX) #if USHRT_MAX == 0xffffU - pack_imp_uint16(d); + pack_imp_uint16(d); #elif USHRT_MAX == 0xffffffffU - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #else - if(sizeof(unsigned short) == 2) { - pack_imp_uint16(d); - } else if(sizeof(unsigned short) == 4) { - pack_imp_uint32(d); - } else { - pack_imp_uint64(d); - } + if (sizeof(unsigned short) == 2) { + pack_imp_uint16(d); + } else if (sizeof(unsigned short) == 4) { + pack_imp_uint32(d); + } else { + pack_imp_uint64(d); + } #endif - return *this; -} + return *this; + } -template -inline packer& packer::pack_unsigned_int(unsigned int d) -{ + template inline packer& packer::pack_unsigned_int(unsigned int d) + { #if defined(SIZEOF_INT) #if SIZEOF_INT == 2 - pack_imp_uint16(d); + pack_imp_uint16(d); #elif SIZEOF_INT == 4 - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #elif defined(UINT_MAX) #if UINT_MAX == 0xffffU - pack_imp_uint16(d); + pack_imp_uint16(d); #elif UINT_MAX == 0xffffffffU - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #else - if(sizeof(unsigned int) == 2) { - pack_imp_uint16(d); - } else if(sizeof(unsigned int) == 4) { - pack_imp_uint32(d); - } else { - pack_imp_uint64(d); - } + if (sizeof(unsigned int) == 2) { + pack_imp_uint16(d); + } else if (sizeof(unsigned int) == 4) { + pack_imp_uint32(d); + } else { + pack_imp_uint64(d); + } #endif - return *this; -} + return *this; + } -template -inline packer& packer::pack_unsigned_long(unsigned long d) -{ + template inline packer& packer::pack_unsigned_long(unsigned long d) + { #if defined(SIZEOF_LONG) #if SIZEOF_LONG == 2 - pack_imp_uint16(d); + pack_imp_uint16(d); #elif SIZEOF_LONG == 4 - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #elif defined(ULONG_MAX) #if ULONG_MAX == 0xffffUL - pack_imp_uint16(d); + pack_imp_uint16(d); #elif ULONG_MAX == 0xffffffffUL - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #else - if(sizeof(unsigned long) == 2) { - pack_imp_uint16(d); - } else if(sizeof(unsigned long) == 4) { - pack_imp_uint32(d); - } else { - pack_imp_uint64(d); - } + if (sizeof(unsigned long) == 2) { + pack_imp_uint16(d); + } else if (sizeof(unsigned long) == 4) { + pack_imp_uint32(d); + } else { + pack_imp_uint64(d); + } #endif - return *this; -} + return *this; + } -template -inline packer& packer::pack_unsigned_long_long(unsigned long long d) -{ + template inline packer& packer::pack_unsigned_long_long(unsigned long long d) + { #if defined(SIZEOF_LONG_LONG) #if SIZEOF_LONG_LONG == 2 - pack_imp_uint16(d); + pack_imp_uint16(d); #elif SIZEOF_LONG_LONG == 4 - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #elif defined(ULLONG_MAX) #if ULLONG_MAX == 0xffffUL - pack_imp_uint16(d); + pack_imp_uint16(d); #elif ULLONG_MAX == 0xffffffffUL - pack_imp_uint32(d); + pack_imp_uint32(d); #else - pack_imp_uint64(d); + pack_imp_uint64(d); #endif #else - if(sizeof(unsigned long long) == 2) { - pack_imp_uint16(d); - } else if(sizeof(unsigned long long) == 4) { - pack_imp_uint32(d); - } else { - pack_imp_uint64(d); - } + if (sizeof(unsigned long long) == 2) { + pack_imp_uint16(d); + } else if (sizeof(unsigned long long) == 4) { + pack_imp_uint32(d); + } else { + pack_imp_uint64(d); + } #endif - return *this; -} - + return *this; + } -template -inline packer& packer::pack_float(float d) -{ - 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; - } else if(d < 0 && d >= float(std::numeric_limits::min()) && d == float(int64_t(d))) { - pack_imp_int64(int64_t(d)); - return *this; + template inline packer& packer::pack_float(float d) + { + 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; + } else if (d < 0 && d >= float(std::numeric_limits::min()) && d == float(int64_t(d))) { + pack_imp_int64(int64_t(d)); + return *this; + } } - } - union { float f; uint32_t i; } mem; - mem.f = d; - char buf[5]; - buf[0] = static_cast(0xcau); _msgpack_store32(&buf[1], mem.i); - append_buffer(buf, 5); - return *this; -} + union { + float f; + uint32_t i; + } mem; + mem.f = d; + char buf[5]; + buf[0] = static_cast(0xcau); + _msgpack_store32(&buf[1], mem.i); + append_buffer(buf, 5); + return *this; + } -template -inline packer& packer::pack_double(double d) -{ - 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; - } else if(d < 0 && d >= double(std::numeric_limits::min()) && d == double(int64_t(d))) { - pack_imp_int64(int64_t(d)); - return *this; + template inline packer& packer::pack_double(double d) + { + 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; + } else if (d < 0 && d >= double(std::numeric_limits::min()) && d == double(int64_t(d))) { + pack_imp_int64(int64_t(d)); + return *this; + } } - } - union { double f; uint64_t i; } mem; - mem.f = d; - char buf[9]; - buf[0] = static_cast(0xcbu); + union { + double f; + uint64_t i; + } mem; + mem.f = d; + char buf[9]; + buf[0] = static_cast(0xcbu); #if defined(TARGET_OS_IPHONE) - // ok + // ok #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi - // https://github.com/msgpack/msgpack-perl/pull/1 - mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL); + // https://github.com/msgpack/msgpack-perl/pull/1 + mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL); #endif - _msgpack_store64(&buf[1], mem.i); - append_buffer(buf, 9); - return *this; -} - - -template -inline packer& packer::pack_nil() -{ - const char d = static_cast(0xc0u); - append_buffer(&d, 1); - return *this; -} - -template -inline packer& packer::pack_true() -{ - const char d = static_cast(0xc3u); - append_buffer(&d, 1); - return *this; -} - -template -inline packer& packer::pack_false() -{ - const char d = static_cast(0xc2u); - append_buffer(&d, 1); - return *this; -} - + _msgpack_store64(&buf[1], mem.i); + append_buffer(buf, 9); + return *this; + } -template -inline packer& packer::pack_array(uint32_t n) -{ - if(n < 16) { - char d = static_cast(0x90u | n); + template inline packer& packer::pack_nil() + { + const char d = static_cast(0xc0u); append_buffer(&d, 1); - } else if(n < 65536) { - char buf[3]; - buf[0] = static_cast(0xdcu); _msgpack_store16(&buf[1], static_cast(n)); - append_buffer(buf, 3); - } else { - char buf[5]; - buf[0] = static_cast(0xddu); _msgpack_store32(&buf[1], static_cast(n)); - append_buffer(buf, 5); + return *this; } - return *this; -} -template -inline packer& packer::pack_map(uint32_t n) -{ - if(n < 16) { - unsigned char d = static_cast(0x80u | n); - char buf = take8_8(d); - append_buffer(&buf, 1); - } else if(n < 65536) { - char buf[3]; - buf[0] = static_cast(0xdeu); _msgpack_store16(&buf[1], static_cast(n)); - append_buffer(buf, 3); - } else { - char buf[5]; - buf[0] = static_cast(0xdfu); _msgpack_store32(&buf[1], static_cast(n)); - append_buffer(buf, 5); + template inline packer& packer::pack_true() + { + const char d = static_cast(0xc3u); + append_buffer(&d, 1); + return *this; } - return *this; -} -template -inline packer& packer::pack_str(uint32_t l) -{ - if(l < 32) { - unsigned char d = static_cast(0xa0u | l); - char buf = take8_8(d); - append_buffer(&buf, 1); - } else if(l < 256) { - char buf[2]; - buf[0] = static_cast(0xd9u); buf[1] = static_cast(l); - append_buffer(buf, 2); - } else if(l < 65536) { - char buf[3]; - buf[0] = static_cast(0xdau); _msgpack_store16(&buf[1], static_cast(l)); - append_buffer(buf, 3); - } else { - char buf[5]; - buf[0] = static_cast(0xdbu); _msgpack_store32(&buf[1], static_cast(l)); - append_buffer(buf, 5); + template inline packer& packer::pack_false() + { + const char d = static_cast(0xc2u); + append_buffer(&d, 1); + return *this; } - return *this; -} -template -inline packer& packer::pack_str_body(const char* b, uint32_t l) -{ - append_buffer(b, l); - return *this; -} - -// Raw (V4) - -template -inline packer& packer::pack_v4raw(uint32_t l) -{ - if(l < 32) { - unsigned char d = static_cast(0xa0u | l); - char buf = take8_8(d); - append_buffer(&buf, 1); - } else if(l < 65536) { - char buf[3]; - buf[0] = static_cast(0xdau); _msgpack_store16(&buf[1], static_cast(l)); - append_buffer(buf, 3); - } else { - char buf[5]; - buf[0] = static_cast(0xdbu); _msgpack_store32(&buf[1], static_cast(l)); - append_buffer(buf, 5); + template inline packer& packer::pack_array(uint32_t n) + { + if (n < 16) { + char d = static_cast(0x90u | n); + append_buffer(&d, 1); + } else if (n < 65536) { + char buf[3]; + buf[0] = static_cast(0xdcu); + _msgpack_store16(&buf[1], static_cast(n)); + append_buffer(buf, 3); + } else { + char buf[5]; + buf[0] = static_cast(0xddu); + _msgpack_store32(&buf[1], static_cast(n)); + append_buffer(buf, 5); + } + return *this; } - return *this; -} - -template -inline packer& packer::pack_v4raw_body(const char* b, uint32_t l) -{ - append_buffer(b, l); - return *this; -} -template -inline packer& packer::pack_bin(uint32_t l) -{ - if(l < 256) { - char buf[2]; - buf[0] = static_cast(0xc4u); buf[1] = static_cast(l); - append_buffer(buf, 2); - } else if(l < 65536) { - char buf[3]; - buf[0] = static_cast(0xc5u); _msgpack_store16(&buf[1], static_cast(l)); - append_buffer(buf, 3); - } else { - char buf[5]; - buf[0] = static_cast(0xc6u); _msgpack_store32(&buf[1], static_cast(l)); - append_buffer(buf, 5); + template inline packer& packer::pack_map(uint32_t n) + { + if (n < 16) { + unsigned char d = static_cast(0x80u | n); + char buf = take8_8(d); + append_buffer(&buf, 1); + } else if (n < 65536) { + char buf[3]; + buf[0] = static_cast(0xdeu); + _msgpack_store16(&buf[1], static_cast(n)); + append_buffer(buf, 3); + } else { + char buf[5]; + buf[0] = static_cast(0xdfu); + _msgpack_store32(&buf[1], static_cast(n)); + append_buffer(buf, 5); + } + return *this; } - return *this; -} - -template -inline packer& packer::pack_bin_body(const char* b, uint32_t l) -{ - append_buffer(b, l); - return *this; -} -template -inline packer& packer::pack_ext(size_t l, int8_t type) -{ - switch(l) { - case 1: { - char buf[2]; - buf[0] = static_cast(0xd4u); - buf[1] = static_cast(type); - append_buffer(buf, 2); - } break; - case 2: { - char buf[2]; - buf[0] = static_cast(0xd5u); - buf[1] = static_cast(type); - append_buffer(buf, 2); - } break; - case 4: { - char buf[2]; - buf[0] = static_cast(0xd6u); - buf[1] = static_cast(type); - append_buffer(buf, 2); - } break; - case 8: { - char buf[2]; - buf[0] = static_cast(0xd7u); - buf[1] = static_cast(type); - append_buffer(buf, 2); - } break; - case 16: { - char buf[2]; - buf[0] = static_cast(0xd8u); - buf[1] = static_cast(type); - append_buffer(buf, 2); - } break; - default: - if(l < 256) { - char buf[3]; - buf[0] = static_cast(0xc7u); + template inline packer& packer::pack_str(uint32_t l) + { + if (l < 32) { + unsigned char d = static_cast(0xa0u | l); + char buf = take8_8(d); + append_buffer(&buf, 1); + } else if (l < 256) { + char buf[2]; + buf[0] = static_cast(0xd9u); buf[1] = static_cast(l); - buf[2] = static_cast(type); - append_buffer(buf, 3); - } else if(l < 65536) { - char buf[4]; - buf[0] = static_cast(0xc8u); + append_buffer(buf, 2); + } else if (l < 65536) { + char buf[3]; + buf[0] = static_cast(0xdau); _msgpack_store16(&buf[1], static_cast(l)); - buf[3] = static_cast(type); - append_buffer(buf, 4); + append_buffer(buf, 3); } else { - char buf[6]; - buf[0] = static_cast(0xc9u); + char buf[5]; + buf[0] = static_cast(0xdbu); _msgpack_store32(&buf[1], static_cast(l)); - buf[5] = static_cast(type); - append_buffer(buf, 6); + append_buffer(buf, 5); } - break; + return *this; } - return *this; -} -template -inline packer& packer::pack_ext_body(const char* b, uint32_t l) -{ - append_buffer(b, l); - return *this; -} - -template -template -inline void packer::pack_imp_uint8(T d) -{ - if(d < (1<<7)) { - /* fixnum */ - char buf = take8_8(d); - append_buffer(&buf, 1); - } else { - /* unsigned 8 */ - char buf[2] = {static_cast(0xccu), take8_8(d)}; - append_buffer(buf, 2); + template inline packer& packer::pack_str_body(const char* b, uint32_t l) + { + append_buffer(b, l); + return *this; } -} -template -template -inline void packer::pack_imp_uint16(T d) -{ - if(d < (1<<7)) { - /* fixnum */ - char buf = take8_16(d); - append_buffer(&buf, 1); - } else if(d < (1<<8)) { - /* unsigned 8 */ - char buf[2] = {static_cast(0xccu), take8_16(d)}; - append_buffer(buf, 2); - } else { - /* unsigned 16 */ - char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], static_cast(d)); - append_buffer(buf, 3); - } -} + // Raw (V4) -template -template -inline void packer::pack_imp_uint32(T d) -{ - if(d < (1<<8)) { - if(d < (1<<7)) { - /* fixnum */ - char buf = take8_32(d); + template inline packer& packer::pack_v4raw(uint32_t l) + { + if (l < 32) { + unsigned char d = static_cast(0xa0u | l); + char buf = take8_8(d); append_buffer(&buf, 1); - } else { - /* unsigned 8 */ - char buf[2] = {static_cast(0xccu), take8_32(d)}; - append_buffer(buf, 2); - } - } else { - if(d < (1<<16)) { - /* unsigned 16 */ + } else if (l < 65536) { char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], static_cast(d)); + buf[0] = static_cast(0xdau); + _msgpack_store16(&buf[1], static_cast(l)); append_buffer(buf, 3); } else { - /* unsigned 32 */ char buf[5]; - buf[0] = static_cast(0xceu); _msgpack_store32(&buf[1], static_cast(d)); + buf[0] = static_cast(0xdbu); + _msgpack_store32(&buf[1], static_cast(l)); append_buffer(buf, 5); } + return *this; } -} -template -template -inline void packer::pack_imp_uint64(T d) -{ - if(d < (1ULL<<8)) { - if(d < (1ULL<<7)) { - /* fixnum */ - char buf = take8_64(d); - append_buffer(&buf, 1); - } else { - /* unsigned 8 */ - char buf[2] = {static_cast(0xccu), take8_64(d)}; + template inline packer& packer::pack_v4raw_body(const char* b, uint32_t l) + { + append_buffer(b, l); + return *this; + } + + template inline packer& packer::pack_bin(uint32_t l) + { + if (l < 256) { + char buf[2]; + buf[0] = static_cast(0xc4u); + buf[1] = static_cast(l); append_buffer(buf, 2); - } - } else { - if(d < (1ULL<<16)) { - /* unsigned 16 */ + } else if (l < 65536) { char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], static_cast(d)); + buf[0] = static_cast(0xc5u); + _msgpack_store16(&buf[1], static_cast(l)); append_buffer(buf, 3); - } else if(d < (1ULL<<32)) { - /* unsigned 32 */ + } else { char buf[5]; - buf[0] = static_cast(0xceu); _msgpack_store32(&buf[1], static_cast(d)); + buf[0] = static_cast(0xc6u); + _msgpack_store32(&buf[1], static_cast(l)); append_buffer(buf, 5); - } else { - /* unsigned 64 */ - char buf[9]; - buf[0] = static_cast(0xcfu); _msgpack_store64(&buf[1], d); - append_buffer(buf, 9); } + return *this; } -} -template -template -inline void packer::pack_imp_int8(T d) -{ - if(d < -(1<<5)) { - /* signed 8 */ - char buf[2] = {static_cast(0xd0u), take8_8(d)}; - append_buffer(buf, 2); - } else { - /* fixnum */ - char buf = take8_8(d); - append_buffer(&buf, 1); + template inline packer& packer::pack_bin_body(const char* b, uint32_t l) + { + append_buffer(b, l); + return *this; } -} -template -template -inline void packer::pack_imp_int16(T d) -{ - if(d < -(1<<5)) { - if(d < -(1<<7)) { - /* signed 16 */ - char buf[3]; - buf[0] = static_cast(0xd1u); _msgpack_store16(&buf[1], static_cast(d)); - append_buffer(buf, 3); - } else { - /* signed 8 */ - char buf[2] = {static_cast(0xd0u), take8_16(d)}; + template inline packer& packer::pack_ext(size_t l, int8_t type) + { + switch (l) { + case 1: { + char buf[2]; + buf[0] = static_cast(0xd4u); + buf[1] = static_cast(type); append_buffer(buf, 2); - } - } else if(d < (1<<7)) { - /* fixnum */ - char buf = take8_16(d); - append_buffer(&buf, 1); - } else { - if(d < (1<<8)) { - /* unsigned 8 */ - char buf[2] = {static_cast(0xccu), take8_16(d)}; + } break; + case 2: { + char buf[2]; + buf[0] = static_cast(0xd5u); + buf[1] = static_cast(type); append_buffer(buf, 2); - } else { - /* unsigned 16 */ - char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], static_cast(d)); - append_buffer(buf, 3); + } break; + case 4: { + char buf[2]; + buf[0] = static_cast(0xd6u); + buf[1] = static_cast(type); + append_buffer(buf, 2); + } break; + case 8: { + char buf[2]; + buf[0] = static_cast(0xd7u); + buf[1] = static_cast(type); + append_buffer(buf, 2); + } break; + case 16: { + char buf[2]; + buf[0] = static_cast(0xd8u); + buf[1] = static_cast(type); + append_buffer(buf, 2); + } break; + default: + if (l < 256) { + char buf[3]; + buf[0] = static_cast(0xc7u); + buf[1] = static_cast(l); + buf[2] = static_cast(type); + append_buffer(buf, 3); + } else if (l < 65536) { + char buf[4]; + buf[0] = static_cast(0xc8u); + _msgpack_store16(&buf[1], static_cast(l)); + buf[3] = static_cast(type); + append_buffer(buf, 4); + } else { + char buf[6]; + buf[0] = static_cast(0xc9u); + _msgpack_store32(&buf[1], static_cast(l)); + buf[5] = static_cast(type); + append_buffer(buf, 6); + } + break; } + return *this; } -} -template -template -inline void packer::pack_imp_int32(T d) -{ - if(d < -(1<<5)) { - if(d < -(1<<15)) { - /* signed 32 */ - char buf[5]; - buf[0] = static_cast(0xd2u); _msgpack_store32(&buf[1], static_cast(d)); - append_buffer(buf, 5); - } else if(d < -(1<<7)) { - /* signed 16 */ - char buf[3]; - buf[0] = static_cast(0xd1u); _msgpack_store16(&buf[1], static_cast(d)); - append_buffer(buf, 3); + template inline packer& packer::pack_ext_body(const char* b, uint32_t l) + { + append_buffer(b, l); + return *this; + } + + template template inline void packer::pack_imp_uint8(T d) + { + if (d < (1 << 7)) { + /* fixnum */ + char buf = take8_8(d); + append_buffer(&buf, 1); } else { - /* signed 8 */ - char buf[2] = { static_cast(0xd0u), take8_32(d)}; + /* unsigned 8 */ + char buf[2] = { static_cast(0xccu), take8_8(d) }; append_buffer(buf, 2); } - } else if(d < (1<<7)) { - /* fixnum */ - char buf = take8_32(d); - append_buffer(&buf, 1); - } else { - if(d < (1<<8)) { + } + + template template inline void packer::pack_imp_uint16(T d) + { + if (d < (1 << 7)) { + /* fixnum */ + char buf = take8_16(d); + append_buffer(&buf, 1); + } else if (d < (1 << 8)) { /* unsigned 8 */ - char buf[2] = { static_cast(0xccu), take8_32(d)}; + char buf[2] = { static_cast(0xccu), take8_16(d) }; append_buffer(buf, 2); - } else if(d < (1<<16)) { + } else { /* unsigned 16 */ char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], static_cast(d)); + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], static_cast(d)); append_buffer(buf, 3); + } + } + + template template inline void packer::pack_imp_uint32(T d) + { + if (d < (1 << 8)) { + if (d < (1 << 7)) { + /* fixnum */ + char buf = take8_32(d); + append_buffer(&buf, 1); + } else { + /* unsigned 8 */ + char buf[2] = { static_cast(0xccu), take8_32(d) }; + append_buffer(buf, 2); + } } else { - /* unsigned 32 */ - char buf[5]; - buf[0] = static_cast(0xceu); _msgpack_store32(&buf[1], static_cast(d)); - append_buffer(buf, 5); + if (d < (1 << 16)) { + /* unsigned 16 */ + char buf[3]; + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], static_cast(d)); + append_buffer(buf, 3); + } else { + /* unsigned 32 */ + char buf[5]; + buf[0] = static_cast(0xceu); + _msgpack_store32(&buf[1], static_cast(d)); + append_buffer(buf, 5); + } } } -} -template -template -inline void packer::pack_imp_int64(T d) -{ - if(d < -(1LL<<5)) { - if(d < -(1LL<<15)) { - if(d < -(1LL<<31)) { - /* signed 64 */ - char buf[9]; - buf[0] = static_cast(0xd3u); _msgpack_store64(&buf[1], d); - append_buffer(buf, 9); + template template inline void packer::pack_imp_uint64(T d) + { + if (d < (1ULL << 8)) { + if (d < (1ULL << 7)) { + /* fixnum */ + char buf = take8_64(d); + append_buffer(&buf, 1); } else { - /* signed 32 */ + /* unsigned 8 */ + char buf[2] = { static_cast(0xccu), take8_64(d) }; + append_buffer(buf, 2); + } + } else { + if (d < (1ULL << 16)) { + /* unsigned 16 */ + char buf[3]; + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], static_cast(d)); + append_buffer(buf, 3); + } else if (d < (1ULL << 32)) { + /* unsigned 32 */ char buf[5]; - buf[0] = static_cast(0xd2u); _msgpack_store32(&buf[1], static_cast(d)); + buf[0] = static_cast(0xceu); + _msgpack_store32(&buf[1], static_cast(d)); append_buffer(buf, 5); + } else { + /* unsigned 64 */ + char buf[9]; + buf[0] = static_cast(0xcfu); + _msgpack_store64(&buf[1], d); + append_buffer(buf, 9); } + } + } + + template template inline void packer::pack_imp_int8(T d) + { + if (d < -(1 << 5)) { + /* signed 8 */ + char buf[2] = { static_cast(0xd0u), take8_8(d) }; + append_buffer(buf, 2); } else { - if(d < -(1<<7)) { + /* fixnum */ + char buf = take8_8(d); + append_buffer(&buf, 1); + } + } + + template template inline void packer::pack_imp_int16(T d) + { + if (d < -(1 << 5)) { + if (d < -(1 << 7)) { /* signed 16 */ char buf[3]; - buf[0] = static_cast(0xd1u); _msgpack_store16(&buf[1], static_cast(d)); + buf[0] = static_cast(0xd1u); + _msgpack_store16(&buf[1], static_cast(d)); append_buffer(buf, 3); } else { /* signed 8 */ - char buf[2] = {static_cast(0xd0u), take8_64(d)}; + char buf[2] = { static_cast(0xd0u), take8_16(d) }; append_buffer(buf, 2); } - } - } else if(d < (1<<7)) { - /* fixnum */ - char buf = take8_64(d); - append_buffer(&buf, 1); - } else { - if(d < (1LL<<16)) { - if(d < (1<<8)) { + } else if (d < (1 << 7)) { + /* fixnum */ + char buf = take8_16(d); + append_buffer(&buf, 1); + } else { + if (d < (1 << 8)) { /* unsigned 8 */ - char buf[2] = {static_cast(0xccu), take8_64(d)}; + char buf[2] = { static_cast(0xccu), take8_16(d) }; append_buffer(buf, 2); } else { /* unsigned 16 */ char buf[3]; - buf[0] = static_cast(0xcdu); _msgpack_store16(&buf[1], static_cast(d)); + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], static_cast(d)); + append_buffer(buf, 3); + } + } + } + + template template inline void packer::pack_imp_int32(T d) + { + if (d < -(1 << 5)) { + if (d < -(1 << 15)) { + /* signed 32 */ + char buf[5]; + buf[0] = static_cast(0xd2u); + _msgpack_store32(&buf[1], static_cast(d)); + append_buffer(buf, 5); + } else if (d < -(1 << 7)) { + /* signed 16 */ + char buf[3]; + buf[0] = static_cast(0xd1u); + _msgpack_store16(&buf[1], static_cast(d)); append_buffer(buf, 3); + } else { + /* signed 8 */ + char buf[2] = { static_cast(0xd0u), take8_32(d) }; + append_buffer(buf, 2); } + } else if (d < (1 << 7)) { + /* fixnum */ + char buf = take8_32(d); + append_buffer(&buf, 1); } else { - if(d < (1LL<<32)) { + if (d < (1 << 8)) { + /* unsigned 8 */ + char buf[2] = { static_cast(0xccu), take8_32(d) }; + append_buffer(buf, 2); + } else if (d < (1 << 16)) { + /* unsigned 16 */ + char buf[3]; + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], static_cast(d)); + append_buffer(buf, 3); + } else { /* unsigned 32 */ char buf[5]; - buf[0] = static_cast(0xceu); _msgpack_store32(&buf[1], static_cast(d)); + buf[0] = static_cast(0xceu); + _msgpack_store32(&buf[1], static_cast(d)); append_buffer(buf, 5); + } + } + } + + template template inline void packer::pack_imp_int64(T d) + { + if (d < -(1LL << 5)) { + if (d < -(1LL << 15)) { + if (d < -(1LL << 31)) { + /* signed 64 */ + char buf[9]; + buf[0] = static_cast(0xd3u); + _msgpack_store64(&buf[1], d); + append_buffer(buf, 9); + } else { + /* signed 32 */ + char buf[5]; + buf[0] = static_cast(0xd2u); + _msgpack_store32(&buf[1], static_cast(d)); + append_buffer(buf, 5); + } } else { - /* unsigned 64 */ - char buf[9]; - buf[0] = static_cast(0xcfu); _msgpack_store64(&buf[1], d); - append_buffer(buf, 9); + if (d < -(1 << 7)) { + /* signed 16 */ + char buf[3]; + buf[0] = static_cast(0xd1u); + _msgpack_store16(&buf[1], static_cast(d)); + append_buffer(buf, 3); + } else { + /* signed 8 */ + char buf[2] = { static_cast(0xd0u), take8_64(d) }; + append_buffer(buf, 2); + } + } + } else if (d < (1 << 7)) { + /* fixnum */ + char buf = take8_64(d); + append_buffer(&buf, 1); + } else { + if (d < (1LL << 16)) { + if (d < (1 << 8)) { + /* unsigned 8 */ + char buf[2] = { static_cast(0xccu), take8_64(d) }; + append_buffer(buf, 2); + } else { + /* unsigned 16 */ + char buf[3]; + buf[0] = static_cast(0xcdu); + _msgpack_store16(&buf[1], static_cast(d)); + append_buffer(buf, 3); + } + } else { + if (d < (1LL << 32)) { + /* unsigned 32 */ + char buf[5]; + buf[0] = static_cast(0xceu); + _msgpack_store32(&buf[1], static_cast(d)); + append_buffer(buf, 5); + } else { + /* unsigned 64 */ + char buf[9]; + buf[0] = static_cast(0xcfu); + _msgpack_store64(&buf[1], d); + append_buffer(buf, 9); + } } } } -} -/// @cond -} // MSGPACK_API_VERSION_NAMESPACE(v1) + /// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond -} // namespace msgpack +} // namespace msgpack #endif // MSGPACK_V1_PACK_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp index 9c44aae6099c..38a95e258e58 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp @@ -11,96 +11,89 @@ #ifndef MSGPACK_V2_OBJECT_FWD_HPP #define MSGPACK_V2_OBJECT_FWD_HPP -#include "msgpack/v2/object_fwd_decl.hpp" #include "msgpack/object_fwd.hpp" +#include "msgpack/v2/object_fwd_decl.hpp" namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v2) { -/// @endcond - -struct object : v1::object { - object() {} - object(v1::object const& o):v1::object(o) {} - /// Construct object from T - /** - * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map, - * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor. - * - * @tparam T The type of `v`. - * @param v The value you want to convert. - */ - template - explicit object(const T& v) { - *this << v; - } - - /// Construct object from T - /** - * The object is constructed on the zone `z`. - * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object - * - * @tparam T The type of `v`. - * @param v The value you want to convert. - * @param z The zone that is used by the object. - */ - template - object(const T& v, msgpack::zone& z):v1::object(v, z) {} - -public: - /// Convert the object - /** - * If the object can't be converted to T, msgpack::type_error would be thrown. - * @tparam T The type of v. - * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object. - * @return The reference of `v`. - */ - template - T& convert(T& v) const { return v1::object::convert(v); } - - using v1::object::with_zone; - implicit_type convert() const; -}; +MSGPACK_API_VERSION_NAMESPACE(v2) +{ + /// @endcond + + struct object : v1::object { + object() {} + object(v1::object const& o) + : v1::object(o) + {} + /// Construct object from T + /** + * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map, + * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor. + * + * @tparam T The type of `v`. + * @param v The value you want to convert. + */ + template explicit object(const T& v) { *this << v; } + + /// Construct object from T + /** + * The object is constructed on the zone `z`. + * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object + * + * @tparam T The type of `v`. + * @param v The value you want to convert. + * @param z The zone that is used by the object. + */ + template + object(const T& v, msgpack::zone& z) + : v1::object(v, z) + {} + + public: + /// Convert the object + /** + * If the object can't be converted to T, msgpack::type_error would be thrown. + * @tparam T The type of v. + * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the + * object. + * @return The reference of `v`. + */ + template T& convert(T& v) const { return v1::object::convert(v); } + + using v1::object::with_zone; + implicit_type convert() const; + }; #if !defined(MSGPACK_USE_CPP03) -namespace adaptor { + namespace adaptor { -// If v1 has as specialization for T, then dispatch v1::adaptor::as. -// So I call v1::has_as meta function intentionally. -template -struct as::value>::type> : v1::adaptor::as { -}; + // If v1 has as specialization for T, then dispatch v1::adaptor::as. + // So I call v1::has_as meta function intentionally. + template struct as::value>::type> : v1::adaptor::as {}; -} // namespace adaptor + } // namespace adaptor -template -struct has_as { -private: - template - static auto check(U*) -> - typename std::enable_if< + template struct has_as { + private: + template + static auto check_(U*) -> typename std::enable_if< // check v2 specialization - std::is_same< - decltype(adaptor::as()(std::declval())), - U - >::value - || - // check v1 specialization - v1::has_as::value, - std::true_type - >::type; - template - static std::false_type check(...); -public: - using type = decltype(check(MSGPACK_NULLPTR)); - static constexpr bool value = type::value; -}; + std::is_same()(std::declval())), U>::value || + // check v1 specialization + v1::has_as::value, + std::true_type>::type; + template static std::false_type check_(...); + + public: + using type = decltype(check_(MSGPACK_NULLPTR)); + static constexpr bool value = type::value; + }; #endif // !defined(MSGPACK_USE_CPP03) -/// @cond + /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v2) /// @endcond diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp index d282f1b4a47f..0fa02c7a149f 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp @@ -11,57 +11,47 @@ #ifndef MSGPACK_V3_OBJECT_FWD_HPP #define MSGPACK_V3_OBJECT_FWD_HPP -#include "msgpack/v3/object_fwd_decl.hpp" #include "msgpack/object_fwd.hpp" +#include "msgpack/v3/object_fwd_decl.hpp" namespace msgpack { /// @cond -MSGPACK_API_VERSION_NAMESPACE(v3) { -/// @endcond +MSGPACK_API_VERSION_NAMESPACE(v3) +{ + /// @endcond #if !defined(MSGPACK_USE_CPP03) -namespace adaptor { + namespace adaptor { -// If v2 has as specialization for T, then dispatch v2::adaptor::as. -// So I call v2::has_as meta function intentionally. -template -struct as::value>::type> : v2::adaptor::as { -}; + // If v2 has as specialization for T, then dispatch v2::adaptor::as. + // So I call v2::has_as meta function intentionally. + template struct as::value>::type> : v2::adaptor::as {}; -} // namespace adaptor + } // namespace adaptor -template -struct has_as { -private: - template - static auto check(U*) -> - typename std::enable_if< + template struct has_as { + private: + template + static auto check_(U*) -> typename std::enable_if< // check v3 specialization - std::is_same< - decltype(adaptor::as()(std::declval())), - U - >::value - || - // check v2 specialization - v2::has_as::value - || - // check v1 specialization - v1::has_as::value, - std::true_type - >::type; - template - static std::false_type check(...); -public: - using type = decltype(check(MSGPACK_NULLPTR)); - static constexpr bool value = type::value; -}; + std::is_same()(std::declval())), U>::value || + // check v2 specialization + v2::has_as::value || + // check v1 specialization + v1::has_as::value, + std::true_type>::type; + template static std::false_type check_(...); + + public: + using type = decltype(check_(MSGPACK_NULLPTR)); + static constexpr bool value = type::value; + }; #endif // !defined(MSGPACK_USE_CPP03) - -/// @cond + /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v3) /// @endcond diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp index fdab088941d5..1b8b0ec220a4 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp @@ -1,3 +1,3 @@ -#define MSGPACK_VERSION_MAJOR 6 -#define MSGPACK_VERSION_MINOR 0 +#define MSGPACK_VERSION_MAJOR 6 +#define MSGPACK_VERSION_MINOR 1 #define MSGPACK_VERSION_REVISION 0 diff --git a/barretenberg/cpp/src/msgpack-c/preprocess.rb b/barretenberg/cpp/src/msgpack-c/preprocess.rb new file mode 100644 index 000000000000..95cd313bd22e --- /dev/null +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt index e0ebf2583ce0..a0233dd3557e 100644 --- a/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/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/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp b/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp index 9ab3dc611e7b..22b7c5958aaf 100644 --- a/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp +++ b/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include #define BOOST_TEST_MODULE MSGPACK_BOOST #include @@ -12,10 +12,8 @@ #if defined(MSGPACK_NO_BOOST) -BOOST_AUTO_TEST_CASE(empty) -{ -} -#else // defined(MSGPACK_NO_BOOST) +BOOST_AUTO_TEST_CASE(empty) {} +#else // defined(MSGPACK_NO_BOOST) const double kEPS = 1e-10; @@ -29,8 +27,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_nil) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_nil()); BOOST_CHECK_NO_THROW(boost::get(val2)); @@ -68,8 +65,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_nil_default) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_nil()); BOOST_CHECK_NO_THROW(boost::get(val2)); @@ -108,8 +104,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_bool) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_bool()); BOOST_CHECK(val2.as_bool()); @@ -162,8 +157,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_positive_integer) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + 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(), 123U); @@ -210,8 +204,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_negative_integer) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + 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(), -123); @@ -258,13 +251,12 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_float) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); 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 +269,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 +284,114 @@ 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 @@ -306,8 +406,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_str) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_string()); BOOST_CHECK_EQUAL(val2.as_string(), "ABC"); @@ -315,7 +414,6 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_str) BOOST_CHECK(val1 == val2); } - BOOST_AUTO_TEST_CASE(object_with_zone_variant_str) { msgpack::zone z; @@ -369,17 +467,14 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_bin) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_vector_char()); BOOST_CHECK(val2.as_vector_char() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); + BOOST_CHECK_NO_THROW(boost::get>(val2)); BOOST_CHECK(val1 == val2); } - - BOOST_AUTO_TEST_CASE(object_with_zone_variant_bin) { msgpack::zone z; @@ -394,7 +489,7 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_bin) msgpack::type::variant val2 = obj.as(); BOOST_CHECK(val2.is_vector_char()); BOOST_CHECK(val2.as_vector_char() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); + BOOST_CHECK_NO_THROW(boost::get>(val2)); BOOST_CHECK(val1 == val2); } @@ -417,8 +512,8 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_raw_ref) // Converted as std::vector. BOOST_CHECK(val2.is_vector_char()); BOOST_CHECK(val2.as_vector_char() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); - // msgpack::type::raw_ref and std::vector are different. + BOOST_CHECK_NO_THROW(boost::get>(val2)); + // msgpack::type::raw_ref and std::vector are different. BOOST_CHECK(!(val1 == val2)); } @@ -441,8 +536,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ext) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_ext()); BOOST_CHECK(val2.as_ext() == e); @@ -487,7 +581,7 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_ext_ref) BOOST_CHECK(val2.is_ext()); BOOST_CHECK(val2.as_ext() == msgpack::type::ext(e)); BOOST_CHECK_NO_THROW(boost::get(val2)); - // msgpack::type::ext_ref and msgpack::type::ext are different. + // msgpack::type::ext_ref and msgpack::type::ext are different. BOOST_CHECK(!(val1 == val2)); } @@ -507,12 +601,11 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_array) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_vector()); BOOST_CHECK(val2.as_vector() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); + BOOST_CHECK_NO_THROW(boost::get>(val2)); BOOST_CHECK(val1 == val2); } @@ -530,7 +623,7 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_array) msgpack::type::variant val2 = obj.as(); BOOST_CHECK(val2.is_vector()); BOOST_CHECK(val2.as_vector() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); + BOOST_CHECK_NO_THROW(boost::get>(val2)); BOOST_CHECK(val1 == val2); } @@ -550,8 +643,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_map) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant val2 = oh.get().as(); BOOST_CHECK(val2.is_multimap()); BOOST_CHECK(val2.as_multimap() == v); @@ -595,8 +687,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_str) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant_ref val2 = oh.get().as(); BOOST_CHECK(val2.is_boost_string_ref()); BOOST_CHECK_EQUAL(val2.as_boost_string_ref(), sr); @@ -604,8 +695,6 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_str) BOOST_CHECK(val1 == val2); } - - BOOST_AUTO_TEST_CASE(object_with_zone_variant_ref_str) { msgpack::zone z; @@ -641,8 +730,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_bin) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant_ref val2 = oh.get().as(); BOOST_CHECK(val2.is_raw_ref()); BOOST_CHECK(val2.as_raw_ref() == rr); @@ -650,8 +738,6 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_bin) BOOST_CHECK(val1 == val2); } - - BOOST_AUTO_TEST_CASE(object_with_zone_variant_ref_bin) { msgpack::zone z; @@ -689,8 +775,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_ext) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant_ref val2 = oh.get().as(); BOOST_CHECK_NO_THROW(boost::get(val2)); BOOST_CHECK(val2.is_ext_ref()); @@ -698,7 +783,6 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_ext) BOOST_CHECK(val1 == val2); } - BOOST_AUTO_TEST_CASE(object_with_zone_variant_ref_ext) { msgpack::zone z; @@ -740,12 +824,11 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_array) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant_ref val2 = oh.get().as(); BOOST_CHECK(val2.is_vector()); BOOST_CHECK(val2.as_vector() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); + BOOST_CHECK_NO_THROW(boost::get>(val2)); BOOST_CHECK(val1 == val2); } @@ -768,7 +851,7 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_ref_array) msgpack::type::variant_ref val2 = obj.as(); BOOST_CHECK(val2.is_vector()); BOOST_CHECK(val2.as_vector() == v); - BOOST_CHECK_NO_THROW(boost::get >(val2)); + BOOST_CHECK_NO_THROW(boost::get>(val2)); BOOST_CHECK(val1 == val2); } @@ -783,7 +866,8 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_map) std::string s1("ABC"); std::string s2("DEF"); #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - v.insert(multimap_t::value_type(msgpack::type::variant_ref(boost::string_ref(s1)), msgpack::type::variant_ref(boost::string_ref(s2)))); + v.insert(multimap_t::value_type(msgpack::type::variant_ref(boost::string_ref(s1)), + msgpack::type::variant_ref(boost::string_ref(s2)))); #else // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 v.insert(multimap_t::value_type(msgpack::type::variant_ref(s1), msgpack::type::variant_ref(s2))); #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 @@ -794,8 +878,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_ref_map) msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); msgpack::type::variant_ref val2 = oh.get().as(); BOOST_CHECK(val2.is_multimap()); BOOST_CHECK(val2.as_multimap() == v); @@ -812,7 +895,8 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_ref_map) std::string s1("ABC"); std::string s2("DEF"); #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 - v.insert(multimap_t::value_type(msgpack::type::variant_ref(boost::string_ref(s1)), msgpack::type::variant_ref(boost::string_ref(s2)))); + v.insert(multimap_t::value_type(msgpack::type::variant_ref(boost::string_ref(s1)), + msgpack::type::variant_ref(boost::string_ref(s2)))); #else // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 v.insert(multimap_t::value_type(msgpack::type::variant_ref(s1), msgpack::type::variant_ref(s2))); #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 diff --git a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp index 2a856cb8ba82..242c21c6a8ea 100644 --- a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp +++ b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp @@ -9,18 +9,15 @@ #if !defined(MSGPACK_USE_CPP03) -class TestEnumClassMemberClass -{ -public: +class TestEnumClassMemberClass { + public: TestEnumClassMemberClass() - : t1(TestEnumClassType::STATE_A), t2(TestEnumClassType::STATE_B), t3(TestEnumClassType::STATE_C) {} - - enum class TestEnumClassType:long { - STATE_INVALID = 0, - STATE_A = 1, - STATE_B = 2, - STATE_C = 3 - }; + : t1(TestEnumClassType::STATE_A) + , t2(TestEnumClassType::STATE_B) + , t3(TestEnumClassType::STATE_C) + {} + + enum class TestEnumClassType : long { STATE_INVALID = 0, STATE_A = 1, STATE_B = 2, STATE_C = 3 }; TestEnumClassType t1; TestEnumClassType t2; TestEnumClassType t3; @@ -35,7 +32,6 @@ using namespace std; const unsigned int kLoop = 10000; const unsigned int kElements = 100; - // C++11 BOOST_AUTO_TEST_CASE(simple_tuple) @@ -43,9 +39,8 @@ BOOST_AUTO_TEST_CASE(simple_tuple) msgpack::sbuffer sbuf; std::tuple val1(true, "kzk", 12.3); msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::tuple val2 = oh.get().as >(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::tuple val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } @@ -54,9 +49,8 @@ BOOST_AUTO_TEST_CASE(simple_tuple_empty) msgpack::sbuffer sbuf; std::tuple<> val1; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::tuple<> val2 = oh.get().as >(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::tuple<> val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } @@ -65,9 +59,8 @@ BOOST_AUTO_TEST_CASE(simple_tuple_size_greater_than_as) msgpack::sbuffer sbuf; std::tuple val1(true, "kzk", 42); msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::tuple val2 = oh.get().as >(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::tuple val2 = oh.get().as>(); BOOST_CHECK_EQUAL(std::get<0>(val1), std::get<0>(val2)); BOOST_CHECK_EQUAL(std::get<1>(val1), std::get<1>(val2)); BOOST_CHECK_EQUAL(std::get<2>(val1), std::get<2>(val2)); @@ -78,8 +71,7 @@ BOOST_AUTO_TEST_CASE(simple_tuple_size_greater_than_convert) msgpack::sbuffer sbuf; std::tuple val1(true, "kzk", 42); msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::tuple val2; oh.get().convert(val2); BOOST_CHECK_EQUAL(std::get<0>(val1), std::get<0>(val2)); @@ -92,9 +84,8 @@ BOOST_AUTO_TEST_CASE(simple_tuple_size_less_than_as) msgpack::sbuffer sbuf; std::tuple val1(true, "kzk", 42); msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::tuple val2 = oh.get().as >(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::tuple val2 = oh.get().as>(); BOOST_CHECK_EQUAL(std::get<0>(val1), std::get<0>(val2)); BOOST_CHECK_EQUAL(std::get<1>(val1), std::get<1>(val2)); } @@ -104,8 +95,7 @@ BOOST_AUTO_TEST_CASE(simple_tuple_size_less_than_convert) msgpack::sbuffer sbuf; std::tuple val1(true, "kzk", 42); msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::tuple val2; oh.get().convert(val2); BOOST_CHECK_EQUAL(std::get<0>(val1), std::get<0>(val2)); @@ -120,10 +110,9 @@ BOOST_AUTO_TEST_CASE(simple_array) val1[i] = rand(); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::ARRAY); - array val2 = oh.get().as >(); + array val2 = oh.get().as>(); BOOST_CHECK_EQUAL(val1.size(), val2.size()); BOOST_CHECK(equal(val1.begin(), val1.end(), val2.begin())); } @@ -134,39 +123,35 @@ BOOST_AUTO_TEST_CASE(simple_array_empty) array val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::ARRAY); - array val2 = oh.get().as >(); + array val2 = oh.get().as>(); BOOST_CHECK_EQUAL(val1.size(), val2.size()); BOOST_CHECK(equal(val1.begin(), val1.end(), val2.begin())); } BOOST_AUTO_TEST_CASE(simple_array_size_less_than) { - array val1 { {1 , 2} }; + array val1{ { 1, 2 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::ARRAY); array val2; try { oh.get().convert(val2); BOOST_CHECK(false); - } - catch (msgpack::type_error const&) { + } catch (msgpack::type_error const&) { BOOST_CHECK(true); } } BOOST_AUTO_TEST_CASE(simple_array_size_greater_than) { - array val1 { {1 , 2} }; + array val1{ { 1, 2 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::ARRAY); array val2; oh.get().convert(val2); @@ -182,10 +167,9 @@ BOOST_AUTO_TEST_CASE(simple_buffer_array_char) val1[i] = static_cast(rand()); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::BIN); - array val2 = oh.get().as >(); + array val2 = oh.get().as>(); BOOST_CHECK_EQUAL(val1.size(), val2.size()); BOOST_CHECK(equal(val1.begin(), val1.end(), val2.begin())); } @@ -196,27 +180,26 @@ BOOST_AUTO_TEST_CASE(simple_buffer_array_char_empty) array val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::BIN); - array val2 = oh.get().as >(); + array val2 = oh.get().as>(); BOOST_CHECK_EQUAL(val1.size(), val2.size()); BOOST_CHECK(equal(val1.begin(), val1.end(), val2.begin())); } BOOST_AUTO_TEST_CASE(simple_buffer_array_unsigned_char) { - if (!msgpack::is_same::value) return; + if (!msgpack::is_same::value) + return; for (unsigned int k = 0; k < kLoop; k++) { array val1; for (unsigned int i = 0; i < kElements; i++) val1[i] = static_cast(rand()); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::BIN); - array val2 = oh.get().as >(); + array val2 = oh.get().as>(); BOOST_CHECK_EQUAL(val1.size(), val2.size()); BOOST_CHECK(equal(val1.begin(), val1.end(), val2.begin())); } @@ -224,14 +207,14 @@ BOOST_AUTO_TEST_CASE(simple_buffer_array_unsigned_char) BOOST_AUTO_TEST_CASE(simple_buffer_array_unsigned_char_empty) { - if (!msgpack::is_same::value) return; + if (!msgpack::is_same::value) + return; array val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); BOOST_CHECK_EQUAL(oh.get().type, msgpack::type::BIN); - array val2 = oh.get().as >(); + array val2 = oh.get().as>(); BOOST_CHECK_EQUAL(val1.size(), val2.size()); BOOST_CHECK(equal(val1.begin(), val1.end(), val2.begin())); } @@ -239,19 +222,20 @@ BOOST_AUTO_TEST_CASE(simple_buffer_array_unsigned_char_empty) // strong typedefs namespace test { -template -struct hash : std::hash { +template struct hash : std::hash { using std::hash::hash; }; -template -struct equal_to : std::equal_to { +template struct equal_to : std::equal_to { using std::equal_to::equal_to; }; -template -struct set_allocator : std::allocator { +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 @@ -259,17 +243,22 @@ struct set_allocator : std::allocator { // parameters, but the first one must match the type. // That's why this helper with exactly one template parameter representing // a whole key-value pair is required -template -struct map_allocator_impl : std::allocator { +template struct map_allocator_impl : std::allocator { using std::allocator::allocator; + + template struct rebind { + using other = map_allocator_impl; + }; }; -template -using map_allocator = map_allocator_impl>; +template using map_allocator = map_allocator_impl>; -template -struct allocator : std::allocator { +template struct allocator : std::allocator { using std::allocator::allocator; + + template struct rebind { + using other = allocator; + }; }; } // namespace test @@ -283,9 +272,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_forward_list) val1.push_front(rand()); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } } @@ -296,9 +284,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_forward_list_empty) type val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -311,9 +298,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_map) val1[rand()] = rand(); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } } @@ -324,9 +310,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_map_empty) type val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -342,9 +327,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_multimap) } msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -356,9 +340,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_multimap_empty) type val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -372,8 +355,7 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_set) val1.insert(rand()); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -385,8 +367,7 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_set_empty) type val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -400,9 +381,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_multiset) val1.insert(rand()); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } } @@ -413,9 +393,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_unordered_multiset_empty) type val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - type val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + type val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -424,8 +403,7 @@ BOOST_AUTO_TEST_CASE(simple_buffer_enum_class_member) TestEnumClassMemberClass val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); TestEnumClassMemberClass val2 = oh.get().as(); BOOST_CHECK(val1.t1 == val2.t1); BOOST_CHECK(val1.t2 == val2.t2); @@ -434,53 +412,58 @@ BOOST_AUTO_TEST_CASE(simple_buffer_enum_class_member) struct no_def_con { no_def_con() = delete; - no_def_con(int i):i(i) {} + no_def_con(int i) + : i(i) + {} int i; MSGPACK_DEFINE(i); }; -inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) { +inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) +{ return lhs.i == rhs.i; } -inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) { +inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) +{ return !(lhs == rhs); } -inline bool operator<(no_def_con const& lhs, no_def_con const& rhs) { - return lhs.i < rhs.i; +inline bool operator<(no_def_con const& lhs, no_def_con const& rhs) +{ + return lhs.i < rhs.i; } namespace msgpack { -MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) +{ namespace adaptor { - template <> - struct as { - no_def_con operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); - if (o.via.array.size != 1) throw msgpack::type_error(); + template <> struct as { + no_def_con operator()(msgpack::object const& o) const + { + if (o.type != msgpack::type::ARRAY) + throw msgpack::type_error(); + if (o.via.array.size != 1) + throw msgpack::type_error(); return no_def_con(o.via.array.ptr[0].as()); } }; - } // adaptor + } // namespace adaptor } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) -} // msgpack +} // namespace msgpack namespace std { template <> struct hash { - size_t operator()(const no_def_con & x) const { - return hash()(x.i); - } + size_t operator()(const no_def_con& x) const { return hash()(x.i); } }; -} // std +} // namespace std BOOST_AUTO_TEST_CASE(no_def_con_simple_buffer) { no_def_con val1(42); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); no_def_con val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -488,230 +471,224 @@ BOOST_AUTO_TEST_CASE(no_def_con_simple_buffer) struct no_def_con_composite { no_def_con_composite() = delete; - no_def_con_composite(int i):ndc(i) {} - no_def_con_composite(no_def_con const& a):ndc(a) {} + no_def_con_composite(int i) + : ndc(i) + {} + no_def_con_composite(no_def_con const& a) + : ndc(a) + {} no_def_con ndc; MSGPACK_DEFINE(ndc); }; -inline bool operator==(no_def_con_composite const& lhs, no_def_con_composite const& rhs) { +inline bool operator==(no_def_con_composite const& lhs, no_def_con_composite const& rhs) +{ return lhs.ndc == rhs.ndc; } -inline bool operator!=(no_def_con_composite const& lhs, no_def_con_composite const& rhs) { +inline bool operator!=(no_def_con_composite const& lhs, no_def_con_composite const& rhs) +{ return !(lhs == rhs); } -inline bool operator<(no_def_con_composite const& lhs, no_def_con_composite const& rhs) { +inline bool operator<(no_def_con_composite const& lhs, no_def_con_composite const& rhs) +{ return lhs.ndc < rhs.ndc; } namespace msgpack { -MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) +{ namespace adaptor { - template <> - struct as { - no_def_con_composite operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); - if (o.via.array.size != 1) throw msgpack::type_error(); + template <> struct as { + no_def_con_composite operator()(msgpack::object const& o) const + { + if (o.type != msgpack::type::ARRAY) + throw msgpack::type_error(); + if (o.via.array.size != 1) + throw msgpack::type_error(); return no_def_con_composite(o.via.array.ptr[0].as()); } }; - } // adaptor + } // namespace adaptor } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) -} // msgpack +} // namespace msgpack BOOST_AUTO_TEST_CASE(no_def_con_composite_simple_buffer) { no_def_con_composite val1(42); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); no_def_con_composite val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } struct no_def_con_inherit : no_def_con { no_def_con_inherit() = delete; - no_def_con_inherit(no_def_con const& a):no_def_con(a) {} + no_def_con_inherit(no_def_con const& a) + : no_def_con(a) + {} MSGPACK_DEFINE(MSGPACK_BASE(no_def_con)); }; namespace msgpack { -MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) +{ namespace adaptor { - template <> - struct as { - no_def_con_inherit operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); - if (o.via.array.size != 1) throw msgpack::type_error(); + template <> struct as { + no_def_con_inherit operator()(msgpack::object const& o) const + { + if (o.type != msgpack::type::ARRAY) + throw msgpack::type_error(); + if (o.via.array.size != 1) + throw msgpack::type_error(); return no_def_con_inherit(o.via.array.ptr[0].as()); } }; - } // adaptor + } // namespace adaptor } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) -} // msgpack +} // namespace msgpack BOOST_AUTO_TEST_CASE(no_def_con_inherit_simple_buffer) { no_def_con_inherit val1(42); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); no_def_con_inherit val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_vector_simple_buffer) { - std::vector val1 { 1, 2, 3 }; + std::vector val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::vector val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_list_simple_buffer) { - std::list val1 { 1, 2, 3 }; + std::list val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::list val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_set_simple_buffer) { - std::set val1 { 1, 2, 3 }; + std::set val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::set val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_multiset_simple_buffer) { - std::multiset val1 { 1, 2, 3 }; + std::multiset val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::multiset val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_assoc_vector_simple_buffer) { - msgpack::type::assoc_vector val1 { {1, 2}, {3, 4}, {5, 6}}; + msgpack::type::assoc_vector val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - msgpack::type::assoc_vector val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::type::assoc_vector val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_assoc_vector_simple_buffer) { - msgpack::type::assoc_vector val1 { {1, 2}, {3, 4}, {5, 6}}; + msgpack::type::assoc_vector val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - msgpack::type::assoc_vector val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::type::assoc_vector val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_map_simple_buffer) { - std::map val1 { {1, 2}, {3, 4}, {5, 6}}; + std::map val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::map val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::map val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_map_simple_buffer) { - std::map val1 { {1, 2}, {3, 4}, {5, 6}}; + std::map val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::map val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::map val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_multimap_simple_buffer) { - std::multimap val1 { {1, 2}, {3, 4}, {5, 6}}; + std::multimap val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::multimap val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::multimap val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_multimap_simple_buffer) { - std::multimap val1 { {1, 2}, {3, 4}, {5, 6}}; + std::multimap val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::multimap val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::multimap val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_deque_simple_buffer) { - std::deque val1 { 1, 2, 3 }; + std::deque val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::deque val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_pair_simple_buffer) { - std::pair val1 {1, 2}; + std::pair val1{ 1, 2 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::pair val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::pair val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_pair_simple_buffer) { - std::pair val1 {1, 2}; + std::pair val1{ 1, 2 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::pair val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::pair val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } @@ -721,49 +698,44 @@ BOOST_AUTO_TEST_CASE(no_def_con_def_con_pair_simple_buffer) BOOST_AUTO_TEST_CASE(no_def_con_tuple_simple_buffer) { - std::tuple val1 {1, 2, 3}; + std::tuple val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::tuple val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::tuple val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_tuple_simple_buffer) { - std::tuple val1 {1, 2, 3}; + std::tuple val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::tuple val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::tuple val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_msgpack_tuple_simple_buffer) { - msgpack::type::tuple val1 {1, 2, 3}; + msgpack::type::tuple val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - msgpack::type::tuple val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::type::tuple val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_msgpack_tuple_simple_buffer) { - msgpack::type::tuple val1 {1, 2, 3}; + msgpack::type::tuple val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - msgpack::type::tuple val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::type::tuple val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } @@ -771,106 +743,93 @@ BOOST_AUTO_TEST_CASE(no_def_con_def_con_msgpack_tuple_simple_buffer) BOOST_AUTO_TEST_CASE(no_def_forward_list_simple_buffer) { - std::forward_list val1 { 1, 2, 3 }; + std::forward_list val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::forward_list val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_unordered_set_simple_buffer) { - std::unordered_set val1 { 1, 2, 3 }; + std::unordered_set val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::unordered_set val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_unordered_multiset_simple_buffer) { - std::unordered_multiset val1 { 1, 2, 3 }; + std::unordered_multiset val1{ 1, 2, 3 }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::unordered_multiset val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_unordered_map_simple_buffer) { - std::unordered_map val1 { {1, 2}, {3, 4}, {5, 6}}; + std::unordered_map val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::unordered_map val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::unordered_map val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_unordered_map_simple_buffer) { - std::unordered_map val1 { {1, 2}, {3, 4}, {5, 6}}; + std::unordered_map val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::unordered_map val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::unordered_map val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_unordered_multimap_simple_buffer) { - std::unordered_multimap val1 { {1, 2}, {3, 4}, {5, 6}}; + std::unordered_multimap val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::unordered_multimap val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::unordered_multimap val2 = + oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_def_con_unordered_multimap_simple_buffer) { - std::unordered_multimap val1 { {1, 2}, {3, 4}, {5, 6}}; + std::unordered_multimap val1{ { 1, 2 }, { 3, 4 }, { 5, 6 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); - std::unordered_multimap val2 - = oh.get().as>(); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + std::unordered_multimap val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(no_def_con_array_simple_buffer) { - std::array val1 { { 1, 2, 3 } }; + std::array val1{ { 1, 2, 3 } }; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::array val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } -#if !defined(MSGPACK_NO_BOOST) - BOOST_AUTO_TEST_CASE(system_clock) { std::chrono::system_clock::time_point val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -884,18 +843,11 @@ BOOST_AUTO_TEST_CASE(system_clock_32) std::chrono::system_clock::time_point val1(std::chrono::seconds(0x12345678L)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - char packed[] = { - static_cast(0xd6u), - static_cast(-1), - static_cast(0x12), - static_cast(0x34), - static_cast(0x56), - static_cast(0x78) - }; + char packed[] = { static_cast(0xd6u), static_cast(-1), static_cast(0x12), + static_cast(0x34), static_cast(0x56), static_cast(0x78) }; BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed, sizeof(packed)), 0); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -909,18 +861,11 @@ BOOST_AUTO_TEST_CASE(system_clock_32_max) std::chrono::system_clock::time_point val1(std::chrono::seconds(0xffffffffL)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - char packed[] = { - static_cast(0xd6u), - static_cast(-1), - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; + char packed[] = { static_cast(0xd6u), static_cast(-1), static_cast(0xffu), + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed, sizeof(packed)), 0); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -929,19 +874,14 @@ BOOST_AUTO_TEST_CASE(system_clock_32_max) BOOST_CHECK(val1 == val3); } - BOOST_AUTO_TEST_CASE(system_clock_64) { std::chrono::system_clock::time_point val1(std::chrono::seconds(0x31234567L)); - val1 += - std::chrono::duration_cast( - std::chrono::nanoseconds(0x312345678L) - ); + val1 += std::chrono::duration_cast(std::chrono::nanoseconds(0x312345678L)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -954,45 +894,28 @@ BOOST_AUTO_TEST_CASE(system_clock_64_max) { std::chrono::system_clock::time_point val1(std::chrono::seconds(0xffffffffL)); - val1 += - std::chrono::duration_cast( - std::chrono::nanoseconds(0x3b9ac9ffL) // 999,999,999 - ); - msgpack::sbuffer sbuf; - msgpack::pack(sbuf, val1); - char packed_nano[] = { - static_cast(0xd7u), - static_cast(-1), - static_cast(0xeeu), // 999,999,999 << 2 - static_cast(0x6b), - static_cast(0x27), - static_cast(0xfcu), - static_cast(0xffu), // 32 bit sec - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; - char packed_micro[] = { - static_cast(0xd7u), - static_cast(-1), - static_cast(0xeeu), // 999,999,000 << 2 - static_cast(0x6b), - static_cast(0x18), - static_cast(0x60), - static_cast(0xffu), // 32 bit sec - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; + val1 += std::chrono::duration_cast( + std::chrono::nanoseconds(0x3b9ac9ffL) // 999,999,999 + ); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + char packed_nano[] = { static_cast(0xd7u), static_cast(-1), + static_cast(0xeeu), // 999,999,999 << 2 + static_cast(0x6b), static_cast(0x27), static_cast(0xfcu), + static_cast(0xffu), // 32 bit sec + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; + char packed_micro[] = { static_cast(0xd7u), static_cast(-1), + static_cast(0xeeu), // 999,999,000 << 2 + static_cast(0x6b), static_cast(0x18), static_cast(0x60), + static_cast(0xffu), // 32 bit sec + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; if (std::chrono::system_clock::duration::period::ratio::den == 1000000000) { BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed_nano, sizeof(packed_nano)), 0); - } - else if (std::chrono::system_clock::duration::period::ratio::den == 1000000) { + } else if (std::chrono::system_clock::duration::period::ratio::den == 1000000) { BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed_micro, sizeof(packed_micro)), 0); } - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1007,8 +930,7 @@ BOOST_AUTO_TEST_CASE(system_clock_impl_min) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1024,8 +946,7 @@ BOOST_AUTO_TEST_CASE(system_clock_impl_max) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1041,8 +962,7 @@ BOOST_AUTO_TEST_CASE(system_clock_impl_now) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::system_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1057,8 +977,7 @@ BOOST_AUTO_TEST_CASE(steady_clock) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1072,18 +991,11 @@ BOOST_AUTO_TEST_CASE(steady_clock_32) std::chrono::steady_clock::time_point val1(std::chrono::seconds(0x12345678L)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - char packed[] = { - static_cast(0xd6u), - static_cast(-1), - static_cast(0x12), - static_cast(0x34), - static_cast(0x56), - static_cast(0x78) - }; + char packed[] = { static_cast(0xd6u), static_cast(-1), static_cast(0x12), + static_cast(0x34), static_cast(0x56), static_cast(0x78) }; BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed, sizeof(packed)), 0); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1097,18 +1009,11 @@ BOOST_AUTO_TEST_CASE(steady_clock_32_max) std::chrono::steady_clock::time_point val1(std::chrono::seconds(0xffffffffL)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - char packed[] = { - static_cast(0xd6u), - static_cast(-1), - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; + char packed[] = { static_cast(0xd6u), static_cast(-1), static_cast(0xffu), + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed, sizeof(packed)), 0); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1117,19 +1022,14 @@ BOOST_AUTO_TEST_CASE(steady_clock_32_max) BOOST_CHECK(val1 == val3); } - BOOST_AUTO_TEST_CASE(steady_clock_64) { std::chrono::steady_clock::time_point val1(std::chrono::seconds(0x31234567L)); - val1 += - std::chrono::duration_cast( - std::chrono::nanoseconds(0x312345678L) - ); + val1 += std::chrono::duration_cast(std::chrono::nanoseconds(0x312345678L)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1142,45 +1042,28 @@ BOOST_AUTO_TEST_CASE(steady_clock_64_max) { std::chrono::steady_clock::time_point val1(std::chrono::seconds(0xffffffffL)); - val1 += - std::chrono::duration_cast( - std::chrono::nanoseconds(0x3b9ac9ffL) // 999,999,999 - ); - msgpack::sbuffer sbuf; - msgpack::pack(sbuf, val1); - char packed_nano[] = { - static_cast(0xd7u), - static_cast(-1), - static_cast(0xeeu), // 999,999,999 << 2 - static_cast(0x6b), - static_cast(0x27), - static_cast(0xfcu), - static_cast(0xffu), // 32 bit sec - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; - char packed_micro[] = { - static_cast(0xd7u), - static_cast(-1), - static_cast(0xeeu), // 999,999,000 << 2 - static_cast(0x6b), - static_cast(0x18), - static_cast(0x60), - static_cast(0xffu), // 32 bit sec - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; + val1 += std::chrono::duration_cast( + std::chrono::nanoseconds(0x3b9ac9ffL) // 999,999,999 + ); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + char packed_nano[] = { static_cast(0xd7u), static_cast(-1), + static_cast(0xeeu), // 999,999,999 << 2 + static_cast(0x6b), static_cast(0x27), static_cast(0xfcu), + static_cast(0xffu), // 32 bit sec + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; + char packed_micro[] = { static_cast(0xd7u), static_cast(-1), + static_cast(0xeeu), // 999,999,000 << 2 + static_cast(0x6b), static_cast(0x18), static_cast(0x60), + static_cast(0xffu), // 32 bit sec + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; if (std::chrono::steady_clock::duration::period::ratio::den == 1000000000) { BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed_nano, sizeof(packed_nano)), 0); - } - else if (std::chrono::steady_clock::duration::period::ratio::den == 1000000) { + } else if (std::chrono::steady_clock::duration::period::ratio::den == 1000000) { BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed_micro, sizeof(packed_micro)), 0); } - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1195,8 +1078,7 @@ BOOST_AUTO_TEST_CASE(steady_clock_impl_min) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1212,8 +1094,7 @@ BOOST_AUTO_TEST_CASE(steady_clock_impl_max) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1229,8 +1110,7 @@ BOOST_AUTO_TEST_CASE(steady_clock_impl_now) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::steady_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1239,15 +1119,13 @@ BOOST_AUTO_TEST_CASE(steady_clock_impl_now) BOOST_CHECK(val1 == val3); } - BOOST_AUTO_TEST_CASE(high_resolution_clock) { std::chrono::high_resolution_clock::time_point val1; msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1261,18 +1139,11 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_32) std::chrono::high_resolution_clock::time_point val1(std::chrono::seconds(0x12345678L)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - char packed[] = { - static_cast(0xd6u), - static_cast(-1), - static_cast(0x12), - static_cast(0x34), - static_cast(0x56), - static_cast(0x78) - }; + char packed[] = { static_cast(0xd6u), static_cast(-1), static_cast(0x12), + static_cast(0x34), static_cast(0x56), static_cast(0x78) }; BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed, sizeof(packed)), 0); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1286,18 +1157,11 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_32_max) std::chrono::high_resolution_clock::time_point val1(std::chrono::seconds(0xffffffffL)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - char packed[] = { - static_cast(0xd6u), - static_cast(-1), - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; + char packed[] = { static_cast(0xd6u), static_cast(-1), static_cast(0xffu), + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed, sizeof(packed)), 0); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1306,19 +1170,15 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_32_max) BOOST_CHECK(val1 == val3); } - BOOST_AUTO_TEST_CASE(high_resolution_clock_64) { std::chrono::high_resolution_clock::time_point val1(std::chrono::seconds(0x31234567L)); - val1 += - std::chrono::duration_cast( - std::chrono::nanoseconds(0x312345678L) - ); + val1 += std::chrono::duration_cast( + std::chrono::nanoseconds(0x312345678L)); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1331,45 +1191,28 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_64_max) { std::chrono::high_resolution_clock::time_point val1(std::chrono::seconds(0xffffffffL)); - val1 += - std::chrono::duration_cast( - std::chrono::nanoseconds(0x3b9ac9ffL) // 999,999,999 - ); - msgpack::sbuffer sbuf; - msgpack::pack(sbuf, val1); - char packed_nano[] = { - static_cast(0xd7u), - static_cast(-1), - static_cast(0xeeu), // 999,999,999 << 2 - static_cast(0x6b), - static_cast(0x27), - static_cast(0xfcu), - static_cast(0xffu), // 32 bit sec - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; - char packed_micro[] = { - static_cast(0xd7u), - static_cast(-1), - static_cast(0xeeu), // 999,999,000 << 2 - static_cast(0x6b), - static_cast(0x18), - static_cast(0x60), - static_cast(0xffu), // 32 bit sec - static_cast(0xffu), - static_cast(0xffu), - static_cast(0xffu) - }; + val1 += std::chrono::duration_cast( + std::chrono::nanoseconds(0x3b9ac9ffL) // 999,999,999 + ); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + char packed_nano[] = { static_cast(0xd7u), static_cast(-1), + static_cast(0xeeu), // 999,999,999 << 2 + static_cast(0x6b), static_cast(0x27), static_cast(0xfcu), + static_cast(0xffu), // 32 bit sec + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; + char packed_micro[] = { static_cast(0xd7u), static_cast(-1), + static_cast(0xeeu), // 999,999,000 << 2 + static_cast(0x6b), static_cast(0x18), static_cast(0x60), + static_cast(0xffu), // 32 bit sec + static_cast(0xffu), static_cast(0xffu), static_cast(0xffu) }; if (std::chrono::high_resolution_clock::duration::period::ratio::den == 1000000000) { BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed_nano, sizeof(packed_nano)), 0); - } - else if (std::chrono::high_resolution_clock::duration::period::ratio::den == 1000000) { + } else if (std::chrono::high_resolution_clock::duration::period::ratio::den == 1000000) { BOOST_CHECK_EQUAL(memcmp(sbuf.data(), packed_micro, sizeof(packed_micro)), 0); } - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1384,8 +1227,7 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_impl_min) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1401,8 +1243,7 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_impl_max) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1418,8 +1259,7 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_impl_now) msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); - msgpack::object_handle oh = - msgpack::unpack(sbuf.data(), sbuf.size()); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); std::chrono::high_resolution_clock::time_point val2 = oh.get().as(); BOOST_CHECK(val1 == val2); @@ -1428,9 +1268,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) { std::stringstream ss; @@ -1460,7 +1297,9 @@ BOOST_AUTO_TEST_CASE(timespec_object_with_zone_zero) BOOST_AUTO_TEST_CASE(timespec_pack_convert_32bit_sec) { std::stringstream ss; - timespec val1{ std::numeric_limits().tv_sec)>::is_signed ? time_t(INT32_MAX) : time_t(UINT32_MAX), 0 }; + timespec val1{ std::numeric_limits().tv_sec)>::is_signed ? time_t(INT32_MAX) + : time_t(UINT32_MAX), + 0 }; msgpack::pack(ss, val1); std::string const& str = ss.str(); @@ -1476,7 +1315,9 @@ BOOST_AUTO_TEST_CASE(timespec_pack_convert_32bit_sec) BOOST_AUTO_TEST_CASE(timespec_object_with_zone_32bit_sec) { msgpack::zone z; - timespec val1{ std::numeric_limits().tv_sec)>::is_signed ? time_t(INT32_MAX) : time_t(UINT32_MAX), 0 }; + timespec val1{ std::numeric_limits().tv_sec)>::is_signed ? time_t(INT32_MAX) + : time_t(UINT32_MAX), + 0 }; msgpack::object obj(val1, z); timespec val2 = obj.as(); BOOST_CHECK_EQUAL(val1.tv_sec, val2.tv_sec); @@ -1510,7 +1351,8 @@ BOOST_AUTO_TEST_CASE(timespec_object_with_zone_max_nano) BOOST_AUTO_TEST_CASE(timespec_pack_convert_34bit_sec_max_nano) { - if (sizeof(decltype(std::declval().tv_sec)) <= 4) return; + if (sizeof(decltype(std::declval().tv_sec)) <= 4) + return; std::stringstream ss; timespec val1{ static_cast().tv_sec)>(0x3ffffffffULL), 999999999 }; @@ -1527,7 +1369,8 @@ BOOST_AUTO_TEST_CASE(timespec_pack_convert_34bit_sec_max_nano) BOOST_AUTO_TEST_CASE(timespec_object_with_zone_34bit_sec_max_nano) { - if (sizeof(decltype(std::declval().tv_sec)) <= 4) return; + if (sizeof(decltype(std::declval().tv_sec)) <= 4) + return; msgpack::zone z; timespec val1{ static_cast().tv_sec)>(0x3ffffffffULL), 999999999 }; msgpack::object obj(val1, z); @@ -1538,7 +1381,8 @@ BOOST_AUTO_TEST_CASE(timespec_object_with_zone_34bit_sec_max_nano) BOOST_AUTO_TEST_CASE(timespec_pack_convert_35bit_sec_max_nano) { - if (sizeof(decltype(std::declval().tv_sec)) <= 4) return; + if (sizeof(decltype(std::declval().tv_sec)) <= 4) + return; std::stringstream ss; timespec val1{ static_cast().tv_sec)>(0x7ffffffffULL), 999999999 }; @@ -1555,7 +1399,8 @@ BOOST_AUTO_TEST_CASE(timespec_pack_convert_35bit_sec_max_nano) BOOST_AUTO_TEST_CASE(timespec_object_with_zone_35bit_sec_max_nano) { - if (sizeof(decltype(std::declval().tv_sec)) <= 4) return; + if (sizeof(decltype(std::declval().tv_sec)) <= 4) + return; msgpack::zone z; timespec val1{ static_cast().tv_sec)>(0x7ffffffffULL), 999999999 }; msgpack::object obj(val1, z); @@ -1566,7 +1411,8 @@ BOOST_AUTO_TEST_CASE(timespec_object_with_zone_35bit_sec_max_nano) BOOST_AUTO_TEST_CASE(timespec_pack_convert_64bit_sec_max_nano) { - if (sizeof(decltype(std::declval().tv_sec)) <= 4) return; + if (sizeof(decltype(std::declval().tv_sec)) <= 4) + return; std::stringstream ss; timespec val1{ std::numeric_limits().tv_sec)>::max(), 999999999 }; @@ -1591,5 +1437,4 @@ BOOST_AUTO_TEST_CASE(timespec_object_with_zone_64bit_sec_max_nano) BOOST_CHECK_EQUAL(val1.tv_nsec, val2.tv_nsec); } - #endif // !defined(MSGPACK_USE_CPP03) diff --git a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp index c90a55ad4aee..5d0ebf6af4e7 100644 --- a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp +++ b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp @@ -8,9 +8,7 @@ #endif // For C++ standards lower than C++17 -BOOST_AUTO_TEST_CASE(dummy) -{ -} +BOOST_AUTO_TEST_CASE(dummy) {} #if MSGPACK_CPP_VERSION >= 201703 @@ -24,9 +22,8 @@ BOOST_AUTO_TEST_CASE(optional_pack_convert_nil) std::optional val1; msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); - std::optional val2 = oh.get().as >(); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); + std::optional val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } @@ -36,15 +33,14 @@ BOOST_AUTO_TEST_CASE(optional_pack_convert_int) std::optional val1 = 1; msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); - std::optional val2 = oh.get().as >(); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); + std::optional val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(optional_pack_convert_vector) { - typedef std::optional > ovi_t; + typedef std::optional> ovi_t; std::stringstream ss; ovi_t val1; std::vector v; @@ -54,15 +50,14 @@ BOOST_AUTO_TEST_CASE(optional_pack_convert_vector) val1 = v; msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); - ovi_t val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); + ovi_t val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(optional_pack_convert_vector_optional) { - typedef std::vector > voi_t; + typedef std::vector> voi_t; std::stringstream ss; voi_t val1; val1.resize(3); @@ -70,9 +65,8 @@ BOOST_AUTO_TEST_CASE(optional_pack_convert_vector_optional) val1[2] = 3; msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); - voi_t val2 = oh.get().as(); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); + voi_t val2 = oh.get().as(); BOOST_CHECK(val1 == val2); } @@ -80,7 +74,7 @@ BOOST_AUTO_TEST_CASE(optional_object_nil) { std::optional val1; msgpack::object obj(val1); - std::optional val2 = obj.as >(); + std::optional val2 = obj.as>(); BOOST_CHECK(val1 == val2); } @@ -88,7 +82,7 @@ BOOST_AUTO_TEST_CASE(optional_object_int) { std::optional val1 = 1; msgpack::object obj(val1); - std::optional val2 = obj.as >(); + std::optional val2 = obj.as>(); BOOST_CHECK(val1 == val2); } @@ -114,7 +108,7 @@ BOOST_AUTO_TEST_CASE(optional_object_with_zone_nil) msgpack::zone z; std::optional val1; msgpack::object obj(val1, z); - std::optional val2 = obj.as >(); + std::optional val2 = obj.as>(); BOOST_CHECK(val1 == val2); } @@ -123,54 +117,61 @@ BOOST_AUTO_TEST_CASE(optional_object_with_zone_int) msgpack::zone z; std::optional val1 = 1; msgpack::object obj(val1, z); - std::optional val2 = obj.as >(); + std::optional val2 = obj.as>(); BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(optional_object_with_zone_vector_optional) { - typedef std::vector > voi_t; + typedef std::vector> voi_t; msgpack::zone z; voi_t val1; val1.resize(3); val1[0] = 1; val1[2] = 3; msgpack::object obj(val1, z); - voi_t val2 = obj.as(); + voi_t val2 = obj.as(); BOOST_CHECK(val1 == val2); } struct no_def_con { no_def_con() = delete; - no_def_con(int i):i(i) {} + no_def_con(int i) + : i(i) + {} int i; MSGPACK_DEFINE(i); }; -inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) { +inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) +{ return lhs.i == rhs.i; } -inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) { +inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) +{ return !(lhs == rhs); } namespace msgpack { -MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { -namespace adaptor { - -template <> -struct as { - no_def_con operator()(msgpack::object const& o) const { - if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); - if (o.via.array.size != 1) throw msgpack::type_error(); - return no_def_con(o.via.array.ptr[0].as()); - } -}; +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) +{ + namespace adaptor { + + template <> struct as { + no_def_con operator()(msgpack::object const& o) const + { + if (o.type != msgpack::type::ARRAY) + throw msgpack::type_error(); + if (o.via.array.size != 1) + throw msgpack::type_error(); + return no_def_con(o.via.array.ptr[0].as()); + } + }; -} // adaptor + } // namespace adaptor } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) -} // msgpack +} // namespace msgpack BOOST_AUTO_TEST_CASE(optional_pack_convert_no_def_con) { @@ -178,8 +179,7 @@ BOOST_AUTO_TEST_CASE(optional_pack_convert_no_def_con) std::optional val1 = no_def_con(1); msgpack::pack(ss, val1); std::string const& str = ss.str(); - msgpack::object_handle oh = - msgpack::unpack(str.data(), str.size()); + msgpack::object_handle oh = msgpack::unpack(str.data(), str.size()); std::optional val2 = oh.get().as>(); BOOST_CHECK(val1 == val2); } @@ -227,7 +227,7 @@ BOOST_AUTO_TEST_CASE(string_view_object_with_zone) BOOST_AUTO_TEST_CASE(byte_pack_convert) { std::stringstream ss; - std::byte val1{0xff}; + std::byte val1{ 0xff }; msgpack::pack(ss, val1); @@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE(byte_pack_convert) BOOST_AUTO_TEST_CASE(byte_object) { - std::byte val1{0x00}; + std::byte val1{ 0x00 }; msgpack::object obj(val1); std::byte val2 = obj.as(); BOOST_CHECK(val1 == val2); @@ -249,7 +249,7 @@ BOOST_AUTO_TEST_CASE(byte_object) BOOST_AUTO_TEST_CASE(byte_object_with_zone) { msgpack::zone z; - std::byte val1{80}; + std::byte val1{ 80 }; msgpack::object obj(val1, z); std::byte val2 = obj.as(); BOOST_CHECK(val1 == val2); @@ -259,7 +259,7 @@ BOOST_AUTO_TEST_CASE(vector_byte_pack_convert) { std::stringstream ss; std::vector val1{ - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; msgpack::pack(ss, val1); @@ -280,7 +280,7 @@ BOOST_AUTO_TEST_CASE(vector_byte_pack_convert) BOOST_AUTO_TEST_CASE(vector_byte_object) { std::vector val1{ - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; // Caller need to manage val1's lifetime. The Data is not copied. @@ -294,7 +294,7 @@ BOOST_AUTO_TEST_CASE(vector_byte_object_with_zone) { msgpack::zone z; std::vector val1{ - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; msgpack::object obj(val1, z); @@ -306,7 +306,7 @@ BOOST_AUTO_TEST_CASE(array_byte_pack_convert) { std::stringstream ss; std::array val1{ - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; msgpack::pack(ss, val1); @@ -327,8 +327,8 @@ BOOST_AUTO_TEST_CASE(array_byte_pack_convert) { msgpack::object_handle oh; msgpack::unpack(oh, str.data(), str.size()); - BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); - BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); + BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); + BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); } } @@ -336,7 +336,7 @@ BOOST_AUTO_TEST_CASE(array_byte_pack_convert) BOOST_AUTO_TEST_CASE(array_byte_object) { std::array val1{ - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; // Caller need to manage val1's lifetime. The Data is not copied. @@ -345,8 +345,8 @@ BOOST_AUTO_TEST_CASE(array_byte_object) auto val2 = obj.as>(); BOOST_CHECK(val1 == val2); - BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); - BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); } @@ -354,15 +354,15 @@ BOOST_AUTO_TEST_CASE(array_byte_object_with_zone) { msgpack::zone z; std::array val1{ - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; msgpack::object obj(val1, z); auto val2 = obj.as>(); BOOST_CHECK(val1 == val2); - BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); - BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); } @@ -389,7 +389,7 @@ BOOST_AUTO_TEST_CASE(array_byte_empty_pack_convert) { msgpack::object_handle oh; msgpack::unpack(oh, str.data(), str.size()); - BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); + BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); } } @@ -404,7 +404,7 @@ BOOST_AUTO_TEST_CASE(array_byte_empty_object) auto val2 = obj.as>(); BOOST_CHECK(val1 == val2); - BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); } @@ -417,7 +417,7 @@ BOOST_AUTO_TEST_CASE(array_byte_empty_object_with_zone) auto val2 = obj.as>(); BOOST_CHECK(val1 == val2); - BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); } @@ -425,7 +425,7 @@ BOOST_AUTO_TEST_CASE(carray_byte_pack_convert) { std::stringstream ss; std::byte val1[] = { - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; msgpack::pack(ss, val1); @@ -450,7 +450,7 @@ BOOST_AUTO_TEST_CASE(carray_byte_object_with_zone) { msgpack::zone z; std::byte val1[] = { - std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff} + std::byte{ 0x01 }, std::byte{ 0x02 }, std::byte{ 0x7f }, std::byte{ 0x80 }, std::byte{ 0xff } }; msgpack::object obj(val1, z); @@ -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