2015-11-17 17:20:52 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-11-17 17:20:52 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_MEMORY_DELETER_HPP
|
|
|
|
#define __UTIL_MEMORY_DELETER_HPP
|
|
|
|
|
2015-11-24 16:53:10 +11:00
|
|
|
#include <functional>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::memory {
|
2015-11-24 16:53:10 +11:00
|
|
|
template <typename T>
|
|
|
|
class func_deleter {
|
|
|
|
public:
|
|
|
|
using func_t = std::function<void(T*)>;
|
|
|
|
|
2017-05-23 12:50:51 +10:00
|
|
|
explicit func_deleter (func_t _func):
|
2015-11-24 16:53:10 +11:00
|
|
|
m_func (_func)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
inline void operator() (T *t)
|
|
|
|
{ m_func (t); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
func_t m_func;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// dispatch object deletion to a known member function.
|
|
|
|
//
|
|
|
|
// XXX: Generates a "sorry, unimplemented" under GCC (which is
|
|
|
|
// effectively an ICE). Their bug tracker seems to indicate they don't
|
|
|
|
// give a fuck, so we can't use this except under clang.
|
2017-08-30 13:42:49 +10:00
|
|
|
template <
|
|
|
|
typename ValueT,
|
|
|
|
typename OwnerT,
|
|
|
|
void (OwnerT::*Func)(ValueT*)
|
|
|
|
>
|
2015-11-19 16:46:19 +11:00
|
|
|
class owner_deleter {
|
2015-11-17 17:20:52 +11:00
|
|
|
public:
|
2017-08-30 13:42:49 +10:00
|
|
|
owner_deleter (OwnerT &owner):
|
2015-11-17 17:20:52 +11:00
|
|
|
m_owner (owner)
|
|
|
|
{ ; }
|
|
|
|
|
2017-08-30 13:42:49 +10:00
|
|
|
inline void operator() (ValueT *t)
|
2015-11-17 17:20:52 +11:00
|
|
|
{
|
2017-08-30 13:42:49 +10:00
|
|
|
(m_owner.*Func) (t);
|
2015-11-17 17:20:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-30 13:42:49 +10:00
|
|
|
OwnerT& m_owner;
|
2015-11-17 17:20:52 +11:00
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-11-17 17:20:52 +11:00
|
|
|
|
|
|
|
#endif
|