1 #ifndef libjmmcg_core_deleter_hpp 2 #define libjmmcg_core_deleter_hpp 3 4 /****************************************************************************** 5 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/deleter.hpp 2055 2017-05-13 19:35:47Z jmmcg $ 6 ** 7 ** Copyright © 2004 by J.M.McGuiness, coder@hussar.me.uk 8 ** 9 ** This library is free software; you can redistribute it and/or 10 ** modify it under the terms of the GNU Lesser General Public 11 ** License as published by the Free Software Foundation; either 12 ** version 2.1 of the License, or (at your option) any later version. 13 ** 14 ** This library is distributed in the hope that it will be useful, 15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 ** Lesser General Public License for more details. 18 ** 19 ** You should have received a copy of the GNU Lesser General Public 20 ** License along with this library; if not, write to the Free Software 21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #include "non_copyable.hpp" 25 26 #include <functional> 27 28 #include <stdlib.h> 29 30 namespace jmmcg { 31 32 /// A trivial class to use global free() to deallocate the memory using an RAII-style wrapper. 33 template<class T> 34 struct free_ptr : protected non_copyable { 35 typedef T element_type; 36 37 const element_type ptr; 38 39 constexpr free_ptr() noexcept(true) 40 : ptr() { 41 } 42 explicit constexpr free_ptr(element_type p) noexcept(true) 43 : ptr(p) { 44 } 45 ~free_ptr() noexcept(true) { 46 ::free(ptr); 47 } 48 }; 49 50 /** 51 Unfortunately std::default_delete does not name the type passed to it. This class fixes that. 52 53 \see std:default_delete 54 */ 55 template<class V> 56 struct default_delete : std::default_delete<V> { 57 typedef V element_type; 58 }; 59 60 /// Another trivial class to make calling the dtor of an object into a functor. 61 template<class V> 62 struct placement_dtor { 63 typedef V element_type; 64 65 void FORCE_INLINE operator()(element_type *v) const noexcept(true) { 66 v->~V(); 67 } 68 }; 69 70 /// Another trivial class to make calling the dtor of an object into a functor, but actually does nothing. 71 template<class V> 72 struct noop_dtor { 73 typedef V element_type; 74 75 constexpr void operator()(element_type *) const noexcept(true) { 76 } 77 }; 78 79 } 80 81 #endif