2015-11-17 17:20:52 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace util::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.
|
2015-11-17 17:20:52 +11:00
|
|
|
template <typename T, typename U, void (U::*F)(T*)>
|
2015-11-19 16:46:19 +11:00
|
|
|
class owner_deleter {
|
2015-11-17 17:20:52 +11:00
|
|
|
public:
|
2015-11-19 16:46:19 +11:00
|
|
|
owner_deleter (U &owner):
|
2015-11-17 17:20:52 +11:00
|
|
|
m_owner (owner)
|
|
|
|
{ ; }
|
|
|
|
|
2015-11-24 16:53:10 +11:00
|
|
|
inline void operator() (T *t)
|
2015-11-17 17:20:52 +11:00
|
|
|
{
|
|
|
|
(m_owner.*F) (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
U& m_owner;
|
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-11-17 17:20:52 +11:00
|
|
|
|
|
|
|
#endif
|