2
0
mirror of https://github.com/boostorg/json.git synced 2026-02-20 14:52:14 +00:00

storage coverage

This commit is contained in:
Vinnie Falco
2019-10-20 21:05:55 -07:00
parent 38cbceadaa
commit 9fc0ef4d3e
7 changed files with 132 additions and 204 deletions

View File

@@ -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 <boost/json/detail/config.hpp>
#include <boost/json/storage.hpp>
#include <boost/align/align_up.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/config.hpp>
#include <boost/assert.hpp>
#include <atomic>
#include <cstddef>
#ifdef BOOST_NO_CXX11_ALLOCATOR
#include <boost/container/allocator_traits.hpp>
#else
#include <memory>
#endif
namespace boost {
namespace json {
namespace detail {
#ifdef BOOST_NO_CXX11_ALLOCATOR
template<class Allocator>
using allocator_of_char =
typename boost::container::allocator_traits<
Allocator>::template rebind_alloc<char>;
#else
template<class Allocator>
using allocator_of_char =
typename std::allocator_traits<
Allocator>::template rebind_alloc<char>;
#endif
template<class Allocator>
struct storage_adaptor
: storage
, boost::empty_value<
allocator_of_char<Allocator>>
{
// VFALCO This is all public because msvc friend bugs
explicit
storage_adaptor(Allocator const& alloc)
: boost::empty_value<
allocator_of_char<Allocator>>(
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

View File

@@ -10,7 +10,6 @@
#ifndef BOOST_JSON_IMPL_STORAGE_HPP
#define BOOST_JSON_IMPL_STORAGE_HPP
#include <boost/json/detail/storage_adaptor.hpp>
#include <utility>
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>(args)...));
}
template<class Allocator>
storage_ptr
make_storage_adaptor(Allocator const& a)
{
return make_storage<
detail::storage_adaptor<Allocator>>(a);
}
} // json
} // boost

View File

@@ -11,7 +11,6 @@
#define BOOST_JSON_IMPL_STORAGE_IPP
#include <boost/json/storage.hpp>
#include <boost/json/detail/storage_adaptor.hpp>
#include <memory>
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<char*>(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<void>());
make_storage<builtin>();
return sp;
}
@@ -35,8 +66,7 @@ storage_ptr&
raw_default_storage() noexcept
{
static storage_ptr sp =
make_storage_adaptor(
std::allocator<void>());
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

View File

@@ -12,6 +12,7 @@
#include <boost/core/exchange.hpp>
#include <boost/core/ignore_unused.hpp>
#include <boost/assert.hpp>
#include <algorithm>
#include <new>
#include <ostream>

View File

@@ -30,11 +30,11 @@ class storage
BOOST_JSON_DECL
void
addref();
addref() noexcept;
BOOST_JSON_DECL
void
release();
release() noexcept;
template<class T>
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<class T, class U>
bool
operator==(
basic_storage_ptr<T> const& lhs,
basic_storage_ptr<U> const& rhs) noexcept
{
return lhs.get() == rhs.get();
}
template<class T, class U>
bool
operator!=(
basic_storage_ptr<T> const& lhs,
basic_storage_ptr<U> const& rhs) noexcept
{
return lhs.get() != rhs.get();
}
template<class T>
bool
operator==(
basic_storage_ptr<T> const& lhs,
std::nullptr_t) noexcept
{
return lhs.get() == nullptr;
}
template<class T>
bool
operator!=(
basic_storage_ptr<T> const& lhs,
std::nullptr_t) noexcept
{
return lhs.get() != nullptr;
}
template<class T>
bool
operator==(
std::nullptr_t,
basic_storage_ptr<T> const& rhs) noexcept
{
return rhs.get() == nullptr;
}
template<class T>
bool
operator!=(
std::nullptr_t,
basic_storage_ptr<T> const& rhs) noexcept
{
return rhs.get() == nullptr;
}
using storage_ptr = basic_storage_ptr<storage>;
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<class Allocator>
storage_ptr
make_storage_adaptor(Allocator const& a);
/** Return a pointer to the current default storage
*/
BOOST_JSON_DECL

View File

@@ -20,7 +20,7 @@
#include <boost/json/string.hpp>
#include <boost/json/detail/is_specialized.hpp>
#include <boost/json/detail/value.hpp>
#include <boost/type_traits/make_void.hpp>
#include <boost/assert.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/pilfer.hpp>
#include <cstdlib>