From 9fc0ef4d3ec8705e31d0785de03174ef0f106b66 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 20 Oct 2019 21:05:55 -0700 Subject: [PATCH] storage coverage --- include/boost/json/detail/storage_adaptor.hpp | 104 ------------------ include/boost/json/impl/storage.hpp | 11 +- include/boost/json/impl/storage.ipp | 90 ++++++--------- include/boost/json/impl/string.ipp | 1 + include/boost/json/storage.hpp | 90 +++++++++------ include/boost/json/value.hpp | 2 +- test/storage.cpp | 38 ++++++- 7 files changed, 132 insertions(+), 204 deletions(-) delete mode 100644 include/boost/json/detail/storage_adaptor.hpp diff --git a/include/boost/json/detail/storage_adaptor.hpp b/include/boost/json/detail/storage_adaptor.hpp deleted file mode 100644 index 83ebfb57..00000000 --- a/include/boost/json/detail/storage_adaptor.hpp +++ /dev/null @@ -1,104 +0,0 @@ -// -// Copyright (c) 2018-2019 Vinnie Falco (vinnie dot falco at gmail dot com) -// -// 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) -// -// Official repository: https://github.com/vinniefalco/json -// - -#ifndef BOOST_JSON_DETAIL_STORAGE_ADAPTOR_HPP -#define BOOST_JSON_DETAIL_STORAGE_ADAPTOR_HPP - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef BOOST_NO_CXX11_ALLOCATOR -#include -#else -#include -#endif - -namespace boost { -namespace json { -namespace detail { - -#ifdef BOOST_NO_CXX11_ALLOCATOR -template -using allocator_of_char = - typename boost::container::allocator_traits< - Allocator>::template rebind_alloc; - -#else -template -using allocator_of_char = - typename std::allocator_traits< - Allocator>::template rebind_alloc; - -#endif - -template -struct storage_adaptor - : storage - , boost::empty_value< - allocator_of_char> -{ - // VFALCO This is all public because msvc friend bugs - - explicit - storage_adaptor(Allocator const& alloc) - : boost::empty_value< - allocator_of_char>( - boost::empty_init_t{}, alloc) - { - } - - void* - allocate( - std::size_t n, - std::size_t align) override - { - auto const n1 = - boost::alignment::align_up(n, align); - BOOST_ASSERT(n1 >= n); - return this->get().allocate(n1); - } - - void - deallocate( - void* p, - std::size_t n, - std::size_t) noexcept override - { - this->get().deallocate( - reinterpret_cast< - char*>(p), n); - } - - bool - is_equal( - storage const& other) const noexcept override - { - auto p = dynamic_cast< - storage_adaptor const*>(&other); - if(! p) - return false; - //return this->get() == p->get(); - // VFALCO We require pointer equality - // to prevent objects from different - // "documents" getting mixed together. - return this == p; - } -}; - -} // detail -} // json -} // boost - -#endif diff --git a/include/boost/json/impl/storage.hpp b/include/boost/json/impl/storage.hpp index aa115337..8a4e3401 100644 --- a/include/boost/json/impl/storage.hpp +++ b/include/boost/json/impl/storage.hpp @@ -10,7 +10,6 @@ #ifndef BOOST_JSON_IMPL_STORAGE_HPP #define BOOST_JSON_IMPL_STORAGE_HPP -#include #include namespace boost { @@ -20,7 +19,7 @@ namespace detail { BOOST_JSON_DECL storage_ptr const& -global_storage(); +global_storage() noexcept; } // detail @@ -32,14 +31,6 @@ make_storage(Args&&... args) new Storage(std::forward(args)...)); } -template -storage_ptr -make_storage_adaptor(Allocator const& a) -{ - return make_storage< - detail::storage_adaptor>(a); -} - } // json } // boost diff --git a/include/boost/json/impl/storage.ipp b/include/boost/json/impl/storage.ipp index d408a872..28141896 100644 --- a/include/boost/json/impl/storage.ipp +++ b/include/boost/json/impl/storage.ipp @@ -11,7 +11,6 @@ #define BOOST_JSON_IMPL_STORAGE_IPP #include -#include #include namespace boost { @@ -24,9 +23,41 @@ namespace detail { storage_ptr const& global_storage() noexcept { + struct builtin : storage + { + void* + allocate( + std::size_t n, + std::size_t) override + { + return std::allocator< + char>().allocate(n); + } + + void + deallocate( + void* p, + std::size_t n, + std::size_t) noexcept override + { + std::allocator< + char>().deallocate( + static_cast(p), n); + } + + bool + is_equal( + storage const& other) const noexcept + { + auto p = dynamic_cast< + builtin const*>(&other); + if(! p) + return false; + return true; + } + }; static storage_ptr const sp = - make_storage_adaptor( - std::allocator()); + make_storage(); return sp; } @@ -35,8 +66,7 @@ storage_ptr& raw_default_storage() noexcept { static storage_ptr sp = - make_storage_adaptor( - std::allocator()); + global_storage(); return sp; } @@ -78,56 +108,6 @@ storage() noexcept { } -//---------------------------------------------------------- - -bool -operator==( - storage_ptr const& lhs, - storage_ptr const& rhs) noexcept -{ - return lhs.get() == rhs.get(); -} - -bool -operator==( - storage* lhs, - storage_ptr const& rhs) noexcept -{ - return lhs == rhs.get(); -} - -bool -operator==( - storage_ptr const& lhs, - storage* rhs) noexcept -{ - return lhs.get() == rhs; -} - -bool -operator!=( - storage_ptr const& lhs, - storage_ptr const& rhs) noexcept -{ - return lhs.get() != rhs.get(); -} - -bool -operator!=( - storage* lhs, - storage_ptr const& rhs) noexcept -{ - return lhs != rhs.get(); -} - -bool -operator!=( - storage_ptr const& lhs, - storage* rhs) noexcept -{ - return lhs.get() != rhs; -} - } // json } // boost diff --git a/include/boost/json/impl/string.ipp b/include/boost/json/impl/string.ipp index f885d309..f0f5c3d5 100644 --- a/include/boost/json/impl/string.ipp +++ b/include/boost/json/impl/string.ipp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include diff --git a/include/boost/json/storage.hpp b/include/boost/json/storage.hpp index cf4292f3..1d90b9e4 100644 --- a/include/boost/json/storage.hpp +++ b/include/boost/json/storage.hpp @@ -30,11 +30,11 @@ class storage BOOST_JSON_DECL void - addref(); + addref() noexcept; BOOST_JSON_DECL void - release(); + release() noexcept; template friend class basic_storage_ptr; @@ -44,7 +44,7 @@ public: sizeof(max_align_t); BOOST_JSON_DECL - storage(); + storage() noexcept; virtual ~storage() = default; @@ -223,40 +223,64 @@ public: make_storage(Args&&... args); }; +template +bool +operator==( + basic_storage_ptr const& lhs, + basic_storage_ptr const& rhs) noexcept +{ + return lhs.get() == rhs.get(); +} + +template +bool +operator!=( + basic_storage_ptr const& lhs, + basic_storage_ptr const& rhs) noexcept +{ + return lhs.get() != rhs.get(); +} + +template +bool +operator==( + basic_storage_ptr const& lhs, + std::nullptr_t) noexcept +{ + return lhs.get() == nullptr; +} + +template +bool +operator!=( + basic_storage_ptr const& lhs, + std::nullptr_t) noexcept +{ + return lhs.get() != nullptr; +} + +template +bool +operator==( + std::nullptr_t, + basic_storage_ptr const& rhs) noexcept +{ + return rhs.get() == nullptr; +} + +template +bool +operator!=( + std::nullptr_t, + basic_storage_ptr const& rhs) noexcept +{ + return rhs.get() == nullptr; +} + using storage_ptr = basic_storage_ptr; -BOOST_JSON_DECL -bool -operator==(storage_ptr const& lhs, storage_ptr const& rhs) noexcept; - -BOOST_JSON_DECL -bool -operator==(storage* lhs, storage_ptr const& rhs) noexcept; - -BOOST_JSON_DECL -bool -operator==(storage_ptr const& lhs, storage* rhs) noexcept; - -BOOST_JSON_DECL -bool -operator!=(storage_ptr const& lhs, storage_ptr const& rhs) noexcept; - -BOOST_JSON_DECL -bool -operator!=(storage* lhs, storage_ptr const& rhs) noexcept; - -BOOST_JSON_DECL -bool -operator!=(storage_ptr const& lhs, storage* rhs) noexcept; - //---------------------------------------------------------- -/** Construct a storage adaptor for the specified allocator -*/ -template -storage_ptr -make_storage_adaptor(Allocator const& a); - /** Return a pointer to the current default storage */ BOOST_JSON_DECL diff --git a/include/boost/json/value.hpp b/include/boost/json/value.hpp index 3385cd4a..43ad149c 100644 --- a/include/boost/json/value.hpp +++ b/include/boost/json/value.hpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/storage.cpp b/test/storage.cpp index 9d6c4498..dd777021 100644 --- a/test/storage.cpp +++ b/test/storage.cpp @@ -11,7 +11,6 @@ #include #include -#include #include "test.hpp" @@ -21,9 +20,46 @@ namespace json { class storage_test : public beast::unit_test::suite { public: + struct throwing : storage + { + throwing() + { + throw std::exception{}; + } + + void* + allocate( + std::size_t, + std::size_t) override + { + return nullptr; + } + + void + deallocate( + void*, + std::size_t, + std::size_t) noexcept override + { + } + + bool + is_equal( + storage const&) const noexcept + { + return true; + } + }; + void run() override { + { + BEAST_THROWS( + make_storage(), + std::exception); + } + basic_storage_ptr sp = make_storage(); pass();