This commit is contained in:
2026-03-23 20:54:41 +08:00
commit e13b3650e9
4596 changed files with 1015768 additions and 0 deletions

View File

@@ -0,0 +1,180 @@
/*
Copyright 2017-2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_SMART_PTR_ALLOCATE_LOCAL_SHARED_ARRAY_HPP
#define BOOST_SMART_PTR_ALLOCATE_LOCAL_SHARED_ARRAY_HPP
#include <boost/smart_ptr/allocate_shared_array.hpp>
#include <boost/smart_ptr/local_shared_ptr.hpp>
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <type_traits>
namespace boost {
namespace detail {
class BOOST_SYMBOL_VISIBLE lsp_array_base
: public local_counted_base {
public:
void set(sp_counted_base* base) noexcept {
count_ = shared_count(base);
}
void local_cb_destroy() noexcept override {
shared_count().swap(count_);
}
shared_count local_cb_get_shared_count() const
noexcept override {
return count_;
}
private:
shared_count count_;
};
template<class A>
class lsp_array_state
: public sp_array_state<A> {
public:
template<class U>
lsp_array_state(const U& other, std::size_t size) noexcept
: sp_array_state<A>(other, size) { }
lsp_array_base& base() noexcept {
return base_;
}
private:
lsp_array_base base_;
};
template<class A, std::size_t N>
class lsp_size_array_state
: public sp_size_array_state<A, N> {
public:
template<class U>
lsp_size_array_state(const U& other, std::size_t size) noexcept
: sp_size_array_state<A, N>(other, size) { }
lsp_array_base& base() noexcept {
return base_;
}
private:
lsp_array_base base_;
};
} /* detail */
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
local_shared_ptr<T> >::type
allocate_local_shared(const A& allocator, std::size_t count)
{
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::lsp_array_state<other> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count);
detail::lsp_array_base& local = node->state().base();
local.set(node);
result.release();
return local_shared_ptr<T>(detail::lsp_internal_constructor_tag(), start,
&local);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
local_shared_ptr<T> >::type
allocate_local_shared(const A& allocator)
{
enum {
count = std::extent<T>::value
};
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::lsp_size_array_state<other, count> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count);
detail::lsp_array_base& local = node->state().base();
local.set(node);
result.release();
return local_shared_ptr<T>(detail::lsp_internal_constructor_tag(), start,
&local);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
local_shared_ptr<T> >::type
allocate_local_shared(const A& allocator, std::size_t count,
const typename std::remove_extent<T>::type& value)
{
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::lsp_array_state<other> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count, value);
detail::lsp_array_base& local = node->state().base();
local.set(node);
result.release();
return local_shared_ptr<T>(detail::lsp_internal_constructor_tag(), start,
&local);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
local_shared_ptr<T> >::type
allocate_local_shared(const A& allocator,
const typename std::remove_extent<T>::type& value)
{
enum {
count = std::extent<T>::value
};
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::lsp_size_array_state<other, count> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count, value);
detail::lsp_array_base& local = node->state().base();
local.set(node);
result.release();
return local_shared_ptr<T>(detail::lsp_internal_constructor_tag(), start,
&local);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
local_shared_ptr<T> >::type
allocate_local_shared_noinit(const A& allocator, std::size_t count)
{
return boost::allocate_local_shared<T>(boost::noinit_adapt(allocator),
count);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
local_shared_ptr<T> >::type
allocate_local_shared_noinit(const A& allocator)
{
return boost::allocate_local_shared<T>(boost::noinit_adapt(allocator));
}
} /* boost */
#endif

View File

@@ -0,0 +1,346 @@
/*
Copyright 2012-2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP
#define BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP
#include <boost/core/allocator_access.hpp>
#include <boost/core/alloc_construct.hpp>
#include <boost/core/first_scalar.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <type_traits>
namespace boost {
namespace detail {
template<class T>
struct sp_array_element {
typedef typename std::remove_cv<typename
std::remove_extent<T>::type>::type type;
};
template<class T>
struct sp_array_count {
enum {
value = 1
};
};
template<class T, std::size_t N>
struct sp_array_count<T[N]> {
enum {
value = N * sp_array_count<T>::value
};
};
template<std::size_t N, std::size_t M>
struct sp_max_size {
enum {
value = N < M ? M : N
};
};
template<std::size_t N, std::size_t M>
struct sp_align_up {
enum {
value = (N + M - 1) & ~(M - 1)
};
};
template<class T>
constexpr inline std::size_t
sp_objects(std::size_t size) noexcept
{
return (size + sizeof(T) - 1) / sizeof(T);
}
template<class A>
class sp_array_state {
public:
typedef A type;
template<class U>
sp_array_state(const U& _allocator, std::size_t _size) noexcept
: allocator_(_allocator),
size_(_size) { }
A& allocator() noexcept {
return allocator_;
}
std::size_t size() const noexcept {
return size_;
}
private:
A allocator_;
std::size_t size_;
};
template<class A, std::size_t N>
class sp_size_array_state {
public:
typedef A type;
template<class U>
sp_size_array_state(const U& _allocator, std::size_t) noexcept
: allocator_(_allocator) { }
A& allocator() noexcept {
return allocator_;
}
constexpr std::size_t size() const noexcept {
return N;
}
private:
A allocator_;
};
template<class T, class U>
struct sp_array_alignment {
enum {
value = sp_max_size<std::alignment_of<T>::value,
std::alignment_of<U>::value>::value
};
};
template<class T, class U>
struct sp_array_offset {
enum {
value = sp_align_up<sizeof(T), sp_array_alignment<T, U>::value>::value
};
};
template<class U, class T>
inline U*
sp_array_start(T* base) noexcept
{
enum {
size = sp_array_offset<T, U>::value
};
return reinterpret_cast<U*>(reinterpret_cast<char*>(base) + size);
}
template<class A, class T>
class sp_array_creator {
typedef typename A::value_type element;
enum {
offset = sp_array_offset<T, element>::value
};
typedef typename sp_type_with_alignment<sp_array_alignment<T,
element>::value>::type type;
public:
template<class U>
sp_array_creator(const U& other, std::size_t size) noexcept
: other_(other),
size_(sp_objects<type>(offset + sizeof(element) * size)) { }
T* create() {
return reinterpret_cast<T*>(other_.allocate(size_));
}
void destroy(T* base) {
other_.deallocate(reinterpret_cast<type*>(base), size_);
}
private:
typename boost::allocator_rebind<A, type>::type other_;
std::size_t size_;
};
template<class T>
class BOOST_SYMBOL_VISIBLE sp_array_base
: public sp_counted_base {
typedef typename T::type allocator;
public:
typedef typename allocator::value_type type;
template<class A>
sp_array_base(const A& other, type* start, std::size_t size)
: state_(other, size) {
boost::alloc_construct_n(state_.allocator(),
boost::first_scalar(start),
state_.size() * sp_array_count<type>::value);
}
template<class A, class U>
sp_array_base(const A& other, type* start, std::size_t size, const U& list)
: state_(other, size) {
enum {
count = sp_array_count<type>::value
};
boost::alloc_construct_n(state_.allocator(),
boost::first_scalar(start), state_.size() * count,
boost::first_scalar(&list), count);
}
T& state() noexcept {
return state_;
}
void dispose() noexcept override {
boost::alloc_destroy_n(state_.allocator(),
boost::first_scalar(sp_array_start<type>(this)),
state_.size() * sp_array_count<type>::value);
}
void destroy() noexcept override {
sp_array_creator<allocator, sp_array_base> other(state_.allocator(),
state_.size());
this->~sp_array_base();
other.destroy(this);
}
void* get_deleter(const sp_typeinfo_&) noexcept override {
return 0;
}
void* get_local_deleter(const sp_typeinfo_&)
noexcept override {
return 0;
}
void* get_untyped_deleter() noexcept override {
return 0;
}
private:
T state_;
};
template<class A, class T>
struct sp_array_result {
public:
template<class U>
sp_array_result(const U& other, std::size_t size)
: creator_(other, size),
result_(creator_.create()) { }
~sp_array_result() {
if (result_) {
creator_.destroy(result_);
}
}
T* get() const noexcept {
return result_;
}
void release() noexcept {
result_ = 0;
}
private:
sp_array_result(const sp_array_result&);
sp_array_result& operator=(const sp_array_result&);
sp_array_creator<A, T> creator_;
T* result_;
};
} /* detail */
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value, shared_ptr<T> >::type
allocate_shared(const A& allocator, std::size_t count)
{
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::sp_array_state<other> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count);
result.release();
return shared_ptr<T>(detail::sp_internal_constructor_tag(), start,
detail::shared_count(static_cast<detail::sp_counted_base*>(node)));
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value, shared_ptr<T> >::type
allocate_shared(const A& allocator)
{
enum {
count = std::extent<T>::value
};
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::sp_size_array_state<other, std::extent<T>::value> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count);
result.release();
return shared_ptr<T>(detail::sp_internal_constructor_tag(), start,
detail::shared_count(static_cast<detail::sp_counted_base*>(node)));
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value, shared_ptr<T> >::type
allocate_shared(const A& allocator, std::size_t count,
const typename std::remove_extent<T>::type& value)
{
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::sp_array_state<other> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count, value);
result.release();
return shared_ptr<T>(detail::sp_internal_constructor_tag(), start,
detail::shared_count(static_cast<detail::sp_counted_base*>(node)));
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value, shared_ptr<T> >::type
allocate_shared(const A& allocator,
const typename std::remove_extent<T>::type& value)
{
enum {
count = std::extent<T>::value
};
typedef typename detail::sp_array_element<T>::type element;
typedef typename allocator_rebind<A, element>::type other;
typedef detail::sp_size_array_state<other, std::extent<T>::value> state;
typedef detail::sp_array_base<state> base;
detail::sp_array_result<other, base> result(allocator, count);
base* node = result.get();
element* start = detail::sp_array_start<element>(node);
::new(static_cast<void*>(node)) base(allocator, start, count, value);
result.release();
return shared_ptr<T>(detail::sp_internal_constructor_tag(), start,
detail::shared_count(static_cast<detail::sp_counted_base*>(node)));
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value, shared_ptr<T> >::type
allocate_shared_noinit(const A& allocator, std::size_t count)
{
return boost::allocate_shared<T>(boost::noinit_adapt(allocator), count);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value, shared_ptr<T> >::type
allocate_shared_noinit(const A& allocator)
{
return boost::allocate_shared<T>(boost::noinit_adapt(allocator));
}
} /* boost */
#endif

View File

@@ -0,0 +1,466 @@
/*
Copyright 2019-2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_SMART_PTR_ALLOCATE_UNIQUE_HPP
#define BOOST_SMART_PTR_ALLOCATE_UNIQUE_HPP
#include <boost/core/allocator_access.hpp>
#include <boost/core/alloc_construct.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/core/first_scalar.hpp>
#include <boost/core/noinit_adaptor.hpp>
#include <boost/core/pointer_traits.hpp>
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <boost/config.hpp>
#include <memory>
#include <utility>
#include <cstddef>
#include <type_traits>
namespace boost {
namespace detail {
template<class T>
struct sp_alloc_size {
static constexpr std::size_t value = 1;
};
template<class T>
struct sp_alloc_size<T[]> {
static constexpr std::size_t value = sp_alloc_size<T>::value;
};
template<class T, std::size_t N>
struct sp_alloc_size<T[N]> {
static constexpr std::size_t value = N * sp_alloc_size<T>::value;
};
template<class T>
struct sp_alloc_result {
typedef T type;
};
template<class T, std::size_t N>
struct sp_alloc_result<T[N]> {
typedef T type[];
};
template<class T>
struct sp_alloc_value {
typedef typename std::remove_cv<typename
std::remove_extent<T>::type>::type type;
};
template<class T, class P>
class sp_alloc_ptr {
public:
typedef T element_type;
sp_alloc_ptr() noexcept
: p_() { }
#if defined(BOOST_MSVC) && BOOST_MSVC == 1600
sp_alloc_ptr(T* p) noexcept
: p_(const_cast<typename std::remove_cv<T>::type*>(p)) { }
#endif
sp_alloc_ptr(std::size_t, P p) noexcept
: p_(p) { }
sp_alloc_ptr(std::nullptr_t) noexcept
: p_() { }
T& operator*() const {
return *p_;
}
T* operator->() const noexcept {
return boost::to_address(p_);
}
explicit operator bool() const noexcept {
return !!p_;
}
bool operator!() const noexcept {
return !p_;
}
P ptr() const noexcept {
return p_;
}
static constexpr std::size_t size() noexcept {
return 1;
}
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
static sp_alloc_ptr pointer_to(T& v) {
return sp_alloc_ptr(1,
std::pointer_traits<P>::pointer_to(const_cast<typename
std::remove_cv<T>::type&>(v)));
}
#endif
private:
P p_;
};
template<class T, class P>
class sp_alloc_ptr<T[], P> {
public:
typedef T element_type;
sp_alloc_ptr() noexcept
: p_() { }
sp_alloc_ptr(std::size_t n, P p) noexcept
: p_(p)
, n_(n) { }
sp_alloc_ptr(std::nullptr_t) noexcept
: p_() { }
T& operator[](std::size_t i) const {
return p_[i];
}
explicit operator bool() const noexcept {
return !!p_;
}
bool operator!() const noexcept {
return !p_;
}
P ptr() const noexcept {
return p_;
}
std::size_t size() const noexcept {
return n_;
}
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
static sp_alloc_ptr pointer_to(T& v) {
return sp_alloc_ptr(n_,
std::pointer_traits<P>::pointer_to(const_cast<typename
std::remove_cv<T>::type&>(v)));
}
#endif
private:
P p_;
std::size_t n_;
};
template<class T, std::size_t N, class P>
class sp_alloc_ptr<T[N], P> {
public:
typedef T element_type;
sp_alloc_ptr() noexcept
: p_() { }
sp_alloc_ptr(std::size_t, P p) noexcept
: p_(p) { }
sp_alloc_ptr(std::nullptr_t) noexcept
: p_() { }
T& operator[](std::size_t i) const {
return p_[i];
}
explicit operator bool() const noexcept {
return !!p_;
}
bool operator!() const noexcept {
return !p_;
}
P ptr() const noexcept {
return p_;
}
static constexpr std::size_t size() noexcept {
return N;
}
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
static sp_alloc_ptr pointer_to(T& v) {
return sp_alloc_ptr(N,
std::pointer_traits<P>::pointer_to(const_cast<typename
std::remove_cv<T>::type&>(v)));
}
#endif
private:
P p_;
};
template<class T, class P>
inline bool
operator==(const sp_alloc_ptr<T, P>& lhs, const sp_alloc_ptr<T, P>& rhs)
{
return lhs.ptr() == rhs.ptr();
}
template<class T, class P>
inline bool
operator!=(const sp_alloc_ptr<T, P>& lhs, const sp_alloc_ptr<T, P>& rhs)
{
return !(lhs == rhs);
}
template<class T, class P>
inline bool
operator==(const sp_alloc_ptr<T, P>& lhs,
std::nullptr_t) noexcept
{
return !lhs.ptr();
}
template<class T, class P>
inline bool
operator==(std::nullptr_t,
const sp_alloc_ptr<T, P>& rhs) noexcept
{
return !rhs.ptr();
}
template<class T, class P>
inline bool
operator!=(const sp_alloc_ptr<T, P>& lhs,
std::nullptr_t) noexcept
{
return !!lhs.ptr();
}
template<class T, class P>
inline bool
operator!=(std::nullptr_t,
const sp_alloc_ptr<T, P>& rhs) noexcept
{
return !!rhs.ptr();
}
template<class A>
inline void
sp_alloc_clear(A& a, typename boost::allocator_pointer<A>::type p, std::size_t,
std::false_type)
{
boost::alloc_destroy(a, boost::to_address(p));
}
template<class A>
inline void
sp_alloc_clear(A& a, typename boost::allocator_pointer<A>::type p,
std::size_t n, std::true_type)
{
#if defined(BOOST_MSVC) && BOOST_MSVC < 1800
if (!p) {
return;
}
#endif
boost::alloc_destroy_n(a, boost::first_scalar(boost::to_address(p)),
n * sp_alloc_size<typename A::value_type>::value);
}
} /* detail */
template<class T, class A>
class alloc_deleter
: empty_value<typename allocator_rebind<A,
typename detail::sp_alloc_value<T>::type>::type> {
typedef typename allocator_rebind<A,
typename detail::sp_alloc_value<T>::type>::type allocator;
typedef empty_value<allocator> base;
public:
typedef detail::sp_alloc_ptr<T,
typename allocator_pointer<allocator>::type> pointer;
explicit alloc_deleter(const allocator& a) noexcept
: base(empty_init_t(), a) { }
void operator()(pointer p) {
detail::sp_alloc_clear(base::get(), p.ptr(), p.size(), std::is_array<T>());
base::get().deallocate(p.ptr(), p.size());
}
};
template<class T, class A>
using alloc_noinit_deleter = alloc_deleter<T, noinit_adaptor<A> >;
namespace detail {
template<class T, class A>
class sp_alloc_make {
public:
typedef typename boost::allocator_rebind<A,
typename sp_alloc_value<T>::type>::type allocator;
private:
typedef boost::alloc_deleter<T, A> deleter;
public:
typedef std::unique_ptr<typename sp_alloc_result<T>::type, deleter> type;
sp_alloc_make(const A& a, std::size_t n)
: a_(a)
, n_(n)
, p_(a_.allocate(n)) { }
~sp_alloc_make() {
if (p_) {
a_.deallocate(p_, n_);
}
}
typename allocator::value_type* get() const noexcept {
return boost::to_address(p_);
}
allocator& state() noexcept {
return a_;
}
type release() noexcept {
pointer p = p_;
p_ = pointer();
return type(typename deleter::pointer(n_, p), deleter(a_));
}
private:
typedef typename boost::allocator_pointer<allocator>::type pointer;
allocator a_;
std::size_t n_;
pointer p_;
};
} /* detail */
template<class T, class A>
inline typename std::enable_if<!std::is_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc)
{
detail::sp_alloc_make<T, A> c(alloc, 1);
boost::alloc_construct(c.state(), c.get());
return c.release();
}
template<class T, class A, class... Args>
inline typename std::enable_if<!std::is_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc, Args&&... args)
{
detail::sp_alloc_make<T, A> c(alloc, 1);
boost::alloc_construct(c.state(), c.get(), std::forward<Args>(args)...);
return c.release();
}
template<class T, class A>
inline typename std::enable_if<!std::is_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc, typename detail::sp_type_identity<T>::type&& value)
{
detail::sp_alloc_make<T, A> c(alloc, 1);
boost::alloc_construct(c.state(), c.get(), std::move(value));
return c.release();
}
template<class T, class A>
inline typename std::enable_if<!std::is_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, noinit_adaptor<A> > > >::type
allocate_unique_noinit(const A& alloc)
{
return boost::allocate_unique<T, noinit_adaptor<A> >(alloc);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc, std::size_t size)
{
detail::sp_alloc_make<T, A> c(alloc, size);
boost::alloc_construct_n(c.state(), boost::first_scalar(c.get()),
size * detail::sp_alloc_size<T>::value);
return c.release();
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
std::unique_ptr<typename detail::sp_alloc_result<T>::type,
alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc)
{
detail::sp_alloc_make<T, A> c(alloc, std::extent<T>::value);
boost::alloc_construct_n(c.state(), boost::first_scalar(c.get()),
detail::sp_alloc_size<T>::value);
return c.release();
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, noinit_adaptor<A> > > >::type
allocate_unique_noinit(const A& alloc, std::size_t size)
{
return boost::allocate_unique<T, noinit_adaptor<A> >(alloc, size);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
std::unique_ptr<typename detail::sp_alloc_result<T>::type,
alloc_deleter<T, noinit_adaptor<A> > > >::type
allocate_unique_noinit(const A& alloc)
{
return boost::allocate_unique<T, noinit_adaptor<A> >(alloc);
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
std::unique_ptr<T, alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc, std::size_t size,
const typename std::remove_extent<T>::type& value)
{
detail::sp_alloc_make<T, A> c(alloc, size);
boost::alloc_construct_n(c.state(), boost::first_scalar(c.get()),
size * detail::sp_alloc_size<T>::value, boost::first_scalar(&value),
detail::sp_alloc_size<typename std::remove_extent<T>::type>::value);
return c.release();
}
template<class T, class A>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
std::unique_ptr<typename detail::sp_alloc_result<T>::type,
alloc_deleter<T, A> > >::type
allocate_unique(const A& alloc,
const typename std::remove_extent<T>::type& value)
{
detail::sp_alloc_make<T, A> c(alloc, std::extent<T>::value);
boost::alloc_construct_n(c.state(), boost::first_scalar(c.get()),
detail::sp_alloc_size<T>::value, boost::first_scalar(&value),
detail::sp_alloc_size<typename std::remove_extent<T>::type>::value);
return c.release();
}
template<class T, class U, class A>
inline typename allocator_pointer<typename allocator_rebind<A,
typename detail::sp_alloc_value<T>::type>::type>::type
get_allocator_pointer(const std::unique_ptr<T,
alloc_deleter<U, A> >& p) noexcept
{
return p.get().ptr();
}
} /* boost */
#endif

View File

@@ -0,0 +1,195 @@
#ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
//
// atomic_shared_ptr.hpp
//
// Copyright 2017 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#include <cstring>
namespace boost
{
template<class T> class atomic_shared_ptr
{
private:
boost::shared_ptr<T> p_;
mutable boost::detail::spinlock l_;
atomic_shared_ptr(const atomic_shared_ptr&);
atomic_shared_ptr& operator=(const atomic_shared_ptr&);
private:
bool compare_exchange( shared_ptr<T>& v, shared_ptr<T> w ) noexcept
{
l_.lock();
if( p_._internal_equiv( v ) )
{
p_.swap( w );
l_.unlock();
return true;
}
else
{
shared_ptr<T> tmp( p_ );
l_.unlock();
tmp.swap( v );
return false;
}
}
public:
constexpr atomic_shared_ptr() noexcept: l_ BOOST_DETAIL_SPINLOCK_INIT
{
}
atomic_shared_ptr( shared_ptr<T> p ) noexcept
: p_( std::move( p ) ), l_ BOOST_DETAIL_SPINLOCK_INIT
{
}
atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept
{
boost::detail::spinlock::scoped_lock lock( l_ );
p_.swap( r );
return *this;
}
constexpr bool is_lock_free() const noexcept
{
return false;
}
shared_ptr<T> load() const noexcept
{
boost::detail::spinlock::scoped_lock lock( l_ );
return p_;
}
template<class M> shared_ptr<T> load( M ) const noexcept
{
boost::detail::spinlock::scoped_lock lock( l_ );
return p_;
}
operator shared_ptr<T>() const noexcept
{
boost::detail::spinlock::scoped_lock lock( l_ );
return p_;
}
void store( shared_ptr<T> r ) noexcept
{
boost::detail::spinlock::scoped_lock lock( l_ );
p_.swap( r );
}
template<class M> void store( shared_ptr<T> r, M ) noexcept
{
boost::detail::spinlock::scoped_lock lock( l_ );
p_.swap( r );
}
shared_ptr<T> exchange( shared_ptr<T> r ) noexcept
{
{
boost::detail::spinlock::scoped_lock lock( l_ );
p_.swap( r );
}
return std::move( r );
}
template<class M> shared_ptr<T> exchange( shared_ptr<T> r, M ) noexcept
{
{
boost::detail::spinlock::scoped_lock lock( l_ );
p_.swap( r );
}
return std::move( r );
}
template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) noexcept
{
return compare_exchange( v, w );
}
template<class M> bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, M ) noexcept
{
return compare_exchange( v, w );
}
bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w ) noexcept
{
return compare_exchange( v, w );
}
template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M, M ) noexcept
{
return compare_exchange( v, w );
}
template<class M> bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, M ) noexcept
{
return compare_exchange( v, w );
}
bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w ) noexcept
{
return compare_exchange( v, w );
}
template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) noexcept
{
return compare_exchange( v, std::move( w ) );
}
template<class M> bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, M ) noexcept
{
return compare_exchange( v, std::move( w ) );
}
bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w ) noexcept
{
return compare_exchange( v, std::move( w ) );
}
template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M, M ) noexcept
{
return compare_exchange( v, std::move( w ) );
}
template<class M> bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, M ) noexcept
{
return compare_exchange( v, std::move( w ) );
}
bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w ) noexcept
{
return compare_exchange( v, std::move( w ) );
}
};
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED

View File

@@ -0,0 +1,48 @@
#ifndef BOOST_SMART_PTR_BAD_WEAK_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_BAD_WEAK_PTR_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// boost/smart_ptr/bad_weak_ptr.hpp
//
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
//
// 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)
//
#include <boost/config.hpp>
#include <exception>
namespace boost
{
#if defined(BOOST_CLANG)
// Intel C++ on Mac defines __clang__ but doesn't support the pragma
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wweak-vtables"
#endif
class bad_weak_ptr: public std::exception
{
public:
char const * what() const noexcept override
{
return "tr1::bad_weak_ptr";
}
};
#if defined(BOOST_CLANG)
# pragma clang diagnostic pop
#endif
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_BAD_WEAK_PTR_HPP_INCLUDED

View File

@@ -0,0 +1,62 @@
#ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// boost/detail/atomic_count.hpp - thread/SMP safe reference counter
//
// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
// Copyright (c) 2013 Peter Dimov
//
// 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
//
// typedef <implementation-defined> boost::detail::atomic_count;
//
// atomic_count a(n);
//
// (n is convertible to long)
//
// Effects: Constructs an atomic_count with an initial value of n
//
// a;
//
// Returns: (long) the current value of a
// Memory Ordering: acquire
//
// ++a;
//
// Effects: Atomically increments the value of a
// Returns: (long) the new value of a
// Memory Ordering: acquire/release
//
// --a;
//
// Effects: Atomically decrements the value of a
// Returns: (long) the new value of a
// Memory Ordering: acquire/release
//
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#if defined( BOOST_AC_DISABLE_THREADS )
# include <boost/smart_ptr/detail/atomic_count_nt.hpp>
#elif defined( BOOST_AC_USE_STD_ATOMIC )
# include <boost/smart_ptr/detail/atomic_count_std_atomic.hpp>
#elif defined( BOOST_SP_DISABLE_THREADS )
# include <boost/smart_ptr/detail/atomic_count_nt.hpp>
#else
# include <boost/smart_ptr/detail/atomic_count_std_atomic.hpp>
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED

View File

@@ -0,0 +1,66 @@
#ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_NT_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_NT_HPP_INCLUDED
//
// boost/detail/atomic_count_nt.hpp
//
// Trivial atomic_count for the single-threaded case
//
// http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html
//
// Copyright 2013 Peter Dimov
//
// 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
//
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using single-threaded, non-atomic atomic_count")
#endif
namespace boost
{
namespace detail
{
class atomic_count
{
public:
explicit atomic_count( long v ): value_( v )
{
}
long operator++()
{
return ++value_;
}
long operator--()
{
return --value_;
}
operator long() const
{
return value_;
}
private:
atomic_count(atomic_count const &);
atomic_count & operator=(atomic_count const &);
long value_;
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_NT_HPP_INCLUDED

View File

@@ -0,0 +1,67 @@
#ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED
//
// boost/detail/atomic_count_std_atomic.hpp
//
// atomic_count for std::atomic
//
// Copyright 2013 Peter Dimov
//
// 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
//
#include <atomic>
#include <cstdint>
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using std::atomic atomic_count")
#endif
namespace boost
{
namespace detail
{
class atomic_count
{
public:
explicit atomic_count( long v ): value_( static_cast< std::int_least32_t >( v ) )
{
}
long operator++()
{
return value_.fetch_add( 1, std::memory_order_acq_rel ) + 1;
}
long operator--()
{
return value_.fetch_sub( 1, std::memory_order_acq_rel ) - 1;
}
operator long() const
{
return value_.load( std::memory_order_acquire );
}
private:
atomic_count(atomic_count const &);
atomic_count & operator=(atomic_count const &);
std::atomic_int_least32_t value_;
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED

View File

@@ -0,0 +1,52 @@
#ifndef BOOST_SMART_PTR_DETAIL_DEPRECATED_MACROS_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_DEPRECATED_MACROS_HPP_INCLUDED
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config/pragma_message.hpp>
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
BOOST_PRAGMA_MESSAGE("The macro BOOST_SP_ENABLE_DEBUG_HOOKS has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#if defined(BOOST_SP_USE_STD_ALLOCATOR)
BOOST_PRAGMA_MESSAGE("The macro BOOST_SP_USE_STD_ALLOCATOR has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
BOOST_PRAGMA_MESSAGE("The macro BOOST_SP_USE_QUICK_ALLOCATOR has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#if defined(BOOST_AC_USE_SPINLOCK)
BOOST_PRAGMA_MESSAGE("The macro BOOST_AC_USE_SPINLOCK has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#if defined(BOOST_AC_USE_PTHREADS)
BOOST_PRAGMA_MESSAGE("The macro BOOST_AC_USE_PTHREADS has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#if defined(BOOST_SP_USE_SPINLOCK)
BOOST_PRAGMA_MESSAGE("The macro BOOST_SP_USE_SPINLOCK has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#if defined(BOOST_SP_USE_PTHREADS)
BOOST_PRAGMA_MESSAGE("The macro BOOST_SP_USE_PTHREADS has been deprecated in 1.87 and support for it was removed in 1.90.")
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_DEPRECATED_MACROS_HPP_INCLUDED

View File

@@ -0,0 +1,31 @@
#ifndef BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// boost/detail/lightweight_mutex.hpp - lightweight mutex
//
// Copyright (c) 2002, 2003 Peter Dimov and Multi Media Ltd.
//
// 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)
//
// typedef <unspecified> boost::detail::lightweight_mutex;
//
// boost::detail::lightweight_mutex is a header-only implementation of
// a subset of the Mutex concept requirements:
//
// http://www.boost.org/doc/html/threads/concepts.html#threads.concepts.Mutex
//
// It's obsoleted by std::mutex.
//
#include <boost/smart_ptr/detail/lwm_std_mutex.hpp>
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED

View File

@@ -0,0 +1,56 @@
#ifndef BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// boost/detail/lightweight_thread.hpp
//
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
// Copyright (c) 2008, 2018 Peter Dimov
//
// 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
//
//
// typedef /*...*/ lw_thread_t; // as pthread_t
// template<class F> int lw_thread_create( lw_thread_t & th, F f );
// void lw_thread_join( lw_thread_t th );
#include <thread>
#include <exception>
namespace boost
{
namespace detail
{
using lw_thread_t = std::thread*;
template<class F> int lw_thread_create( lw_thread_t& th, F f )
{
try
{
th = new std::thread( f );
return 0;
}
catch( std::exception const& )
{
return -1;
}
}
void lw_thread_join( lw_thread_t th )
{
th->join();
delete th;
}
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LIGHTWEIGHT_THREAD_HPP_INCLUDED

View File

@@ -0,0 +1,144 @@
#ifndef BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// detail/local_counted_base.hpp
//
// Copyright 2017 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/detail/shared_count.hpp>
#include <boost/config.hpp>
#include <utility>
namespace boost
{
namespace detail
{
class BOOST_SYMBOL_VISIBLE local_counted_base
{
private:
local_counted_base & operator= ( local_counted_base const & );
private:
// not 'int' or 'unsigned' to avoid aliasing and enable optimizations
enum count_type { min_ = 0, initial_ = 1, max_ = 2147483647 };
count_type local_use_count_;
public:
constexpr local_counted_base() noexcept: local_use_count_( initial_ )
{
}
constexpr local_counted_base( local_counted_base const & ) noexcept: local_use_count_( initial_ )
{
}
virtual ~local_counted_base() /*noexcept*/
{
}
virtual void local_cb_destroy() noexcept = 0;
virtual boost::detail::shared_count local_cb_get_shared_count() const noexcept = 0;
void add_ref() noexcept
{
#if !defined(__NVCC__)
#if defined( __has_builtin )
# if __has_builtin( __builtin_assume )
__builtin_assume( local_use_count_ >= 1 );
# endif
#endif
#endif
local_use_count_ = static_cast<count_type>( local_use_count_ + 1 );
}
void release() noexcept
{
local_use_count_ = static_cast<count_type>( local_use_count_ - 1 );
if( local_use_count_ == 0 )
{
local_cb_destroy();
}
}
long local_use_count() const noexcept
{
return local_use_count_;
}
};
class BOOST_SYMBOL_VISIBLE local_counted_impl: public local_counted_base
{
private:
local_counted_impl( local_counted_impl const & );
private:
shared_count pn_;
public:
explicit local_counted_impl( shared_count const& pn ) noexcept: pn_( pn )
{
}
explicit local_counted_impl( shared_count && pn ) noexcept: pn_( std::move(pn) )
{
}
void local_cb_destroy() noexcept override
{
delete this;
}
boost::detail::shared_count local_cb_get_shared_count() const noexcept override
{
return pn_;
}
};
class BOOST_SYMBOL_VISIBLE local_counted_impl_em: public local_counted_base
{
public:
shared_count pn_;
void local_cb_destroy() noexcept override
{
shared_count().swap( pn_ );
}
boost::detail::shared_count local_cb_get_shared_count() const noexcept override
{
return pn_;
}
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED

View File

@@ -0,0 +1,83 @@
#ifndef BOOST_SMART_PTR_DETAIL_LOCAL_SP_DELETER_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_LOCAL_SP_DELETER_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// detail/local_sp_deleter.hpp
//
// Copyright 2017 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/detail/local_counted_base.hpp>
#include <boost/config.hpp>
namespace boost
{
namespace detail
{
template<class D> class local_sp_deleter: public local_counted_impl_em
{
private:
D d_;
public:
local_sp_deleter(): d_()
{
}
explicit local_sp_deleter( D const& d ) noexcept: d_( d )
{
}
explicit local_sp_deleter( D&& d ) noexcept: d_( std::move(d) )
{
}
D& deleter() noexcept
{
return d_;
}
template<class Y> void operator()( Y* p ) noexcept
{
d_( p );
}
void operator()( std::nullptr_t p ) noexcept
{
d_( p );
}
};
template<> class local_sp_deleter<void>
{
};
template<class D> D * get_local_deleter( local_sp_deleter<D> * p ) noexcept
{
return &p->deleter();
}
inline void * get_local_deleter( local_sp_deleter<void> * /*p*/ ) noexcept
{
return 0;
}
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LOCAL_SP_DELETER_HPP_INCLUDED

View File

@@ -0,0 +1,62 @@
#ifndef BOOST_SMART_PTR_DETAIL_LWM_STD_MUTEX_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_LWM_STD_MUTEX_HPP_INCLUDED
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt)
#include <boost/assert.hpp>
#include <mutex>
namespace boost
{
namespace detail
{
class lightweight_mutex
{
private:
std::mutex m_;
lightweight_mutex(lightweight_mutex const &);
lightweight_mutex & operator=(lightweight_mutex const &);
public:
lightweight_mutex()
{
}
class scoped_lock;
friend class scoped_lock;
class scoped_lock
{
private:
std::mutex & m_;
scoped_lock(scoped_lock const &);
scoped_lock & operator=(scoped_lock const &);
public:
scoped_lock( lightweight_mutex & m ): m_( m.m_ )
{
m_.lock();
}
~scoped_lock()
{
m_.unlock();
}
};
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_STD_MUTEX_HPP_INCLUDED

View File

@@ -0,0 +1,63 @@
#ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
// Copyright 2003 David Abrahams
// Copyright 2003, 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config/header_deprecated.hpp>
#include <memory>
#include <cstddef>
BOOST_HEADER_DEPRECATED("std::allocator or std::pmr::synchronized_pool_resource")
namespace boost
{
namespace detail
{
template<class T> struct quick_allocator
{
static void* alloc()
{
return std::allocator<T>().allocate( 1 );
}
static void* alloc( std::size_t n )
{
if( n != sizeof(T) ) // class-specific delete called for a derived object
{
return ::operator new( n );
}
else
{
return alloc();
}
}
static void dealloc( void* p )
{
if( p != 0 ) // 18.4.1.1/13
{
std::allocator<T>().deallocate( static_cast<T*>( p ), 1 );
}
}
static void dealloc( void* p, std::size_t n )
{
if( n != sizeof(T) ) // class-specific delete called for a derived object
{
::operator delete( p );
}
else
{
dealloc( p );
}
}
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED

View File

@@ -0,0 +1,581 @@
#ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// detail/shared_count.hpp
//
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
// Copyright 2004-2005 Peter Dimov
//
// 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)
//
#include <boost/smart_ptr/bad_weak_ptr.hpp>
#include <boost/smart_ptr/detail/sp_counted_base.hpp>
#include <boost/smart_ptr/detail/sp_counted_impl.hpp>
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#include <boost/core/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/core/addressof.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <cstdint>
#include <memory> // std::auto_ptr
#include <functional> // std::less
#include <cstddef> // std::size_t
#ifdef BOOST_NO_EXCEPTIONS
# include <new> // std::bad_alloc
#endif
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
namespace boost
{
namespace movelib
{
template< class T, class D > class unique_ptr;
} // namespace movelib
namespace detail
{
struct sp_nothrow_tag {};
template< class D > struct sp_inplace_tag
{
};
template< class T > class sp_reference_wrapper
{
public:
explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
{
}
template< class Y > void operator()( Y * p ) const
{
(*t_)( p );
}
private:
T * t_;
};
template< class D > struct sp_convert_reference
{
typedef D type;
};
template< class D > struct sp_convert_reference< D& >
{
typedef sp_reference_wrapper< D > type;
};
template<class T> std::size_t sp_hash_pointer( T* p ) noexcept
{
std::uintptr_t v = reinterpret_cast<std::uintptr_t>( p );
// match boost::hash<T*>
return static_cast<std::size_t>( v + ( v >> 3 ) );
}
class weak_count;
class shared_count
{
private:
sp_counted_base * pi_;
friend class weak_count;
public:
constexpr shared_count() noexcept: pi_(0)
{
}
constexpr explicit shared_count( sp_counted_base * pi ) noexcept: pi_( pi )
{
}
template<class Y> explicit shared_count( Y * p ): pi_( 0 )
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
pi_ = new sp_counted_impl_p<Y>( p );
}
catch(...)
{
boost::checked_delete( p );
throw;
}
#else
pi_ = new sp_counted_impl_p<Y>( p );
if( pi_ == 0 )
{
boost::checked_delete( p );
boost::throw_exception( std::bad_alloc() );
}
#endif
}
template<class P, class D> shared_count( P p, D d ): pi_(0)
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
pi_ = new sp_counted_impl_pd<P, D>(p, d);
}
catch(...)
{
d(p); // delete p
throw;
}
#else
pi_ = new sp_counted_impl_pd<P, D>(p, d);
if(pi_ == 0)
{
d(p); // delete p
boost::throw_exception(std::bad_alloc());
}
#endif
}
template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
pi_ = new sp_counted_impl_pd< P, D >( p );
}
catch( ... )
{
D::operator_fn( p ); // delete p
throw;
}
#else
pi_ = new sp_counted_impl_pd< P, D >( p );
if( pi_ == 0 )
{
D::operator_fn( p ); // delete p
boost::throw_exception( std::bad_alloc() );
}
#endif // #ifndef BOOST_NO_EXCEPTIONS
}
template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
{
typedef sp_counted_impl_pda<P, D, A> impl_type;
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
A2 a2( a );
#ifndef BOOST_NO_EXCEPTIONS
try
{
pi_ = a2.allocate( 1 );
::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
}
catch(...)
{
d( p );
if( pi_ != 0 )
{
a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
}
throw;
}
#else
pi_ = a2.allocate( 1 );
if( pi_ != 0 )
{
::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
}
else
{
d( p );
boost::throw_exception( std::bad_alloc() );
}
#endif
}
template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
{
typedef sp_counted_impl_pda< P, D, A > impl_type;
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
A2 a2( a );
#ifndef BOOST_NO_EXCEPTIONS
try
{
pi_ = a2.allocate( 1 );
::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
}
catch(...)
{
D::operator_fn( p );
if( pi_ != 0 )
{
a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
}
throw;
}
#else
pi_ = a2.allocate( 1 );
if( pi_ != 0 )
{
::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
}
else
{
D::operator_fn( p );
boost::throw_exception( std::bad_alloc() );
}
#endif // #ifndef BOOST_NO_EXCEPTIONS
}
#ifndef BOOST_NO_AUTO_PTR
// auto_ptr<Y> is special cased to provide the strong guarantee
template<class Y>
explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
{
#ifdef BOOST_NO_EXCEPTIONS
if( pi_ == 0 )
{
boost::throw_exception(std::bad_alloc());
}
#endif
r.release();
}
#endif
template<class Y, class D>
explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
{
typedef typename sp_convert_reference<D>::type D2;
D2 d2( static_cast<D&&>( r.get_deleter() ) );
pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
#ifdef BOOST_NO_EXCEPTIONS
if( pi_ == 0 )
{
boost::throw_exception( std::bad_alloc() );
}
#endif
r.release();
}
template<class Y, class D>
explicit shared_count( boost::movelib::unique_ptr<Y, D> & r ): pi_( 0 )
{
typedef typename sp_convert_reference<D>::type D2;
D2 d2( r.get_deleter() );
pi_ = new sp_counted_impl_pd< typename boost::movelib::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
#ifdef BOOST_NO_EXCEPTIONS
if( pi_ == 0 )
{
boost::throw_exception( std::bad_alloc() );
}
#endif
r.release();
}
~shared_count() /*noexcept*/
{
if( pi_ != 0 ) pi_->release();
}
shared_count(shared_count const & r) noexcept: pi_(r.pi_)
{
if( pi_ != 0 ) pi_->add_ref_copy();
}
shared_count(shared_count && r) noexcept: pi_(r.pi_)
{
r.pi_ = 0;
}
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
shared_count( weak_count const & r, sp_nothrow_tag ) noexcept; // constructs an empty *this when r.use_count() == 0
shared_count & operator= (shared_count const & r) noexcept
{
sp_counted_base * tmp = r.pi_;
if( tmp != pi_ )
{
if( tmp != 0 ) tmp->add_ref_copy();
if( pi_ != 0 ) pi_->release();
pi_ = tmp;
}
return *this;
}
void swap(shared_count & r) noexcept
{
sp_counted_base * tmp = r.pi_;
r.pi_ = pi_;
pi_ = tmp;
}
long use_count() const noexcept
{
return pi_ != 0? pi_->use_count(): 0;
}
bool unique() const noexcept
{
return use_count() == 1;
}
bool empty() const noexcept
{
return pi_ == 0;
}
bool operator==( shared_count const & r ) const noexcept
{
return pi_ == r.pi_;
}
bool operator==( weak_count const & r ) const noexcept;
bool operator<( shared_count const & r ) const noexcept
{
return std::less<sp_counted_base *>()( pi_, r.pi_ );
}
bool operator<( weak_count const & r ) const noexcept;
void * get_deleter( sp_typeinfo_ const & ti ) const noexcept
{
return pi_? pi_->get_deleter( ti ): 0;
}
void * get_local_deleter( sp_typeinfo_ const & ti ) const noexcept
{
return pi_? pi_->get_local_deleter( ti ): 0;
}
void * get_untyped_deleter() const noexcept
{
return pi_? pi_->get_untyped_deleter(): 0;
}
std::size_t hash_value() const noexcept
{
return sp_hash_pointer( pi_ );
}
};
class weak_count
{
private:
sp_counted_base * pi_;
friend class shared_count;
public:
constexpr weak_count() noexcept: pi_(0)
{
}
weak_count(shared_count const & r) noexcept: pi_(r.pi_)
{
if(pi_ != 0) pi_->weak_add_ref();
}
weak_count(weak_count const & r) noexcept: pi_(r.pi_)
{
if(pi_ != 0) pi_->weak_add_ref();
}
// Move support
weak_count(weak_count && r) noexcept: pi_(r.pi_)
{
r.pi_ = 0;
}
~weak_count() /*noexcept*/
{
if(pi_ != 0) pi_->weak_release();
}
weak_count & operator= (shared_count const & r) noexcept
{
sp_counted_base * tmp = r.pi_;
if( tmp != pi_ )
{
if(tmp != 0) tmp->weak_add_ref();
if(pi_ != 0) pi_->weak_release();
pi_ = tmp;
}
return *this;
}
weak_count & operator= (weak_count const & r) noexcept
{
sp_counted_base * tmp = r.pi_;
if( tmp != pi_ )
{
if(tmp != 0) tmp->weak_add_ref();
if(pi_ != 0) pi_->weak_release();
pi_ = tmp;
}
return *this;
}
void swap(weak_count & r) noexcept
{
sp_counted_base * tmp = r.pi_;
r.pi_ = pi_;
pi_ = tmp;
}
long use_count() const noexcept
{
return pi_ != 0? pi_->use_count(): 0;
}
bool empty() const noexcept
{
return pi_ == 0;
}
bool operator==( weak_count const & r ) const noexcept
{
return pi_ == r.pi_;
}
bool operator==( shared_count const & r ) const noexcept
{
return pi_ == r.pi_;
}
bool operator<( weak_count const & r ) const noexcept
{
return std::less<sp_counted_base *>()( pi_, r.pi_ );
}
bool operator<( shared_count const & r ) const noexcept
{
return std::less<sp_counted_base *>()( pi_, r.pi_ );
}
std::size_t hash_value() const noexcept
{
return sp_hash_pointer( pi_ );
}
};
inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
{
if( pi_ == 0 || !pi_->add_ref_lock() )
{
boost::throw_exception( boost::bad_weak_ptr() );
}
}
inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ) noexcept: pi_( r.pi_ )
{
if( pi_ != 0 && !pi_->add_ref_lock() )
{
pi_ = 0;
}
}
inline bool shared_count::operator==( weak_count const & r ) const noexcept
{
return pi_ == r.pi_;
}
inline bool shared_count::operator<( weak_count const & r ) const noexcept
{
return std::less<sp_counted_base *>()( pi_, r.pi_ );
}
} // namespace detail
} // namespace boost
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic pop
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED

View File

@@ -0,0 +1,76 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_CONVERTIBLE_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_CONVERTIBLE_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// detail/sp_convertible.hpp
//
// Copyright 2008 Peter Dimov
//
// 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
#include <boost/config.hpp>
#include <cstddef>
namespace boost
{
namespace detail
{
template< class Y, class T > struct sp_convertible
{
typedef char (&yes) [1];
typedef char (&no) [2];
static yes f( T* );
static no f( ... );
enum _vt { value = sizeof( (f)( static_cast<Y*>(0) ) ) == sizeof(yes) };
};
template< class Y, class T > struct sp_convertible< Y, T[] >
{
enum _vt { value = false };
};
template< class Y, class T > struct sp_convertible< Y[], T[] >
{
enum _vt { value = sp_convertible< Y[1], T[1] >::value };
};
template< class Y, std::size_t N, class T > struct sp_convertible< Y[N], T[] >
{
enum _vt { value = sp_convertible< Y[1], T[1] >::value };
};
struct sp_empty
{
};
template< bool > struct sp_enable_if_convertible_impl;
template<> struct sp_enable_if_convertible_impl<true>
{
typedef sp_empty type;
};
template<> struct sp_enable_if_convertible_impl<false>
{
};
template< class Y, class T > struct sp_enable_if_convertible: public sp_enable_if_convertible_impl< sp_convertible< Y, T >::value >
{
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_CONVERTIBLE_HPP_INCLUDED

View File

@@ -0,0 +1,30 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// detail/sp_counted_base.hpp
//
// Copyright 2005-2013 Peter Dimov
//
// 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)
//
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#if defined( BOOST_SP_DISABLE_THREADS )
# include <boost/smart_ptr/detail/sp_counted_base_nt.hpp>
#else
# include <boost/smart_ptr/detail/sp_counted_base_std_atomic.hpp>
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_HPP_INCLUDED

View File

@@ -0,0 +1,118 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// detail/sp_counted_base_nt.hpp
//
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
// Copyright 2004-2005 Peter Dimov
//
// 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)
//
#include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
#include <boost/config.hpp>
#include <cstdint>
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using single-threaded, non-atomic sp_counted_base")
#endif
namespace boost
{
namespace detail
{
class BOOST_SYMBOL_VISIBLE sp_counted_base
{
private:
sp_counted_base( sp_counted_base const & );
sp_counted_base & operator= ( sp_counted_base const & );
std::int_least32_t use_count_; // #shared
std::int_least32_t weak_count_; // #weak + (#shared != 0)
public:
sp_counted_base() noexcept: use_count_( 1 ), weak_count_( 1 )
{
}
virtual ~sp_counted_base() /*noexcept*/
{
}
// dispose() is called when use_count_ drops to zero, to release
// the resources managed by *this.
virtual void dispose() noexcept = 0; // nothrow
// destroy() is called when weak_count_ drops to zero.
virtual void destroy() noexcept // nothrow
{
delete this;
}
virtual void * get_deleter( sp_typeinfo_ const & ti ) noexcept = 0;
virtual void * get_local_deleter( sp_typeinfo_ const & ti ) noexcept = 0;
virtual void * get_untyped_deleter() noexcept = 0;
void add_ref_copy() noexcept
{
++use_count_;
}
bool add_ref_lock() noexcept // true on success
{
if( use_count_ == 0 ) return false;
++use_count_;
return true;
}
void release() noexcept
{
if( --use_count_ == 0 )
{
dispose();
weak_release();
}
}
void weak_add_ref() noexcept
{
++weak_count_;
}
void weak_release() noexcept
{
if( --weak_count_ == 0 )
{
destroy();
}
}
long use_count() const noexcept
{
return use_count_;
}
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED

View File

@@ -0,0 +1,146 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// detail/sp_counted_base_std_atomic.hpp - C++11 std::atomic
//
// Copyright (c) 2007, 2013 Peter Dimov
//
// 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
#include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
#include <boost/config.hpp>
#include <atomic>
#include <cstdint>
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using std::atomic sp_counted_base")
#endif
namespace boost
{
namespace detail
{
inline void atomic_increment( std::atomic_int_least32_t * pw ) noexcept
{
pw->fetch_add( 1, std::memory_order_relaxed );
}
inline std::int_least32_t atomic_decrement( std::atomic_int_least32_t * pw ) noexcept
{
return pw->fetch_sub( 1, std::memory_order_acq_rel );
}
inline std::int_least32_t atomic_conditional_increment( std::atomic_int_least32_t * pw ) noexcept
{
// long r = *pw;
// if( r != 0 ) ++*pw;
// return r;
std::int_least32_t r = pw->load( std::memory_order_relaxed );
for( ;; )
{
if( r == 0 )
{
return r;
}
if( pw->compare_exchange_weak( r, r + 1, std::memory_order_relaxed, std::memory_order_relaxed ) )
{
return r;
}
}
}
class BOOST_SYMBOL_VISIBLE sp_counted_base
{
private:
sp_counted_base( sp_counted_base const & );
sp_counted_base & operator= ( sp_counted_base const & );
std::atomic_int_least32_t use_count_; // #shared
std::atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
public:
sp_counted_base() noexcept: use_count_( 1 ), weak_count_( 1 )
{
}
virtual ~sp_counted_base() /*noexcept*/
{
}
// dispose() is called when use_count_ drops to zero, to release
// the resources managed by *this.
virtual void dispose() noexcept = 0;
// destroy() is called when weak_count_ drops to zero.
virtual void destroy() noexcept
{
delete this;
}
virtual void * get_deleter( sp_typeinfo_ const & ti ) noexcept = 0;
virtual void * get_local_deleter( sp_typeinfo_ const & ti ) noexcept = 0;
virtual void * get_untyped_deleter() noexcept = 0;
void add_ref_copy() noexcept
{
atomic_increment( &use_count_ );
}
bool add_ref_lock() noexcept // true on success
{
return atomic_conditional_increment( &use_count_ ) != 0;
}
void release() noexcept
{
if( atomic_decrement( &use_count_ ) == 1 )
{
dispose();
weak_release();
}
}
void weak_add_ref() noexcept
{
atomic_increment( &weak_count_ );
}
void weak_release() noexcept
{
if( atomic_decrement( &weak_count_ ) == 1 )
{
destroy();
}
}
long use_count() const noexcept
{
return use_count_.load( std::memory_order_acquire );
}
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED

View File

@@ -0,0 +1,193 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// detail/sp_counted_impl.hpp
//
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
// Copyright 2004-2005 Peter Dimov
//
// 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)
//
#include <boost/smart_ptr/detail/sp_counted_base.hpp>
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#include <boost/core/checked_delete.hpp>
#include <boost/core/addressof.hpp>
#include <boost/config.hpp>
#include <memory> // std::allocator, std::allocator_traits
#include <cstddef> // std::size_t
namespace boost
{
namespace detail
{
// get_local_deleter
template<class D> class local_sp_deleter;
template<class D> D * get_local_deleter( D * /*p*/ ) noexcept
{
return 0;
}
template<class D> D * get_local_deleter( local_sp_deleter<D> * p ) noexcept;
//
template<class X> class BOOST_SYMBOL_VISIBLE sp_counted_impl_p: public sp_counted_base
{
private:
X * px_;
sp_counted_impl_p( sp_counted_impl_p const & );
sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
typedef sp_counted_impl_p<X> this_type;
public:
explicit sp_counted_impl_p( X * px ): px_( px )
{
}
void dispose() noexcept override
{
boost::checked_delete( px_ );
}
void * get_deleter( sp_typeinfo_ const & ) noexcept override
{
return 0;
}
void * get_local_deleter( sp_typeinfo_ const & ) noexcept override
{
return 0;
}
void * get_untyped_deleter() noexcept override
{
return 0;
}
};
template<class P, class D> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pd: public sp_counted_base
{
private:
P ptr; // copy constructor must not throw
D del; // copy/move constructor must not throw
sp_counted_impl_pd( sp_counted_impl_pd const & );
sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
typedef sp_counted_impl_pd<P, D> this_type;
public:
// pre: d(p) must not throw
sp_counted_impl_pd( P p, D & d ): ptr( p ), del( static_cast< D&& >( d ) )
{
}
sp_counted_impl_pd( P p ): ptr( p ), del()
{
}
void dispose() noexcept override
{
del( ptr );
}
void * get_deleter( sp_typeinfo_ const & ti ) noexcept override
{
return ti == BOOST_SP_TYPEID_(D)? &reinterpret_cast<char&>( del ): 0;
}
void * get_local_deleter( sp_typeinfo_ const & ti ) noexcept override
{
return ti == BOOST_SP_TYPEID_(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0;
}
void * get_untyped_deleter() noexcept override
{
return &reinterpret_cast<char&>( del );
}
};
template<class P, class D, class A> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pda: public sp_counted_base
{
private:
P p_; // copy constructor must not throw
D d_; // copy/move constructor must not throw
A a_; // copy constructor must not throw
sp_counted_impl_pda( sp_counted_impl_pda const & );
sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
typedef sp_counted_impl_pda<P, D, A> this_type;
public:
// pre: d( p ) must not throw
sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( static_cast< D&& >( d ) ), a_( a )
{
}
sp_counted_impl_pda( P p, A a ): p_( p ), d_( a ), a_( a )
{
}
void dispose() noexcept override
{
d_( p_ );
}
void destroy() noexcept override
{
typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
A2 a2( a_ );
this->~this_type();
a2.deallocate( this, 1 );
}
void * get_deleter( sp_typeinfo_ const & ti ) noexcept override
{
return ti == BOOST_SP_TYPEID_( D )? &reinterpret_cast<char&>( d_ ): 0;
}
void * get_local_deleter( sp_typeinfo_ const & ti ) noexcept override
{
return ti == BOOST_SP_TYPEID_( D )? boost::detail::get_local_deleter( boost::addressof( d_ ) ): 0;
}
void * get_untyped_deleter() noexcept override
{
return &reinterpret_cast<char&>( d_ );
}
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED

View File

@@ -0,0 +1,37 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_CXX20_CONSTEXPR_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_CXX20_CONSTEXPR_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif
// detail/sp_noexcept.hpp
//
// Copyright 2025 Mathias Stearn
//
// 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
// This macro is used to mark functions as constexpr if the compiler supports
// constexpr destructors. Since you can't have a constexpr smart pointer object,
// everything except null constructors are guided behind this macro. Because
// this also guards a use of dynamic_cast, we need to check for its availability
// as well. It isn't worth splitting out since all known compilers that support
// constexpr dynamic_cast also support constexpr destructors.
//
// WARNING: This does not check for changing active member of a union in
// constant expressions which is allowed in C++20. If that is needed, we
// need to raise the checked version to 202002L.
#if defined(__cpp_constexpr_dynamic_alloc) && __cpp_constexpr_dynamic_alloc >= 201907L \
&& defined(__cpp_constexpr) && __cpp_constexpr >= 201907L
#define BOOST_SP_CXX20_CONSTEXPR constexpr
#else
#define BOOST_SP_CXX20_CONSTEXPR
#define BOOST_SP_NO_CXX20_CONSTEXPR
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_CXX20_CONSTEXPR_HPP_INCLUDED

View File

@@ -0,0 +1,40 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_DISABLE_DEPRECATED_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_DISABLE_DEPRECATED_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// boost/smart_ptr/detail/sp_disable_deprecated.hpp
//
// Copyright 2015 Peter Dimov
//
// 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)
//
#include <boost/config.hpp>
#if defined( __GNUC__ ) && ( defined( __GXX_EXPERIMENTAL_CXX0X__ ) || ( __cplusplus >= 201103L ) )
# if defined( BOOST_GCC )
# if BOOST_GCC >= 40600
# define BOOST_SP_DISABLE_DEPRECATED
# endif
# elif defined( __clang__ ) && defined( __has_warning )
# if __has_warning( "-Wdeprecated-declarations" )
# define BOOST_SP_DISABLE_DEPRECATED
# endif
# endif
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_DISABLE_DEPRECATED_HPP_INCLUDED

View File

@@ -0,0 +1,39 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_NOEXCEPT_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_NOEXCEPT_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// detail/sp_noexcept.hpp
//
// Copyright 2016, 2017 Peter Dimov
//
// 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
// BOOST_SP_NOEXCEPT (obsolete, only retained for compatibility)
#define BOOST_SP_NOEXCEPT noexcept
// BOOST_SP_NOEXCEPT_WITH_ASSERT (noexcept, unless a user assertion handler is present)
#if defined(BOOST_DISABLE_ASSERTS) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && defined(NDEBUG) )
# define BOOST_SP_NOEXCEPT_WITH_ASSERT noexcept
#elif defined(BOOST_ENABLE_ASSERT_HANDLER) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && !defined(NDEBUG) )
# define BOOST_SP_NOEXCEPT_WITH_ASSERT
#else
# define BOOST_SP_NOEXCEPT_WITH_ASSERT noexcept
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_NOEXCEPT_HPP_INCLUDED

View File

@@ -0,0 +1,23 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_THREAD_PAUSE_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_THREAD_PAUSE_HPP_INCLUDED
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/yield_primitives.hpp>
#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( "<boost/core/yield_primitives.hpp>" )
namespace boost
{
namespace detail
{
using boost::core::sp_thread_pause;
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_THREAD_PAUSE_HPP_INCLUDED

View File

@@ -0,0 +1,23 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/yield_primitives.hpp>
#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( "<boost/core/yield_primitives.hpp>" )
namespace boost
{
namespace detail
{
using boost::core::sp_thread_sleep;
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED

View File

@@ -0,0 +1,23 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_THREAD_YIELD_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_THREAD_YIELD_HPP_INCLUDED
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/yield_primitives.hpp>
#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( "<boost/core/yield_primitives.hpp>" )
namespace boost
{
namespace detail
{
using boost::core::sp_thread_yield;
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_THREAD_YIELD_HPP_INCLUDED

View File

@@ -0,0 +1,55 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_TYPE_TRAITS_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_TYPE_TRAITS_HPP_INCLUDED
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <type_traits>
namespace boost
{
namespace detail
{
// std::is_bounded_array (C++20)
template<class T> struct sp_is_bounded_array: std::false_type
{
};
template<class T, std::size_t N> struct sp_is_bounded_array< T[N] >: std::true_type
{
};
// std::is_unbounded_array (C++20)
template<class T> struct sp_is_unbounded_array: std::false_type
{
};
template<class T> struct sp_is_unbounded_array< T[] >: std::true_type
{
};
// std::type_identity (C++20)
template<class T> struct sp_type_identity
{
typedef T type;
};
// boost::type_with_alignment
template<std::size_t A> struct sp_type_with_alignment
{
struct alignas(A) type
{
unsigned char padding[ A ];
};
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_TYPE_TRAITS_HPP_INCLUDED

View File

@@ -0,0 +1,58 @@
#ifndef BOOST_SMART_PTR_DETAIL_SP_TYPEINFO_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SP_TYPEINFO_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// smart_ptr/detail/sp_typeinfo_.hpp
//
// Copyright 2007, 2019 Peter Dimov
//
// 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)
#include <boost/config.hpp>
#if defined( BOOST_NO_TYPEID ) || defined( BOOST_NO_STD_TYPEINFO )
#include <boost/core/typeinfo.hpp>
namespace boost
{
namespace detail
{
typedef boost::core::typeinfo sp_typeinfo_;
} // namespace detail
} // namespace boost
#define BOOST_SP_TYPEID_(T) BOOST_CORE_TYPEID(T)
#else // defined( BOOST_NO_TYPEID ) || defined( BOOST_NO_STD_TYPEINFO )
#include <typeinfo>
namespace boost
{
namespace detail
{
typedef std::type_info sp_typeinfo_;
} // namespace detail
} // namespace boost
#define BOOST_SP_TYPEID_(T) typeid(T)
#endif // defined( BOOST_NO_TYPEID ) || defined( BOOST_NO_STD_TYPEINFO )
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_TYPEINFO_HPP_INCLUDED

View File

@@ -0,0 +1,44 @@
#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SPINLOCK_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// boost/detail/spinlock.hpp
//
// Copyright (c) 2008 Peter Dimov
//
// 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)
//
// struct spinlock
// {
// void lock();
// bool try_lock();
// void unlock();
//
// class scoped_lock;
// };
//
// #define BOOST_DETAIL_SPINLOCK_INIT <unspecified>
//
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#if defined(__clang__)
// Old Clang versions have trouble with ATOMIC_FLAG_INIT
# include <boost/smart_ptr/detail/spinlock_gcc_atomic.hpp>
#else
# include <boost/smart_ptr/detail/spinlock_std_atomic.hpp>
#endif
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_HPP_INCLUDED

View File

@@ -0,0 +1,94 @@
#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright 2008, 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/smart_ptr/detail/yield_k.hpp>
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using __atomic spinlock")
#endif
namespace boost
{
namespace detail
{
class spinlock
{
public:
// `bool` alignment is required for Apple PPC32
// https://github.com/boostorg/smart_ptr/issues/105
// https://github.com/PurpleI2P/i2pd/issues/1726
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107590
union
{
unsigned char v_;
bool align_;
};
public:
bool try_lock()
{
return __atomic_test_and_set( &v_, __ATOMIC_ACQUIRE ) == 0;
}
void lock()
{
for( unsigned k = 0; !try_lock(); ++k )
{
boost::detail::yield( k );
}
}
void unlock()
{
__atomic_clear( &v_, __ATOMIC_RELEASE );
}
public:
class scoped_lock
{
private:
spinlock & sp_;
scoped_lock( scoped_lock const & );
scoped_lock & operator=( scoped_lock const & );
public:
explicit scoped_lock( spinlock & sp ): sp_( sp )
{
sp.lock();
}
~scoped_lock()
{
sp_.unlock();
}
};
};
} // namespace detail
} // namespace boost
#define BOOST_DETAIL_SPINLOCK_INIT {{0}}
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED

View File

@@ -0,0 +1,91 @@
#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// boost/detail/spinlock_pool.hpp
//
// Copyright (c) 2008 Peter Dimov
//
// 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)
//
// spinlock_pool<0> is reserved for atomic<>, when/if it arrives
// spinlock_pool<1> is reserved for shared_ptr reference counts
// spinlock_pool<2> is reserved for shared_ptr atomic access
//
#include <boost/config.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#include <cstddef>
namespace boost
{
namespace detail
{
template< int M > class spinlock_pool
{
private:
static spinlock pool_[ 41 ];
public:
static spinlock & spinlock_for( void const * pv )
{
#if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
std::size_t i = reinterpret_cast< unsigned long long >( pv ) % 41;
#else
std::size_t i = reinterpret_cast< std::size_t >( pv ) % 41;
#endif
return pool_[ i ];
}
class scoped_lock
{
private:
spinlock & sp_;
scoped_lock( scoped_lock const & );
scoped_lock & operator=( scoped_lock const & );
public:
explicit scoped_lock( void const * pv ): sp_( spinlock_for( pv ) )
{
sp_.lock();
}
~scoped_lock()
{
sp_.unlock();
}
};
};
template< int M > spinlock spinlock_pool< M >::pool_[ 41 ] =
{
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
BOOST_DETAIL_SPINLOCK_INIT
};
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED

View File

@@ -0,0 +1,90 @@
#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// Copyright (c) 2014 Peter Dimov
//
// 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)
//
#include <boost/smart_ptr/detail/yield_k.hpp>
#include <atomic>
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using std::atomic spinlock")
#endif
namespace boost
{
namespace detail
{
class spinlock
{
public:
std::atomic_flag v_;
public:
bool try_lock() noexcept
{
return !v_.test_and_set( std::memory_order_acquire );
}
void lock() noexcept
{
for( unsigned k = 0; !try_lock(); ++k )
{
boost::detail::yield( k );
}
}
void unlock() noexcept
{
v_ .clear( std::memory_order_release );
}
public:
class scoped_lock
{
private:
spinlock & sp_;
scoped_lock( scoped_lock const & );
scoped_lock & operator=( scoped_lock const & );
public:
explicit scoped_lock( spinlock & sp ) noexcept: sp_( sp )
{
sp.lock();
}
~scoped_lock() /*noexcept*/
{
sp_.unlock();
}
};
};
} // namespace detail
} // namespace boost
#define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT }
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED

View File

@@ -0,0 +1,49 @@
#ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// boost/smart_ptr/detail/yield_k.hpp
//
// Copyright 2008, 2020 Peter Dimov
//
// inline void boost::detail::yield( unsigned k );
//
// Typical use:
// for( unsigned k = 0; !try_lock(); ++k ) yield( k );
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/yield_primitives.hpp>
namespace boost
{
namespace detail
{
inline void yield( unsigned k )
{
// Experiments on Windows and Fedora 32 show that a single pause,
// followed by an immediate sp_thread_sleep(), is best.
if( k & 1 )
{
boost::core::sp_thread_sleep();
}
else
{
boost::core::sp_thread_pause();
}
}
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED

View File

@@ -0,0 +1,40 @@
#ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_HPP_INCLUDED
#define BOOST_SMART_PTR_ENABLE_SHARED_FROM_HPP_INCLUDED
// enable_shared_from.hpp
//
// Copyright 2019, 2020 Peter Dimov
//
// 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
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/enable_shared_from_this.hpp>
namespace boost
{
class enable_shared_from: public enable_shared_from_this<enable_shared_from>
{
private:
using enable_shared_from_this<enable_shared_from>::shared_from_this;
using enable_shared_from_this<enable_shared_from>::weak_from_this;
};
template<class T> shared_ptr<T> shared_from( T * p )
{
return shared_ptr<T>( p->enable_shared_from_this<enable_shared_from>::shared_from_this(), p );
}
template<class T> weak_ptr<T> weak_from( T * p ) noexcept
{
return weak_ptr<T>( p->enable_shared_from_this<enable_shared_from>::weak_from_this(), p );
}
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_HPP_INCLUDED

View File

@@ -0,0 +1,161 @@
#ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
#define BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
//
// enable_shared_from_raw.hpp
//
// Copyright 2002, 2009, 2014 Peter Dimov
// Copyright 2008-2009 Frank Mori Hess
//
// 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
//
#include <boost/config.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/assert.hpp>
#include <boost/config/workaround.hpp>
namespace boost
{
template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
template<typename T> boost::weak_ptr<T> weak_from_raw(T *);
namespace detail
{
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
} // namespace detail
class enable_shared_from_raw
{
protected:
enable_shared_from_raw()
{
}
enable_shared_from_raw( enable_shared_from_raw const & )
{
}
enable_shared_from_raw & operator=( enable_shared_from_raw const & )
{
return *this;
}
~enable_shared_from_raw()
{
BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
}
private:
void init_if_expired() const
{
if( weak_this_.expired() )
{
shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
weak_this_ = shared_this_;
}
}
void init_if_empty() const
{
if( weak_this_._empty() )
{
shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
weak_this_ = shared_this_;
}
}
private:
template<class Y> friend class shared_ptr;
template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
shared_ptr<void const volatile> shared_from_this() const
{
init_if_expired();
return shared_ptr<void const volatile>( weak_this_ );
}
shared_ptr<void const volatile> shared_from_this() const volatile
{
return const_cast< enable_shared_from_raw const * >( this )->shared_from_this();
}
weak_ptr<void const volatile> weak_from_this() const
{
init_if_empty();
return weak_this_;
}
weak_ptr<void const volatile> weak_from_this() const volatile
{
return const_cast< enable_shared_from_raw const * >( this )->weak_from_this();
}
// Note: invoked automatically by shared_ptr; do not call
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * ) const
{
BOOST_ASSERT( ppx != 0 );
if( weak_this_.expired() )
{
weak_this_ = *ppx;
}
else if( shared_this_.use_count() != 0 )
{
BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
BOOST_ASSERT( pd != 0 );
pd->set_deleter( *ppx );
ppx->reset( shared_this_, ppx->get() );
shared_this_.reset();
}
}
mutable weak_ptr<void const volatile> weak_this_;
private:
mutable shared_ptr<void const volatile> shared_this_;
};
template<typename T>
boost::shared_ptr<T> shared_from_raw(T *p)
{
BOOST_ASSERT(p != 0);
return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
}
template<typename T>
boost::weak_ptr<T> weak_from_raw(T *p)
{
BOOST_ASSERT(p != 0);
boost::weak_ptr<T> result(p->enable_shared_from_raw::weak_from_this(), p);
return result;
}
namespace detail
{
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe )
{
if( pe != 0 )
{
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
}
}
} // namepsace detail
} // namespace boost
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED

View File

@@ -0,0 +1,88 @@
#ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
#define BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
//
// enable_shared_from_this.hpp
//
// Copyright 2002, 2009 Peter Dimov
//
// 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
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
#include <boost/smart_ptr/weak_ptr.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/assert.hpp>
namespace boost
{
template<class T> class enable_shared_from_this
{
protected:
constexpr enable_shared_from_this() noexcept
{
}
constexpr enable_shared_from_this(enable_shared_from_this const &) noexcept
{
}
enable_shared_from_this & operator=(enable_shared_from_this const &) noexcept
{
return *this;
}
~enable_shared_from_this() noexcept // ~weak_ptr<T> newer throws, so this call also must not throw
{
}
public:
shared_ptr<T> shared_from_this()
{
shared_ptr<T> p( weak_this_ );
BOOST_ASSERT( p.get() == this );
return p;
}
shared_ptr<T const> shared_from_this() const
{
shared_ptr<T const> p( weak_this_ );
BOOST_ASSERT( p.get() == this );
return p;
}
weak_ptr<T> weak_from_this() noexcept
{
return weak_this_;
}
weak_ptr<T const> weak_from_this() const noexcept
{
return weak_this_;
}
public: // actually private, but avoids compiler template friendship issues
// Note: invoked automatically by shared_ptr; do not call
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const noexcept
{
if( weak_this_.expired() )
{
weak_this_ = shared_ptr<T>( *ppx, py );
}
}
private:
mutable weak_ptr<T> weak_this_;
};
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED

View File

@@ -0,0 +1,321 @@
#ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
//
// intrusive_ptr.hpp
//
// Copyright (c) 2001, 2002 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
#include <boost/smart_ptr/detail/sp_cxx20_constexpr.hpp>
#include <boost/smart_ptr/detail/sp_convertible.hpp>
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
#include <boost/assert.hpp>
#include <functional> // for std::less
#include <iosfwd> // for std::basic_ostream
#include <cstddef>
namespace boost
{
//
// intrusive_ptr
//
// A smart pointer that uses intrusive reference counting.
//
// Relies on unqualified calls to
//
// void intrusive_ptr_add_ref(T * p);
// void intrusive_ptr_release(T * p);
//
// (p != 0)
//
// The object is responsible for destroying itself.
//
template<class T> class intrusive_ptr
{
private:
typedef intrusive_ptr this_type;
public:
typedef T element_type;
constexpr intrusive_ptr() noexcept : px( 0 )
{
}
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr( T * p, bool add_ref = true ): px( p )
{
if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
}
template<class U>
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
: px( rhs.get() )
{
if( px != 0 ) intrusive_ptr_add_ref( px );
}
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
{
if( px != 0 ) intrusive_ptr_add_ref( px );
}
BOOST_SP_CXX20_CONSTEXPR ~intrusive_ptr()
{
if( px != 0 ) intrusive_ptr_release( px );
}
template<class U> BOOST_SP_CXX20_CONSTEXPR intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
{
this_type(rhs).swap(*this);
return *this;
}
// Move support
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr(intrusive_ptr && rhs) noexcept : px( rhs.px )
{
rhs.px = 0;
}
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr & operator=(intrusive_ptr && rhs) noexcept
{
this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
return *this;
}
template<class U> friend class intrusive_ptr;
template<class U>
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr(intrusive_ptr<U> && rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty())
: px( rhs.px )
{
rhs.px = 0;
}
template<class U>
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr & operator=(intrusive_ptr<U> && rhs) noexcept
{
this_type( static_cast< intrusive_ptr<U> && >( rhs ) ).swap(*this);
return *this;
}
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr & operator=(intrusive_ptr const & rhs)
{
this_type(rhs).swap(*this);
return *this;
}
BOOST_SP_CXX20_CONSTEXPR intrusive_ptr & operator=(T * rhs)
{
this_type(rhs).swap(*this);
return *this;
}
BOOST_SP_CXX20_CONSTEXPR void reset()
{
this_type().swap( *this );
}
BOOST_SP_CXX20_CONSTEXPR void reset( T * rhs )
{
this_type( rhs ).swap( *this );
}
BOOST_SP_CXX20_CONSTEXPR void reset( T * rhs, bool add_ref )
{
this_type( rhs, add_ref ).swap( *this );
}
BOOST_SP_CXX20_CONSTEXPR T * get() const noexcept
{
return px;
}
BOOST_SP_CXX20_CONSTEXPR T * detach() noexcept
{
T * ret = px;
px = 0;
return ret;
}
BOOST_SP_CXX20_CONSTEXPR T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
return *px;
}
BOOST_SP_CXX20_CONSTEXPR T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
return px;
}
BOOST_SP_CXX20_CONSTEXPR explicit operator bool () const noexcept
{
return px != 0;
}
BOOST_SP_CXX20_CONSTEXPR void swap(intrusive_ptr & rhs) noexcept
{
T * tmp = px;
px = rhs.px;
rhs.px = tmp;
}
private:
T * px;
};
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) noexcept
{
return a.get() == b.get();
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) noexcept
{
return a.get() != b.get();
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline bool operator==(intrusive_ptr<T> const & a, U * b) noexcept
{
return a.get() == b;
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline bool operator!=(intrusive_ptr<T> const & a, U * b) noexcept
{
return a.get() != b;
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline bool operator==(T * a, intrusive_ptr<U> const & b) noexcept
{
return a == b.get();
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline bool operator!=(T * a, intrusive_ptr<U> const & b) noexcept
{
return a != b.get();
}
template<class T> BOOST_SP_CXX20_CONSTEXPR inline bool operator==( intrusive_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> BOOST_SP_CXX20_CONSTEXPR inline bool operator==( std::nullptr_t, intrusive_ptr<T> const & p ) noexcept
{
return p.get() == 0;
}
template<class T> BOOST_SP_CXX20_CONSTEXPR inline bool operator!=( intrusive_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() != 0;
}
template<class T> BOOST_SP_CXX20_CONSTEXPR inline bool operator!=( std::nullptr_t, intrusive_ptr<T> const & p ) noexcept
{
return p.get() != 0;
}
template<class T> BOOST_SP_CXX20_CONSTEXPR inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) noexcept
{
return std::less<T *>()(a.get(), b.get());
}
template<class T> BOOST_SP_CXX20_CONSTEXPR inline void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs) noexcept
{
lhs.swap(rhs);
}
// mem_fn support
template<class T> BOOST_SP_CXX20_CONSTEXPR inline T * get_pointer(intrusive_ptr<T> const & p) noexcept
{
return p.get();
}
// pointer casts
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
{
return static_cast<T *>(p.get());
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
{
return const_cast<T *>(p.get());
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
{
return dynamic_cast<T *>(p.get());
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline intrusive_ptr<T> static_pointer_cast( intrusive_ptr<U> && p ) noexcept
{
return intrusive_ptr<T>( static_cast<T*>( p.detach() ), false );
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline intrusive_ptr<T> const_pointer_cast( intrusive_ptr<U> && p ) noexcept
{
return intrusive_ptr<T>( const_cast<T*>( p.detach() ), false );
}
template<class T, class U> BOOST_SP_CXX20_CONSTEXPR inline intrusive_ptr<T> dynamic_pointer_cast( intrusive_ptr<U> && p ) noexcept
{
T * p2 = dynamic_cast<T*>( p.get() );
intrusive_ptr<T> r( p2, false );
if( p2 ) p.detach();
return r;
}
// operator<<
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
{
os << p.get();
return os;
}
// hash_value
template< class T > struct hash;
template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p ) noexcept
{
return boost::hash< T* >()( p.get() );
}
} // namespace boost
// std::hash
namespace std
{
template<class T> struct hash< ::boost::intrusive_ptr<T> >
{
std::size_t operator()( ::boost::intrusive_ptr<T> const & p ) const noexcept
{
return std::hash< T* >()( p.get() );
}
};
} // namespace std
#endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED

View File

@@ -0,0 +1,187 @@
/*
* Copyright Andrey Semashev 2007 - 2013.
* 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)
*/
/*!
* \file intrusive_ref_counter.hpp
* \author Andrey Semashev
* \date 12.03.2009
*
* This header contains a reference counter class for \c intrusive_ptr.
*/
#ifndef BOOST_SMART_PTR_INTRUSIVE_REF_COUNTER_HPP_INCLUDED_
#define BOOST_SMART_PTR_INTRUSIVE_REF_COUNTER_HPP_INCLUDED_
#include <boost/smart_ptr/detail/atomic_count.hpp>
#include <boost/config.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif
#if defined(_MSC_VER)
#pragma warning(push)
// This is a bogus MSVC warning, which is flagged by friend declarations of intrusive_ptr_add_ref and intrusive_ptr_release in intrusive_ref_counter:
// 'name' : the inline specifier cannot be used when a friend declaration refers to a specialization of a function template
// Note that there is no inline specifier in the declarations.
#pragma warning(disable: 4396)
#endif
namespace boost {
namespace sp_adl_block {
/*!
* \brief Thread unsafe reference counter policy for \c intrusive_ref_counter
*
* The policy instructs the \c intrusive_ref_counter base class to implement
* a reference counter suitable for single threaded use only. Pointers to the same
* object with this kind of reference counter must not be used by different threads.
*/
struct thread_unsafe_counter
{
typedef unsigned int type;
static unsigned int load(unsigned int const& counter) noexcept
{
return counter;
}
static void increment(unsigned int& counter) noexcept
{
++counter;
}
static unsigned int decrement(unsigned int& counter) noexcept
{
return --counter;
}
};
/*!
* \brief Thread safe reference counter policy for \c intrusive_ref_counter
*
* The policy instructs the \c intrusive_ref_counter base class to implement
* a thread-safe reference counter, if the target platform supports multithreading.
*/
struct thread_safe_counter
{
typedef boost::detail::atomic_count type;
static unsigned int load(boost::detail::atomic_count const& counter) noexcept
{
return static_cast< unsigned int >(static_cast< long >(counter));
}
static void increment(boost::detail::atomic_count& counter) noexcept
{
++counter;
}
static unsigned int decrement(boost::detail::atomic_count& counter) noexcept
{
return static_cast< unsigned int >(--counter);
}
};
template< typename DerivedT, typename CounterPolicyT = thread_safe_counter >
class intrusive_ref_counter;
template< typename DerivedT, typename CounterPolicyT >
void intrusive_ptr_add_ref(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) noexcept;
template< typename DerivedT, typename CounterPolicyT >
void intrusive_ptr_release(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) noexcept;
/*!
* \brief A reference counter base class
*
* This base class can be used with user-defined classes to add support
* for \c intrusive_ptr. The class contains a reference counter defined by the \c CounterPolicyT.
* Upon releasing the last \c intrusive_ptr referencing the object
* derived from the \c intrusive_ref_counter class, operator \c delete
* is automatically called on the pointer to the object.
*
* The other template parameter, \c DerivedT, is the user's class that derives from \c intrusive_ref_counter.
*/
template< typename DerivedT, typename CounterPolicyT >
class intrusive_ref_counter
{
private:
//! Reference counter type
typedef typename CounterPolicyT::type counter_type;
//! Reference counter
mutable counter_type m_ref_counter;
public:
/*!
* Default constructor
*
* \post <tt>use_count() == 0</tt>
*/
intrusive_ref_counter() noexcept : m_ref_counter(0)
{
}
/*!
* Copy constructor
*
* \post <tt>use_count() == 0</tt>
*/
intrusive_ref_counter(intrusive_ref_counter const&) noexcept : m_ref_counter(0)
{
}
/*!
* Assignment
*
* \post The reference counter is not modified after assignment
*/
intrusive_ref_counter& operator= (intrusive_ref_counter const&) noexcept { return *this; }
/*!
* \return The reference counter
*/
unsigned int use_count() const noexcept
{
return CounterPolicyT::load(m_ref_counter);
}
protected:
/*!
* Destructor
*/
BOOST_DEFAULTED_FUNCTION(~intrusive_ref_counter(), {})
friend void intrusive_ptr_add_ref< DerivedT, CounterPolicyT >(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) noexcept;
friend void intrusive_ptr_release< DerivedT, CounterPolicyT >(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) noexcept;
};
template< typename DerivedT, typename CounterPolicyT >
inline void intrusive_ptr_add_ref(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) noexcept
{
CounterPolicyT::increment(p->m_ref_counter);
}
template< typename DerivedT, typename CounterPolicyT >
inline void intrusive_ptr_release(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) noexcept
{
if (CounterPolicyT::decrement(p->m_ref_counter) == 0)
delete static_cast< const DerivedT* >(p);
}
} // namespace sp_adl_block
using sp_adl_block::intrusive_ref_counter;
using sp_adl_block::thread_unsafe_counter;
using sp_adl_block::thread_safe_counter;
} // namespace boost
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif // BOOST_SMART_PTR_INTRUSIVE_REF_COUNTER_HPP_INCLUDED_

View File

@@ -0,0 +1,642 @@
#ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
// local_shared_ptr.hpp
//
// Copyright 2017 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
#include <boost/assert.hpp>
namespace boost
{
template<class T> class local_shared_ptr;
namespace detail
{
template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
{
boost::detail::sp_assert_convertible< Y, E >();
typedef boost::detail::local_sp_deleter< boost::checked_deleter<Y> > D;
boost::shared_ptr<E> p2( p, D() );
D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
pd->pn_ = p2._internal_count();
pn = pd;
}
template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
{
boost::detail::sp_assert_convertible< Y[], E[] >();
typedef boost::detail::local_sp_deleter< boost::checked_array_deleter<E> > D;
boost::shared_ptr<E[]> p2( p, D() );
D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
pd->pn_ = p2._internal_count();
pn = pd;
}
template< class E, std::size_t N, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[N] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
{
boost::detail::sp_assert_convertible< Y[N], E[N] >();
typedef boost::detail::local_sp_deleter< boost::checked_array_deleter<E> > D;
boost::shared_ptr<E[N]> p2( p, D() );
D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
pd->pn_ = p2._internal_count();
pn = pd;
}
template< class E, class P, class D > inline void lsp_deleter_construct( boost::local_shared_ptr< E > * /*ppx*/, P p, D const& d, boost::detail::local_counted_base * & pn )
{
typedef boost::detail::local_sp_deleter<D> D2;
boost::shared_ptr<E> p2( p, D2( d ) );
D2 * pd = static_cast< D2 * >( p2._internal_get_untyped_deleter() );
pd->pn_ = p2._internal_count();
pn = pd;
}
template< class E, class P, class D, class A > inline void lsp_allocator_construct( boost::local_shared_ptr< E > * /*ppx*/, P p, D const& d, A const& a, boost::detail::local_counted_base * & pn )
{
typedef boost::detail::local_sp_deleter<D> D2;
boost::shared_ptr<E> p2( p, D2( d ), a );
D2 * pd = static_cast< D2 * >( p2._internal_get_untyped_deleter() );
pd->pn_ = p2._internal_count();
pn = pd;
}
struct lsp_internal_constructor_tag
{
};
} // namespace detail
//
// local_shared_ptr
//
// as shared_ptr, but local to a thread.
// reference count manipulations are non-atomic.
//
template<class T> class local_shared_ptr
{
private:
typedef local_shared_ptr this_type;
public:
typedef typename boost::detail::sp_element<T>::type element_type;
private:
element_type * px;
boost::detail::local_counted_base * pn;
template<class Y> friend class local_shared_ptr;
public:
// destructor
~local_shared_ptr() noexcept
{
if( pn )
{
pn->release();
}
}
// constructors
constexpr local_shared_ptr() noexcept : px( 0 ), pn( 0 )
{
}
constexpr local_shared_ptr( std::nullptr_t ) noexcept : px( 0 ), pn( 0 )
{
}
// internal constructor, used by make_shared
constexpr local_shared_ptr( boost::detail::lsp_internal_constructor_tag, element_type * px_, boost::detail::local_counted_base * pn_ ) noexcept : px( px_ ), pn( pn_ )
{
}
template<class Y>
explicit local_shared_ptr( Y * p ): px( p ), pn( 0 )
{
boost::detail::lsp_pointer_construct( this, p, pn );
}
template<class Y, class D> local_shared_ptr( Y * p, D d ): px( p ), pn( 0 )
{
boost::detail::lsp_deleter_construct( this, p, d, pn );
}
template<class D> local_shared_ptr( std::nullptr_t p, D d ): px( p ), pn( 0 )
{
boost::detail::lsp_deleter_construct( this, p, d, pn );
}
template<class Y, class D, class A> local_shared_ptr( Y * p, D d, A a ): px( p ), pn( 0 )
{
boost::detail::lsp_allocator_construct( this, p, d, a, pn );
}
template<class D, class A> local_shared_ptr( std::nullptr_t p, D d, A a ): px( p ), pn( 0 )
{
boost::detail::lsp_allocator_construct( this, p, d, a, pn );
}
// construction from shared_ptr
template<class Y> local_shared_ptr( shared_ptr<Y> const & r,
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
: px( r.get() ), pn( 0 )
{
boost::detail::sp_assert_convertible< Y, T >();
if( r.use_count() != 0 )
{
pn = new boost::detail::local_counted_impl( r._internal_count() );
}
}
template<class Y> local_shared_ptr( shared_ptr<Y> && r,
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
: px( r.get() ), pn( 0 )
{
boost::detail::sp_assert_convertible< Y, T >();
if( r.use_count() != 0 )
{
pn = new boost::detail::local_counted_impl( r._internal_count() );
r.reset();
}
}
// construction from unique_ptr
template< class Y, class D >
local_shared_ptr( std::unique_ptr< Y, D > && r,
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
: px( r.get() ), pn( 0 )
{
boost::detail::sp_assert_convertible< Y, T >();
if( px )
{
pn = new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) )._internal_count() );
}
}
template< class Y, class D >
local_shared_ptr( boost::movelib::unique_ptr< Y, D > r ); // !
// : px( r.get() ), pn( new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) ) ) )
//{
// boost::detail::sp_assert_convertible< Y, T >();
//}
// copy constructor
local_shared_ptr( local_shared_ptr const & r ) noexcept : px( r.px ), pn( r.pn )
{
if( pn )
{
pn->add_ref();
}
}
// move constructor
local_shared_ptr( local_shared_ptr && r ) noexcept : px( r.px ), pn( r.pn )
{
r.px = 0;
r.pn = 0;
}
// converting copy constructor
template<class Y> local_shared_ptr( local_shared_ptr<Y> const & r,
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) noexcept
: px( r.px ), pn( r.pn )
{
boost::detail::sp_assert_convertible< Y, T >();
if( pn )
{
pn->add_ref();
}
}
// converting move constructor
template<class Y> local_shared_ptr( local_shared_ptr<Y> && r,
typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) noexcept
: px( r.px ), pn( r.pn )
{
boost::detail::sp_assert_convertible< Y, T >();
r.px = 0;
r.pn = 0;
}
// aliasing
template<class Y>
local_shared_ptr( local_shared_ptr<Y> const & r, element_type * p ) noexcept : px( p ), pn( r.pn )
{
if( pn )
{
pn->add_ref();
}
}
template<class Y>
local_shared_ptr( local_shared_ptr<Y> && r, element_type * p ) noexcept : px( p ), pn( r.pn )
{
r.px = 0;
r.pn = 0;
}
// assignment
local_shared_ptr & operator=( local_shared_ptr const & r ) noexcept
{
local_shared_ptr( r ).swap( *this );
return *this;
}
template<class Y> local_shared_ptr & operator=( local_shared_ptr<Y> const & r ) noexcept
{
local_shared_ptr( r ).swap( *this );
return *this;
}
local_shared_ptr & operator=( local_shared_ptr && r ) noexcept
{
local_shared_ptr( std::move( r ) ).swap( *this );
return *this;
}
template<class Y>
local_shared_ptr & operator=( local_shared_ptr<Y> && r ) noexcept
{
local_shared_ptr( std::move( r ) ).swap( *this );
return *this;
}
local_shared_ptr & operator=( std::nullptr_t ) noexcept
{
local_shared_ptr().swap(*this);
return *this;
}
template<class Y, class D>
local_shared_ptr & operator=( std::unique_ptr<Y, D> && r )
{
local_shared_ptr( std::move(r) ).swap( *this );
return *this;
}
template<class Y, class D>
local_shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r ); // !
// reset
void reset() noexcept
{
local_shared_ptr().swap( *this );
}
template<class Y> void reset( Y * p ) // Y must be complete
{
local_shared_ptr( p ).swap( *this );
}
template<class Y, class D> void reset( Y * p, D d )
{
local_shared_ptr( p, d ).swap( *this );
}
template<class Y, class D, class A> void reset( Y * p, D d, A a )
{
local_shared_ptr( p, d, a ).swap( *this );
}
template<class Y> void reset( local_shared_ptr<Y> const & r, element_type * p ) noexcept
{
local_shared_ptr( r, p ).swap( *this );
}
template<class Y> void reset( local_shared_ptr<Y> && r, element_type * p ) noexcept
{
local_shared_ptr( std::move( r ), p ).swap( *this );
}
// accessors
typename boost::detail::sp_dereference< T >::type operator* () const noexcept
{
return *px;
}
typename boost::detail::sp_member_access< T >::type operator-> () const noexcept
{
return px;
}
typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
}
element_type * get() const noexcept
{
return px;
}
explicit operator bool () const noexcept
{
return px != 0;
}
long local_use_count() const noexcept
{
return pn? pn->local_use_count(): 0;
}
// conversions to shared_ptr, weak_ptr
template<class Y, class E = typename boost::detail::sp_enable_if_convertible<T,Y>::type> operator shared_ptr<Y>() const noexcept
{
boost::detail::sp_assert_convertible<T, Y>();
if( pn )
{
return shared_ptr<Y>( boost::detail::sp_internal_constructor_tag(), px, pn->local_cb_get_shared_count() );
}
else
{
return shared_ptr<Y>();
}
}
template<class Y, class E = typename boost::detail::sp_enable_if_convertible<T,Y>::type> operator weak_ptr<Y>() const noexcept
{
boost::detail::sp_assert_convertible<T, Y>();
if( pn )
{
return shared_ptr<Y>( boost::detail::sp_internal_constructor_tag(), px, pn->local_cb_get_shared_count() );
}
else
{
return weak_ptr<Y>();
}
}
// swap
void swap( local_shared_ptr & r ) noexcept
{
std::swap( px, r.px );
std::swap( pn, r.pn );
}
// owner_before
template<class Y> bool owner_before( local_shared_ptr<Y> const & r ) const noexcept
{
return std::less< boost::detail::local_counted_base* >()( pn, r.pn );
}
// owner_equals
template<class Y> bool owner_equals( local_shared_ptr<Y> const & r ) const noexcept
{
return pn == r.pn;
}
};
template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) noexcept
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) noexcept
{
return a.get() != b.get();
}
template<class T> inline bool operator==( local_shared_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator==( std::nullptr_t, local_shared_ptr<T> const & p ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator!=( local_shared_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator!=( std::nullptr_t, local_shared_ptr<T> const & p ) noexcept
{
return p.get() != 0;
}
template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) noexcept
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) noexcept
{
return a.get() != b.get();
}
template<class T, class U> inline bool operator==( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) noexcept
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) noexcept
{
return a.get() != b.get();
}
template<class T, class U> inline bool operator<(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept
{
return a.owner_before( b );
}
template<class T> inline void swap( local_shared_ptr<T> & a, local_shared_ptr<T> & b ) noexcept
{
a.swap( b );
}
template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> const & r ) noexcept
{
(void) static_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = static_cast< E* >( r.get() );
return local_shared_ptr<T>( r, p );
}
template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> const & r ) noexcept
{
(void) const_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = const_cast< E* >( r.get() );
return local_shared_ptr<T>( r, p );
}
template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> const & r ) noexcept
{
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = dynamic_cast< E* >( r.get() );
return p? local_shared_ptr<T>( r, p ): local_shared_ptr<T>();
}
template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> const & r ) noexcept
{
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = reinterpret_cast< E* >( r.get() );
return local_shared_ptr<T>( r, p );
}
template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> && r ) noexcept
{
(void) static_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = static_cast< E* >( r.get() );
return local_shared_ptr<T>( std::move(r), p );
}
template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> && r ) noexcept
{
(void) const_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = const_cast< E* >( r.get() );
return local_shared_ptr<T>( std::move(r), p );
}
template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> && r ) noexcept
{
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = dynamic_cast< E* >( r.get() );
return p? local_shared_ptr<T>( std::move(r), p ): local_shared_ptr<T>();
}
template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> && r ) noexcept
{
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
typedef typename local_shared_ptr<T>::element_type E;
E * p = reinterpret_cast< E* >( r.get() );
return local_shared_ptr<T>( std::move(r), p );
}
// get_pointer() enables boost::mem_fn to recognize local_shared_ptr
template<class T> inline typename local_shared_ptr<T>::element_type * get_pointer( local_shared_ptr<T> const & p ) noexcept
{
return p.get();
}
// operator<<
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< ( std::basic_ostream<E, T> & os, local_shared_ptr<Y> const & p )
{
os << p.get();
return os;
}
// get_deleter
template<class D, class T> D * get_deleter( local_shared_ptr<T> const & p ) noexcept
{
return get_deleter<D>( shared_ptr<T>( p ) );
}
// hash_value
template< class T > struct hash;
template< class T > std::size_t hash_value( local_shared_ptr<T> const & p ) noexcept
{
return boost::hash< typename local_shared_ptr<T>::element_type* >()( p.get() );
}
} // namespace boost
// std::hash
namespace std
{
template<class T> struct hash< ::boost::local_shared_ptr<T> >
{
std::size_t operator()( ::boost::local_shared_ptr<T> const & p ) const noexcept
{
return std::hash< typename ::boost::local_shared_ptr<T>::element_type* >()( p.get() );
}
};
} // namespace std
#endif // #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED

View File

@@ -0,0 +1,17 @@
#ifndef BOOST_SMART_PTR_MAKE_LOCAL_SHARED_HPP_INCLUDED
#define BOOST_SMART_PTR_MAKE_LOCAL_SHARED_HPP_INCLUDED
// make_local_shared.hpp
//
// Copyright 2017 Peter Dimov
//
// 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
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/make_local_shared_object.hpp>
#include <boost/smart_ptr/make_local_shared_array.hpp>
#endif // #ifndef BOOST_SMART_PTR_MAKE_LOCAL_SHARED_HPP_INCLUDED

View File

@@ -0,0 +1,76 @@
/*
Copyright 2017 Peter Dimov
Copyright 2017-2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_SMART_PTR_MAKE_LOCAL_SHARED_ARRAY_HPP
#define BOOST_SMART_PTR_MAKE_LOCAL_SHARED_ARRAY_HPP
#include <boost/core/default_allocator.hpp>
#include <boost/smart_ptr/allocate_local_shared_array.hpp>
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <type_traits>
namespace boost {
template<class T>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
local_shared_ptr<T> >::type
make_local_shared()
{
return boost::allocate_local_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>());
}
template<class T>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
local_shared_ptr<T> >::type
make_local_shared(const typename std::remove_extent<T>::type& value)
{
return boost::allocate_local_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), value);
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
local_shared_ptr<T> >::type
make_local_shared(std::size_t size)
{
return boost::allocate_local_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), size);
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
local_shared_ptr<T> >::type
make_local_shared(std::size_t size,
const typename std::remove_extent<T>::type& value)
{
return boost::allocate_local_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), size, value);
}
template<class T>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value,
local_shared_ptr<T> >::type
make_local_shared_noinit()
{
return boost::allocate_local_shared_noinit<T>(boost::
default_allocator<typename detail::sp_array_element<T>::type>());
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
local_shared_ptr<T> >::type
make_local_shared_noinit(std::size_t size)
{
return boost::allocate_local_shared_noinit<T>(boost::
default_allocator<typename detail::sp_array_element<T>::type>(), size);
}
} /* boost */
#endif

View File

@@ -0,0 +1,170 @@
#ifndef BOOST_SMART_PTR_MAKE_LOCAL_SHARED_OBJECT_HPP_INCLUDED
#define BOOST_SMART_PTR_MAKE_LOCAL_SHARED_OBJECT_HPP_INCLUDED
// make_local_shared_object.hpp
//
// Copyright 2017 Peter Dimov
//
// 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
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/local_shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/config.hpp>
#include <type_traits>
#include <utility>
#include <cstddef>
namespace boost
{
namespace detail
{
// lsp_if_not_array
template<class T> struct lsp_if_not_array
{
typedef boost::local_shared_ptr<T> type;
};
template<class T> struct lsp_if_not_array<T[]>
{
};
template<class T, std::size_t N> struct lsp_if_not_array<T[N]>
{
};
// lsp_ms_deleter
template<class T, class A> class lsp_ms_deleter: public local_counted_impl_em
{
private:
typedef typename sp_aligned_storage<sizeof(T), std::alignment_of<T>::value>::type storage_type;
storage_type storage_;
A a_;
bool initialized_;
private:
void destroy() noexcept
{
if( initialized_ )
{
T * p = reinterpret_cast< T* >( storage_.data_ );
std::allocator_traits<A>::destroy( a_, p );
initialized_ = false;
}
}
public:
explicit lsp_ms_deleter( A const & a ) noexcept : a_( a ), initialized_( false )
{
}
// optimization: do not copy storage_
lsp_ms_deleter( lsp_ms_deleter const & r ) noexcept : a_( r.a_), initialized_( false )
{
}
~lsp_ms_deleter() noexcept
{
destroy();
}
void operator()( T * ) noexcept
{
destroy();
}
static void operator_fn( T* ) noexcept // operator() can't be static
{
}
void * address() noexcept
{
return storage_.data_;
}
void set_initialized() noexcept
{
initialized_ = true;
}
};
} // namespace detail
template<class T, class A, class... Args> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared( A const & a, Args&&... args )
{
typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
A2 a2( a );
typedef boost::detail::lsp_ms_deleter<T, A2> D;
boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
void * pv = pd->address();
std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
pd->set_initialized();
T * pt2 = static_cast< T* >( pv );
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
pd->pn_ = pt._internal_count();
return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
}
template<class T, class A> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared_noinit( A const & a )
{
typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
A2 a2( a );
typedef boost::detail::lsp_ms_deleter< T, std::allocator<T> > D;
boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
void * pv = pd->address();
::new( pv ) T;
pd->set_initialized();
T * pt2 = static_cast< T* >( pv );
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
pd->pn_ = pt._internal_count();
return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
}
template<class T, class... Args> typename boost::detail::lsp_if_not_array<T>::type make_local_shared( Args&&... args )
{
typedef typename std::remove_const<T>::type T2;
return boost::allocate_local_shared<T2>( std::allocator<T2>(), std::forward<Args>(args)... );
}
template<class T> typename boost::detail::lsp_if_not_array<T>::type make_local_shared_noinit()
{
typedef typename std::remove_const<T>::type T2;
return boost::allocate_shared_noinit<T2>( std::allocator<T2>() );
}
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED

View File

@@ -0,0 +1,18 @@
#ifndef BOOST_SMART_PTR_MAKE_SHARED_HPP_INCLUDED
#define BOOST_SMART_PTR_MAKE_SHARED_HPP_INCLUDED
// make_shared.hpp
//
// Copyright (c) 2007, 2008, 2012 Peter Dimov
//
// 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
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/smart_ptr/make_shared_array.hpp>
#include <boost/smart_ptr/allocate_shared_array.hpp>
#endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_HPP_INCLUDED

View File

@@ -0,0 +1,68 @@
/*
Copyright 2012-2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_SMART_PTR_MAKE_SHARED_ARRAY_HPP
#define BOOST_SMART_PTR_MAKE_SHARED_ARRAY_HPP
#include <boost/core/default_allocator.hpp>
#include <boost/smart_ptr/allocate_shared_array.hpp>
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <type_traits>
namespace boost {
template<class T>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value, shared_ptr<T> >::type
make_shared()
{
return boost::allocate_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>());
}
template<class T>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value, shared_ptr<T> >::type
make_shared(const typename std::remove_extent<T>::type& value)
{
return boost::allocate_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), value);
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value, shared_ptr<T> >::type
make_shared(std::size_t size)
{
return boost::allocate_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), size);
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value, shared_ptr<T> >::type
make_shared(std::size_t size, const typename std::remove_extent<T>::type& value)
{
return boost::allocate_shared<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), size, value);
}
template<class T>
inline typename std::enable_if<detail::sp_is_bounded_array<T>::value, shared_ptr<T> >::type
make_shared_noinit()
{
return boost::allocate_shared_noinit<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>());
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value, shared_ptr<T> >::type
make_shared_noinit(std::size_t size)
{
return boost::allocate_shared_noinit<T>(boost::default_allocator<typename
detail::sp_array_element<T>::type>(), size);
}
} /* boost */
#endif

View File

@@ -0,0 +1,265 @@
#ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED
#define BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED
// make_shared_object.hpp
//
// Copyright (c) 2007, 2008, 2012 Peter Dimov
//
// 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
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <boost/config.hpp>
#include <utility>
#include <cstddef>
#include <new>
#include <type_traits>
namespace boost
{
namespace detail
{
template< std::size_t N, std::size_t A > struct sp_aligned_storage
{
union type
{
char data_[ N ];
typename sp_type_with_alignment< A >::type align_;
};
};
template< class T > class sp_ms_deleter
{
private:
typedef typename sp_aligned_storage< sizeof( T ), std::alignment_of< T >::value >::type storage_type;
bool initialized_;
storage_type storage_;
private:
void destroy() noexcept
{
if( initialized_ )
{
#if defined( __GNUC__ )
// fixes incorrect aliasing warning
T * p = reinterpret_cast< T* >( storage_.data_ );
p->~T();
#else
reinterpret_cast< T* >( storage_.data_ )->~T();
#endif
initialized_ = false;
}
}
public:
sp_ms_deleter() noexcept : initialized_( false )
{
}
template<class A> explicit sp_ms_deleter( A const & ) noexcept : initialized_( false )
{
}
// optimization: do not copy storage_
sp_ms_deleter( sp_ms_deleter const & ) noexcept : initialized_( false )
{
}
~sp_ms_deleter() noexcept
{
destroy();
}
void operator()( T * ) noexcept
{
destroy();
}
static void operator_fn( T* ) noexcept // operator() can't be static
{
}
void * address() noexcept
{
return storage_.data_;
}
void set_initialized() noexcept
{
initialized_ = true;
}
};
template< class T, class A > class sp_as_deleter
{
private:
typedef typename sp_aligned_storage< sizeof( T ), std::alignment_of< T >::value >::type storage_type;
storage_type storage_;
A a_;
bool initialized_;
private:
void destroy() noexcept
{
if( initialized_ )
{
T * p = reinterpret_cast< T* >( storage_.data_ );
std::allocator_traits<A>::destroy( a_, p );
initialized_ = false;
}
}
public:
sp_as_deleter( A const & a ) noexcept : a_( a ), initialized_( false )
{
}
// optimization: do not copy storage_
sp_as_deleter( sp_as_deleter const & r ) noexcept : a_( r.a_), initialized_( false )
{
}
~sp_as_deleter() noexcept
{
destroy();
}
void operator()( T * ) noexcept
{
destroy();
}
static void operator_fn( T* ) noexcept // operator() can't be static
{
}
void * address() noexcept
{
return storage_.data_;
}
void set_initialized() noexcept
{
initialized_ = true;
}
};
template< class T > struct sp_if_not_array
{
typedef boost::shared_ptr< T > type;
};
template< class T > struct sp_if_not_array< T[] >
{
};
template< class T, std::size_t N > struct sp_if_not_array< T[N] >
{
};
} // namespace detail
#define BOOST_SP_MSD( T ) boost::detail::sp_inplace_tag< boost::detail::sp_ms_deleter< T > >()
// _noinit versions
template< class T > typename boost::detail::sp_if_not_array< T >::type make_shared_noinit()
{
boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );
boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
void * pv = pd->address();
::new( pv ) T;
pd->set_initialized();
T * pt2 = static_cast< T* >( pv );
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
return boost::shared_ptr< T >( pt, pt2 );
}
template< class T, class A > typename boost::detail::sp_if_not_array< T >::type allocate_shared_noinit( A const & a )
{
boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );
boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
void * pv = pd->address();
::new( pv ) T;
pd->set_initialized();
T * pt2 = static_cast< T* >( pv );
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
return boost::shared_ptr< T >( pt, pt2 );
}
//
template< class T, class... Args > typename boost::detail::sp_if_not_array< T >::type make_shared( Args && ... args )
{
boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );
boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );
void * pv = pd->address();
::new( pv ) T( std::forward<Args>( args )... );
pd->set_initialized();
T * pt2 = static_cast< T* >( pv );
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
return boost::shared_ptr< T >( pt, pt2 );
}
template< class T, class A, class... Args > typename boost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, Args && ... args )
{
typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
A2 a2( a );
typedef boost::detail::sp_as_deleter< T, A2 > D;
boost::shared_ptr< T > pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
void * pv = pd->address();
std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
pd->set_initialized();
T * pt2 = static_cast< T* >( pv );
boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
return boost::shared_ptr< T >( pt, pt2 );
}
#undef BOOST_SP_MSD
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED

View File

@@ -0,0 +1,64 @@
/*
Copyright 2012-2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_SMART_PTR_MAKE_UNIQUE_HPP
#define BOOST_SMART_PTR_MAKE_UNIQUE_HPP
#include <boost/smart_ptr/detail/sp_type_traits.hpp>
#include <memory>
#include <type_traits>
#include <utility>
namespace boost {
template<class T>
inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
make_unique()
{
return std::unique_ptr<T>(new T());
}
template<class T, class... Args>
inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T>
inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
make_unique(typename std::remove_reference<T>::type&& value)
{
return std::unique_ptr<T>(new T(std::move(value)));
}
template<class T>
inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
make_unique_noinit()
{
return std::unique_ptr<T>(new T);
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
std::unique_ptr<T> >::type
make_unique(std::size_t size)
{
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
}
template<class T>
inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
std::unique_ptr<T> >::type
make_unique_noinit(std::size_t size)
{
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]);
}
} /* boost */
#endif

View File

@@ -0,0 +1,25 @@
#ifndef BOOST_SMART_PTR_OWNER_EQUAL_TO_HPP_INCLUDED
#define BOOST_SMART_PTR_OWNER_EQUAL_TO_HPP_INCLUDED
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
namespace boost
{
template<class T = void> struct owner_equal_to
{
typedef bool result_type;
typedef T first_argument_type;
typedef T second_argument_type;
template<class U, class V> bool operator()( U const & u, V const & v ) const noexcept
{
return u.owner_equals( v );
}
};
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_OWNER_EQUAL_TO_HPP_INCLUDED

View File

@@ -0,0 +1,26 @@
#ifndef BOOST_SMART_PTR_OWNER_HASH_HPP_INCLUDED
#define BOOST_SMART_PTR_OWNER_HASH_HPP_INCLUDED
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <cstddef>
namespace boost
{
template<class T> struct owner_hash
{
typedef std::size_t result_type;
typedef T argument_type;
std::size_t operator()( T const & t ) const noexcept
{
return t.owner_hash_value();
}
};
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_OWNER_HASH_HPP_INCLUDED

View File

@@ -0,0 +1,34 @@
#ifndef BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED
#define BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED
//
// owner_less.hpp
//
// Copyright (c) 2008 Frank Mori Hess
// Copyright (c) 2016 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
namespace boost
{
template<class T = void> struct owner_less
{
typedef bool result_type;
typedef T first_argument_type;
typedef T second_argument_type;
template<class U, class V> bool operator()( U const & u, V const & v ) const noexcept
{
return u.owner_before( v );
}
};
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED

View File

@@ -0,0 +1,114 @@
#ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
#define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
// Copyright (c) 2001, 2002 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#include <boost/core/checked_delete.hpp>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <cstddef> // for std::ptrdiff_t
namespace boost
{
// scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
// is guaranteed, either on destruction of the scoped_array or via an explicit
// reset(). Use shared_array or std::vector if your needs are more complex.
template<class T> class scoped_array // noncopyable
{
private:
T * px;
scoped_array(scoped_array const &);
scoped_array & operator=(scoped_array const &);
typedef scoped_array<T> this_type;
void operator==( scoped_array const& ) const;
void operator!=( scoped_array const& ) const;
public:
typedef T element_type;
explicit scoped_array( T * p = 0 ) noexcept : px( p )
{
}
~scoped_array() noexcept
{
boost::checked_array_delete( px );
}
void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type(p).swap(*this);
}
T & operator[](std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
BOOST_ASSERT( i >= 0 );
return px[i];
}
T * get() const noexcept
{
return px;
}
explicit operator bool () const noexcept
{
return px != 0;
}
void swap(scoped_array & b) noexcept
{
T * tmp = b.px;
b.px = px;
px = tmp;
}
};
template<class T> inline bool operator==( scoped_array<T> const & p, std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator==( std::nullptr_t, scoped_array<T> const & p ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator!=( scoped_array<T> const & p, std::nullptr_t ) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator!=( std::nullptr_t, scoped_array<T> const & p ) noexcept
{
return p.get() != 0;
}
template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) noexcept
{
a.swap(b);
}
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED

View File

@@ -0,0 +1,148 @@
#ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
// Copyright (c) 2001, 2002 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
#include <boost/core/checked_delete.hpp>
#include <boost/assert.hpp>
#include <boost/config/workaround.hpp>
#include <boost/config.hpp>
#include <cstddef>
#ifndef BOOST_NO_AUTO_PTR
# include <memory> // for std::auto_ptr
#endif
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
namespace boost
{
// scoped_ptr mimics a built-in pointer except that it guarantees deletion
// of the object pointed to, either on destruction of the scoped_ptr or via
// an explicit reset(). scoped_ptr is a simple solution for simple needs;
// use shared_ptr or std::auto_ptr if your needs are more complex.
template<class T> class scoped_ptr // noncopyable
{
private:
T * px;
scoped_ptr(scoped_ptr const &);
scoped_ptr & operator=(scoped_ptr const &);
typedef scoped_ptr<T> this_type;
void operator==( scoped_ptr const& ) const;
void operator!=( scoped_ptr const& ) const;
public:
typedef T element_type;
explicit scoped_ptr( T * p = 0 ) noexcept : px( p )
{
}
#ifndef BOOST_NO_AUTO_PTR
explicit scoped_ptr( std::auto_ptr<T> p ) noexcept : px( p.release() )
{
}
#endif
~scoped_ptr() noexcept
{
boost::checked_delete( px );
}
void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type(p).swap(*this);
}
T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
return *px;
}
T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
return px;
}
T * get() const noexcept
{
return px;
}
explicit operator bool () const noexcept
{
return px != 0;
}
void swap(scoped_ptr & b) noexcept
{
T * tmp = b.px;
b.px = px;
px = tmp;
}
};
template<class T> inline bool operator==( scoped_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator==( std::nullptr_t, scoped_ptr<T> const & p ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator!=( scoped_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator!=( std::nullptr_t, scoped_ptr<T> const & p ) noexcept
{
return p.get() != 0;
}
template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) noexcept
{
a.swap(b);
}
// get_pointer(p) is a generic way to say p.get()
template<class T> inline T * get_pointer(scoped_ptr<T> const & p) noexcept
{
return p.get();
}
} // namespace boost
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic pop
#endif
#endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED

View File

@@ -0,0 +1,261 @@
#ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
#define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
//
// shared_array.hpp
//
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
// Copyright (c) 2001, 2002, 2012 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/shared_count.hpp>
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
#include <boost/core/checked_delete.hpp>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <algorithm> // for std::swap
#include <functional> // for std::less
#include <cstddef> // for std::ptrdiff_t
namespace boost
{
//
// shared_array
//
// shared_array extends shared_ptr to arrays.
// The array pointed to is deleted when the last shared_array pointing to it
// is destroyed or reset.
//
template<class T> class shared_array
{
private:
// Borland 5.5.1 specific workarounds
typedef checked_array_deleter<T> deleter;
typedef shared_array<T> this_type;
public:
typedef T element_type;
shared_array() noexcept : px( 0 ), pn()
{
}
shared_array( std::nullptr_t ) noexcept : px( 0 ), pn()
{
}
template<class Y>
explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
{
boost::detail::sp_assert_convertible< Y[], T[] >();
}
//
// Requirements: D's copy constructor must not throw
//
// shared_array will release p by calling d(p)
//
template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
{
boost::detail::sp_assert_convertible< Y[], T[] >();
}
// As above, but with allocator. A's copy constructor shall not throw.
template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
{
boost::detail::sp_assert_convertible< Y[], T[] >();
}
// generated copy constructor, destructor are fine...
// ... except in C++0x, move disables the implicit copy
shared_array( shared_array const & r ) noexcept : px( r.px ), pn( r.pn )
{
}
shared_array( shared_array && r ) noexcept : px( r.px ), pn()
{
pn.swap( r.pn );
r.px = 0;
}
// conversion
template<class Y>
shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
noexcept : px( r.px ), pn( r.pn )
{
boost::detail::sp_assert_convertible< Y[], T[] >();
}
// aliasing
template< class Y >
shared_array( shared_array<Y> const & r, element_type * p ) noexcept : px( p ), pn( r.pn )
{
}
// assignment
shared_array & operator=( shared_array const & r ) noexcept
{
this_type( r ).swap( *this );
return *this;
}
template<class Y>
shared_array & operator=( shared_array<Y> const & r ) noexcept
{
this_type( r ).swap( *this );
return *this;
}
shared_array & operator=( shared_array && r ) noexcept
{
this_type( static_cast< shared_array && >( r ) ).swap( *this );
return *this;
}
template<class Y>
shared_array & operator=( shared_array<Y> && r ) noexcept
{
this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
return *this;
}
void reset() noexcept
{
this_type().swap( *this );
}
template<class Y> void reset( Y * p ) // Y must be complete
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type( p ).swap( *this );
}
template<class Y, class D> void reset( Y * p, D d )
{
this_type( p, d ).swap( *this );
}
template<class Y, class D, class A> void reset( Y * p, D d, A a )
{
this_type( p, d, a ).swap( *this );
}
template<class Y> void reset( shared_array<Y> const & r, element_type * p ) noexcept
{
this_type( r, p ).swap( *this );
}
T & operator[] (std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT(px != 0);
BOOST_ASSERT(i >= 0);
return px[i];
}
T * get() const noexcept
{
return px;
}
explicit operator bool () const noexcept
{
return px != 0;
}
bool unique() const noexcept
{
return pn.unique();
}
long use_count() const noexcept
{
return pn.use_count();
}
void swap(shared_array<T> & other) noexcept
{
std::swap(px, other.px);
pn.swap(other.pn);
}
void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const noexcept
{
return pn.get_deleter( ti );
}
private:
template<class Y> friend class shared_array;
T * px; // contained pointer
detail::shared_count pn; // reference counter
}; // shared_array
template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) noexcept
{
return a.get() == b.get();
}
template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) noexcept
{
return a.get() != b.get();
}
template<class T> inline bool operator==( shared_array<T> const & p, std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator==( std::nullptr_t, shared_array<T> const & p ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator!=( shared_array<T> const & p, std::nullptr_t ) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator!=( std::nullptr_t, shared_array<T> const & p ) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) noexcept
{
return std::less<T*>()(a.get(), b.get());
}
template<class T> void swap(shared_array<T> & a, shared_array<T> & b) noexcept
{
a.swap(b);
}
template< class D, class T > D * get_deleter( shared_array<T> const & p ) noexcept
{
return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID_(D) ) );
}
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED

View File

@@ -0,0 +1,986 @@
#ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
//
// shared_ptr.hpp
//
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
// Copyright (c) 2001-2008 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
#include <boost/smart_ptr/detail/shared_count.hpp>
#include <boost/smart_ptr/detail/sp_convertible.hpp>
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
#include <boost/core/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
#include <boost/smart_ptr/detail/spinlock_pool.hpp>
#endif
#include <algorithm> // for std::swap
#include <functional> // for std::less
#include <typeinfo> // for std::bad_cast
#include <cstddef> // for std::size_t
#include <memory> // for std::auto_ptr
#include <iosfwd> // for std::basic_ostream
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
namespace boost
{
template<class T> class shared_ptr;
template<class T> class weak_ptr;
template<class T> class enable_shared_from_this;
class enable_shared_from_raw;
namespace movelib
{
template< class T, class D > class unique_ptr;
} // namespace movelib
namespace detail
{
// sp_element, element_type
template< class T > struct sp_element
{
typedef T type;
};
template< class T > struct sp_element< T[] >
{
typedef T type;
};
template< class T, std::size_t N > struct sp_element< T[N] >
{
typedef T type;
};
// sp_dereference, return type of operator*
template< class T > struct sp_dereference
{
typedef T & type;
};
template<> struct sp_dereference< void >
{
typedef void type;
};
template<> struct sp_dereference< void const >
{
typedef void type;
};
template<> struct sp_dereference< void volatile >
{
typedef void type;
};
template<> struct sp_dereference< void const volatile >
{
typedef void type;
};
template< class T > struct sp_dereference< T[] >
{
typedef void type;
};
template< class T, std::size_t N > struct sp_dereference< T[N] >
{
typedef void type;
};
// sp_member_access, return type of operator->
template< class T > struct sp_member_access
{
typedef T * type;
};
template< class T > struct sp_member_access< T[] >
{
typedef void type;
};
template< class T, std::size_t N > struct sp_member_access< T[N] >
{
typedef void type;
};
// sp_array_access, return type of operator[]
template< class T > struct sp_array_access
{
typedef void type;
};
template< class T > struct sp_array_access< T[] >
{
typedef T & type;
};
template< class T, std::size_t N > struct sp_array_access< T[N] >
{
typedef T & type;
};
// sp_extent, for operator[] index check
template< class T > struct sp_extent
{
enum _vt { value = 0 };
};
template< class T, std::size_t N > struct sp_extent< T[N] >
{
enum _vt { value = N };
};
// enable_shared_from_this support
template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> const * ppx, Y const * py, boost::enable_shared_from_this< T > const * pe )
{
if( pe != 0 )
{
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
}
}
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
#ifdef _MANAGED
// Avoid C4793, ... causes native code generation
struct sp_any_pointer
{
template<class T> sp_any_pointer( T* ) {}
};
inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
{
}
#else // _MANAGED
inline void sp_enable_shared_from_this( ... )
{
}
#endif // _MANAGED
// sp_assert_convertible
template< class Y, class T > inline void sp_assert_convertible() noexcept
{
static_assert( sp_convertible< Y, T >::value, "incompatible pointer type" );
}
// pointer constructor helper
template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
{
boost::detail::shared_count( p ).swap( pn );
boost::detail::sp_enable_shared_from_this( ppx, p, p );
}
template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
{
sp_assert_convertible< Y[], T[] >();
boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
}
template< class T, std::size_t N, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
{
sp_assert_convertible< Y[N], T[N] >();
boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
}
// deleter constructor helper
template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p )
{
boost::detail::sp_enable_shared_from_this( ppx, p, p );
}
template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ )
{
sp_assert_convertible< Y[], T[] >();
}
template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ )
{
sp_assert_convertible< Y[N], T[N] >();
}
struct sp_internal_constructor_tag
{
};
} // namespace detail
//
// shared_ptr
//
// An enhanced relative of scoped_ptr with reference counted copy semantics.
// The object pointed to is deleted when the last shared_ptr pointing to it
// is destroyed or reset.
//
template<class T> class shared_ptr
{
private:
// Borland 5.5.1 specific workaround
typedef shared_ptr<T> this_type;
public:
typedef typename boost::detail::sp_element< T >::type element_type;
constexpr shared_ptr() noexcept : px( 0 ), pn()
{
}
constexpr shared_ptr( std::nullptr_t ) noexcept : px( 0 ), pn()
{
}
constexpr shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count const & pn_ ) noexcept : px( px_ ), pn( pn_ )
{
}
constexpr shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count && pn_ ) noexcept : px( px_ ), pn( std::move( pn_ ) )
{
}
template<class Y>
explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
{
boost::detail::sp_pointer_construct( this, p, pn );
}
//
// Requirements: D's copy/move constructors must not throw
//
// shared_ptr will release p by calling d(p)
//
template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, static_cast< D&& >( d ) )
{
boost::detail::sp_deleter_construct( this, p );
}
template<class D> shared_ptr( std::nullptr_t p, D d ): px( p ), pn( p, static_cast< D&& >( d ) )
{
}
// As above, but with allocator. A's copy constructor shall not throw.
template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, static_cast< D&& >( d ), a )
{
boost::detail::sp_deleter_construct( this, p );
}
template<class D, class A> shared_ptr( std::nullptr_t p, D d, A a ): px( p ), pn( p, static_cast< D&& >( d ), a )
{
}
// generated copy constructor, destructor are fine...
// ... except in C++0x, move disables the implicit copy
shared_ptr( shared_ptr const & r ) noexcept : px( r.px ), pn( r.pn )
{
}
template<class Y>
explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
{
boost::detail::sp_assert_convertible< Y, T >();
// it is now safe to copy r.px, as pn(r.pn) did not throw
px = r.px;
}
template<class Y>
shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
noexcept : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
{
if( !pn.empty() )
{
px = r.px;
}
}
template<class Y>
shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
noexcept : px( r.px ), pn( r.pn )
{
boost::detail::sp_assert_convertible< Y, T >();
}
// aliasing
template< class Y >
shared_ptr( shared_ptr<Y> const & r, element_type * p ) noexcept : px( p ), pn( r.pn )
{
}
#ifndef BOOST_NO_AUTO_PTR
template<class Y>
explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
{
boost::detail::sp_assert_convertible< Y, T >();
Y * tmp = r.get();
pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( this, tmp );
}
template<class Y>
shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
{
boost::detail::sp_assert_convertible< Y, T >();
Y * tmp = r.get();
pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( this, tmp );
}
#endif // BOOST_NO_AUTO_PTR
template< class Y, class D >
shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
{
boost::detail::sp_assert_convertible< Y, T >();
typename std::unique_ptr< Y, D >::pointer tmp = r.get();
if( tmp != 0 )
{
pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( this, tmp );
}
}
template< class Y, class D >
shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
{
boost::detail::sp_assert_convertible< Y, T >();
typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
if( tmp != 0 )
{
pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( this, tmp );
}
}
// assignment
shared_ptr & operator=( shared_ptr const & r ) noexcept
{
this_type(r).swap(*this);
return *this;
}
template<class Y>
shared_ptr & operator=(shared_ptr<Y> const & r) noexcept
{
this_type(r).swap(*this);
return *this;
}
#ifndef BOOST_NO_AUTO_PTR
template<class Y>
shared_ptr & operator=( std::auto_ptr<Y> & r )
{
this_type( r ).swap( *this );
return *this;
}
template<class Y>
shared_ptr & operator=( std::auto_ptr<Y> && r )
{
this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
return *this;
}
#endif // BOOST_NO_AUTO_PTR
template<class Y, class D>
shared_ptr & operator=( std::unique_ptr<Y, D> && r )
{
this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
return *this;
}
template<class Y, class D>
shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
{
// this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
boost::detail::sp_assert_convertible< Y, T >();
typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
shared_ptr tmp;
if( p != 0 )
{
tmp.px = p;
tmp.pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( &tmp, p );
}
tmp.swap( *this );
return *this;
}
// Move support
shared_ptr( shared_ptr && r ) noexcept : px( r.px ), pn( static_cast< boost::detail::shared_count && >( r.pn ) )
{
r.px = 0;
}
template<class Y>
shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
noexcept : px( r.px ), pn( static_cast< boost::detail::shared_count && >( r.pn ) )
{
boost::detail::sp_assert_convertible< Y, T >();
r.px = 0;
}
shared_ptr & operator=( shared_ptr && r ) noexcept
{
this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
return *this;
}
template<class Y>
shared_ptr & operator=( shared_ptr<Y> && r ) noexcept
{
this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
return *this;
}
// aliasing move
template<class Y>
shared_ptr( shared_ptr<Y> && r, element_type * p ) noexcept : px( p ), pn()
{
pn.swap( r.pn );
r.px = 0;
}
shared_ptr & operator=( std::nullptr_t ) noexcept
{
this_type().swap(*this);
return *this;
}
void reset() noexcept
{
this_type().swap(*this);
}
template<class Y> void reset( Y * p ) // Y must be complete
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type( p ).swap( *this );
}
template<class Y, class D> void reset( Y * p, D d )
{
this_type( p, static_cast< D&& >( d ) ).swap( *this );
}
template<class Y, class D, class A> void reset( Y * p, D d, A a )
{
this_type( p, static_cast< D&& >( d ), a ).swap( *this );
}
template<class Y> void reset( shared_ptr<Y> const & r, element_type * p ) noexcept
{
this_type( r, p ).swap( *this );
}
template<class Y> void reset( shared_ptr<Y> && r, element_type * p ) noexcept
{
this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
}
typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
return *px;
}
typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
return px;
}
typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( px != 0 );
BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
}
element_type * get() const noexcept
{
return px;
}
explicit operator bool () const noexcept
{
return px != 0;
}
bool unique() const noexcept
{
return pn.unique();
}
long use_count() const noexcept
{
return pn.use_count();
}
void swap( shared_ptr & other ) noexcept
{
std::swap(px, other.px);
pn.swap(other.pn);
}
template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const noexcept
{
return pn < rhs.pn;
}
template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const noexcept
{
return pn < rhs.pn;
}
template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const noexcept
{
return pn == rhs.pn;
}
template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const noexcept
{
return pn == rhs.pn;
}
std::size_t owner_hash_value() const noexcept
{
return pn.hash_value();
}
void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const noexcept
{
return pn.get_deleter( ti );
}
void * _internal_get_local_deleter( boost::detail::sp_typeinfo_ const & ti ) const noexcept
{
return pn.get_local_deleter( ti );
}
void * _internal_get_untyped_deleter() const noexcept
{
return pn.get_untyped_deleter();
}
bool _internal_equiv( shared_ptr const & r ) const noexcept
{
return px == r.px && pn == r.pn;
}
boost::detail::shared_count _internal_count() const noexcept
{
return pn;
}
private:
template<class Y> friend class shared_ptr;
template<class Y> friend class weak_ptr;
element_type * px; // contained pointer
boost::detail::shared_count pn; // reference counter
}; // shared_ptr
template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept
{
return a.get() != b.get();
}
template<class T> inline bool operator==( shared_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator==( std::nullptr_t, shared_ptr<T> const & p ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator!=( shared_ptr<T> const & p, std::nullptr_t ) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator!=( std::nullptr_t, shared_ptr<T> const & p ) noexcept
{
return p.get() != 0;
}
template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept
{
return a.owner_before( b );
}
template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) noexcept
{
a.swap(b);
}
template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) noexcept
{
(void) static_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = static_cast< E* >( r.get() );
return shared_ptr<T>( r, p );
}
template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) noexcept
{
(void) const_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = const_cast< E* >( r.get() );
return shared_ptr<T>( r, p );
}
template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) noexcept
{
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = dynamic_cast< E* >( r.get() );
return p? shared_ptr<T>( r, p ): shared_ptr<T>();
}
template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) noexcept
{
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = reinterpret_cast< E* >( r.get() );
return shared_ptr<T>( r, p );
}
template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) noexcept
{
(void) static_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = static_cast< E* >( r.get() );
return shared_ptr<T>( std::move(r), p );
}
template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) noexcept
{
(void) const_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = const_cast< E* >( r.get() );
return shared_ptr<T>( std::move(r), p );
}
template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) noexcept
{
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = dynamic_cast< E* >( r.get() );
return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
}
template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) noexcept
{
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = reinterpret_cast< E* >( r.get() );
return shared_ptr<T>( std::move(r), p );
}
// get_pointer() enables boost::mem_fn to recognize shared_ptr
template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) noexcept
{
return p.get();
}
// operator<<
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
{
os << p.get();
return os;
}
// get_deleter
namespace detail
{
template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) noexcept
{
return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID_(D)) );
}
template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) noexcept;
template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) noexcept;
class esft2_deleter_wrapper
{
private:
shared_ptr<void const volatile> deleter_;
public:
esft2_deleter_wrapper() noexcept
{
}
template< class T > void set_deleter( shared_ptr<T> const & deleter ) noexcept
{
deleter_ = deleter;
}
template<typename D> D* get_deleter() const noexcept
{
return boost::detail::basic_get_deleter<D>( deleter_ );
}
template< class T> void operator()( T* ) BOOST_SP_NOEXCEPT_WITH_ASSERT
{
BOOST_ASSERT( deleter_.use_count() <= 1 );
deleter_.reset();
}
};
} // namespace detail
template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) noexcept
{
D * d = boost::detail::basic_get_deleter<D>( p );
if( d == 0 )
{
d = boost::detail::basic_get_local_deleter( d, p );
}
if( d == 0 )
{
boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
// The following get_deleter method call is fully qualified because
// older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
}
return d;
}
// atomic access
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) noexcept
{
return false;
}
template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) noexcept
{
boost::detail::spinlock_pool<2>::scoped_lock lock( p );
return *p;
}
template<class T, class M> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ M ) noexcept
{
return atomic_load( p );
}
template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) noexcept
{
boost::detail::spinlock_pool<2>::scoped_lock lock( p );
p->swap( r );
}
template<class T, class M> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) noexcept
{
atomic_store( p, r ); // std::move( r )
}
template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) noexcept
{
boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
sp.lock();
p->swap( r );
sp.unlock();
return r; // return std::move( r )
}
template<class T, class M> shared_ptr<T> inline atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) noexcept
{
return atomic_exchange( p, r ); // std::move( r )
}
template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) noexcept
{
boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
sp.lock();
if( p->_internal_equiv( *v ) )
{
p->swap( w );
sp.unlock();
return true;
}
else
{
shared_ptr<T> tmp( *p );
sp.unlock();
tmp.swap( *v );
return false;
}
}
template<class T, class M> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, /*memory_order success*/ M, /*memory_order failure*/ M ) noexcept
{
return atomic_compare_exchange( p, v, w ); // std::move( w )
}
#endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
// hash_value
template< class T > struct hash;
template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) noexcept
{
return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
}
} // namespace boost
// std::hash
namespace std
{
template<class T> struct hash< ::boost::shared_ptr<T> >
{
std::size_t operator()( ::boost::shared_ptr<T> const & p ) const noexcept
{
return std::hash< typename ::boost::shared_ptr<T>::element_type* >()( p.get() );
}
};
} // namespace std
#include <boost/smart_ptr/detail/local_sp_deleter.hpp>
namespace boost
{
namespace detail
{
template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) noexcept
{
return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
}
template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) noexcept
{
return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
}
} // namespace detail
#if defined(__cpp_deduction_guides)
template<class T> shared_ptr( weak_ptr<T> ) -> shared_ptr<T>;
template<class T, class D> shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
#endif
} // namespace boost
#if defined( BOOST_SP_DISABLE_DEPRECATED )
#pragma GCC diagnostic pop
#endif
#endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED

View File

@@ -0,0 +1,268 @@
#ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
//
// weak_ptr.hpp
//
// Copyright (c) 2001, 2002, 2003 Peter Dimov
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/ for documentation.
//
#include <boost/smart_ptr/detail/shared_count.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <memory>
#include <cstddef>
namespace boost
{
template<class T> class weak_ptr
{
private:
// Borland 5.5.1 specific workarounds
typedef weak_ptr<T> this_type;
public:
typedef typename boost::detail::sp_element< T >::type element_type;
constexpr weak_ptr() noexcept : px(0), pn()
{
}
// generated copy constructor, assignment, destructor are fine...
// ... except in C++0x, move disables the implicit copy
weak_ptr( weak_ptr const & r ) noexcept : px( r.px ), pn( r.pn )
{
}
weak_ptr & operator=( weak_ptr const & r ) noexcept
{
px = r.px;
pn = r.pn;
return *this;
}
//
// The "obvious" converting constructor implementation:
//
// template<class Y>
// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn)
// {
// }
//
// has a serious problem.
//
// r.px may already have been invalidated. The px(r.px)
// conversion may require access to *r.px (virtual inheritance).
//
// It is not possible to avoid spurious access violations since
// in multithreaded programs r.px may be invalidated at any point.
//
template<class Y>
weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
noexcept : px(r.lock().get()), pn(r.pn)
{
boost::detail::sp_assert_convertible< Y, T >();
}
template<class Y>
weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
noexcept : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
{
boost::detail::sp_assert_convertible< Y, T >();
r.px = 0;
}
// for better efficiency in the T == Y case
weak_ptr( weak_ptr && r )
noexcept : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
{
r.px = 0;
}
// for better efficiency in the T == Y case
weak_ptr & operator=( weak_ptr && r ) noexcept
{
this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
return *this;
}
template<class Y>
weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
noexcept : px( r.px ), pn( r.pn )
{
boost::detail::sp_assert_convertible< Y, T >();
}
// aliasing
template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) noexcept: px( p ), pn( r.pn )
{
}
template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) noexcept: px( p ), pn( r.pn )
{
}
template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) noexcept: px( p ), pn( std::move( r.pn ) )
{
}
template<class Y>
weak_ptr & operator=( weak_ptr<Y> const & r ) noexcept
{
boost::detail::sp_assert_convertible< Y, T >();
px = r.lock().get();
pn = r.pn;
return *this;
}
template<class Y>
weak_ptr & operator=( weak_ptr<Y> && r ) noexcept
{
this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
return *this;
}
template<class Y>
weak_ptr & operator=( shared_ptr<Y> const & r ) noexcept
{
boost::detail::sp_assert_convertible< Y, T >();
px = r.px;
pn = r.pn;
return *this;
}
shared_ptr<T> lock() const noexcept
{
return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
}
long use_count() const noexcept
{
return pn.use_count();
}
bool expired() const noexcept
{
return pn.use_count() == 0;
}
bool _empty() const noexcept // extension, not in std::weak_ptr
{
return pn.empty();
}
bool empty() const noexcept // extension, not in std::weak_ptr
{
return pn.empty();
}
void reset() noexcept
{
this_type().swap(*this);
}
void swap(this_type & other) noexcept
{
std::swap(px, other.px);
pn.swap(other.pn);
}
template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const noexcept
{
return pn < rhs.pn;
}
template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const noexcept
{
return pn < rhs.pn;
}
template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const noexcept
{
return pn == rhs.pn;
}
template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const noexcept
{
return pn == rhs.pn;
}
std::size_t owner_hash_value() const noexcept
{
return pn.hash_value();
}
private:
template<class Y> friend class weak_ptr;
template<class Y> friend class shared_ptr;
element_type * px; // contained pointer
boost::detail::weak_count pn; // reference counter
}; // weak_ptr
template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) noexcept
{
return a.owner_before( b );
}
template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) noexcept
{
a.swap(b);
}
#if defined(__cpp_deduction_guides)
template<class T> weak_ptr( shared_ptr<T> ) -> weak_ptr<T>;
#endif
// hash_value
template< class T > std::size_t hash_value( boost::weak_ptr<T> const & p ) noexcept
{
return p.owner_hash_value();
}
} // namespace boost
// std::hash, std::equal_to
namespace std
{
template<class T> struct hash< ::boost::weak_ptr<T> >
{
std::size_t operator()( ::boost::weak_ptr<T> const & p ) const noexcept
{
return p.owner_hash_value();
}
};
template<class T> struct equal_to< ::boost::weak_ptr<T> >
{
bool operator()( ::boost::weak_ptr<T> const & a, ::boost::weak_ptr<T> const & b ) const noexcept
{
return a.owner_equals( b );
}
};
} // namespace std
#endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED