2012-11-06 14:17:32 +00:00
|
|
|
/*
|
2014-01-23 20:40:46 -08:00
|
|
|
* Copyright (c) 2012-2014 Glen Joseph Fernandes
|
2012-11-06 14:17:32 +00:00
|
|
|
* glenfe at live dot com
|
|
|
|
|
*
|
2012-12-03 05:56:17 +00:00
|
|
|
* Distributed under the Boost Software License,
|
|
|
|
|
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
2012-11-06 14:17:32 +00:00
|
|
|
* or copy at http://boost.org/LICENSE_1_0.txt)
|
|
|
|
|
*/
|
|
|
|
|
#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
|
|
|
|
#define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
|
|
|
|
|
2014-02-07 08:03:47 -08:00
|
|
|
#include <boost/smart_ptr/detail/allocator_pair.hpp>
|
2014-01-28 02:27:49 -08:00
|
|
|
#include <boost/smart_ptr/detail/array_traits.hpp>
|
2014-02-07 08:03:47 -08:00
|
|
|
#include <boost/smart_ptr/detail/array_utility.hpp>
|
2012-11-06 14:17:32 +00:00
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
namespace detail {
|
|
|
|
|
template<typename T>
|
2014-02-04 00:29:51 -08:00
|
|
|
struct ms_deleter_base;
|
2014-01-23 20:40:46 -08:00
|
|
|
|
2012-11-28 07:32:30 +00:00
|
|
|
template<typename T>
|
2014-02-04 00:29:51 -08:00
|
|
|
struct ms_deleter_base<T[]> {
|
|
|
|
|
ms_deleter_base(std::size_t size_)
|
2014-02-02 22:00:08 -08:00
|
|
|
: size(size_) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::size_t size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T, std::size_t N>
|
2014-02-04 00:29:51 -08:00
|
|
|
struct ms_deleter_base<T[N]> {
|
2014-02-02 22:00:08 -08:00
|
|
|
enum {
|
|
|
|
|
size = N
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-04 00:29:51 -08:00
|
|
|
template<typename T, typename A>
|
|
|
|
|
class as_deleter
|
|
|
|
|
: ms_deleter_base<T> {
|
|
|
|
|
using ms_deleter_base<T>::size;
|
2014-02-02 22:00:08 -08:00
|
|
|
|
2012-11-06 14:17:32 +00:00
|
|
|
public:
|
2014-02-02 22:00:08 -08:00
|
|
|
typedef typename array_inner<T>::type type;
|
|
|
|
|
|
2014-02-07 08:03:47 -08:00
|
|
|
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
|
|
|
|
typedef typename std::allocator_traits<A>::
|
|
|
|
|
template rebind_alloc<type> allocator;
|
|
|
|
|
#else
|
|
|
|
|
typedef typename A::
|
|
|
|
|
template rebind<type>::other allocator;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
as_deleter(const A& allocator_)
|
|
|
|
|
: pair(allocator_, 0) {
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
2014-02-04 00:29:51 -08:00
|
|
|
|
2014-02-07 08:03:47 -08:00
|
|
|
as_deleter(const A& allocator_, std::size_t size_)
|
2014-02-04 00:29:51 -08:00
|
|
|
: ms_deleter_base<T>(size_),
|
2014-02-07 08:03:47 -08:00
|
|
|
pair(allocator_, 0) {
|
2014-02-04 00:29:51 -08:00
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
2014-02-07 08:03:47 -08:00
|
|
|
void set(type* memory) {
|
2014-02-04 08:43:36 -08:00
|
|
|
pair.data = memory;
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
2012-11-07 18:37:17 +00:00
|
|
|
void operator()(const void*) {
|
2014-02-04 08:43:36 -08:00
|
|
|
if (pair.data) {
|
2014-02-07 08:03:47 -08:00
|
|
|
as_destroy<type, allocator>(pair, pair.data, size);
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
|
2012-12-07 01:53:35 +00:00
|
|
|
private:
|
2014-02-07 08:03:47 -08:00
|
|
|
as_pair<allocator, type*> pair;
|
2014-02-04 00:29:51 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class ms_deleter
|
|
|
|
|
: ms_deleter_base<T> {
|
|
|
|
|
using ms_deleter_base<T>::size;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
typedef typename array_inner<T>::type type;
|
|
|
|
|
|
|
|
|
|
ms_deleter()
|
|
|
|
|
: object(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ms_deleter(std::size_t size_)
|
|
|
|
|
: ms_deleter_base<T>(size_),
|
|
|
|
|
object(0) {
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 08:03:47 -08:00
|
|
|
void set(type* memory) {
|
2014-02-04 00:29:51 -08:00
|
|
|
object = memory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator()(const void*) {
|
|
|
|
|
if (object) {
|
2014-02-07 08:03:47 -08:00
|
|
|
ms_destroy(object, size);
|
2014-02-04 00:29:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
type* object;
|
|
|
|
|
};
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|