From 8d2370635408b9acd392843de96179383a95a4bf Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 19 Oct 2015 15:57:00 +0200 Subject: util/DeleteDisposer: new utility class --- Makefile.am | 1 + src/client/Client.hxx | 6 ----- src/client/ClientList.cxx | 5 ++--- src/db/plugins/simple/Directory.cxx | 8 ++++--- src/db/plugins/simple/Directory.hxx | 6 ----- src/lib/nfs/Manager.cxx | 9 +++----- src/util/DeleteDisposer.hxx | 44 +++++++++++++++++++++++++++++++++++++ 7 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 src/util/DeleteDisposer.hxx diff --git a/Makefile.am b/Makefile.am index c7f26f732..28707446b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -391,6 +391,7 @@ libutil_a_SOURCES = \ src/util/Macros.hxx \ src/util/Cast.hxx \ src/util/Clamp.hxx \ + src/util/DeleteDisposer.hxx \ src/util/Alloc.cxx src/util/Alloc.hxx \ src/util/VarSize.hxx \ src/util/Error.cxx src/util/Error.hxx \ diff --git a/src/client/Client.hxx b/src/client/Client.hxx index 0e3047d58..38127b124 100644 --- a/src/client/Client.hxx +++ b/src/client/Client.hxx @@ -51,12 +51,6 @@ public: struct playlist &playlist; struct PlayerControl &player_control; - struct Disposer { - void operator()(Client *client) const { - delete client; - } - }; - unsigned permission; /** the uid of the client process, or -1 if unknown */ diff --git a/src/client/ClientList.cxx b/src/client/ClientList.cxx index 43a35798f..375e99090 100644 --- a/src/client/ClientList.cxx +++ b/src/client/ClientList.cxx @@ -20,8 +20,7 @@ #include "config.h" #include "ClientList.hxx" #include "ClientInternal.hxx" - -#include +#include "util/DeleteDisposer.hxx" #include @@ -36,7 +35,7 @@ ClientList::Remove(Client &client) void ClientList::CloseAll() { - list.clear_and_dispose(Client::Disposer()); + list.clear_and_dispose(DeleteDisposer()); } void diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index b795ee713..0c2b19efb 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -31,6 +31,7 @@ #include "lib/icu/Collate.hxx" #include "fs/Traits.hxx" #include "util/Alloc.hxx" +#include "util/DeleteDisposer.hxx" #include "util/Error.hxx" #include @@ -51,7 +52,7 @@ Directory::~Directory() delete mounted_database; songs.clear_and_dispose(Song::Disposer()); - children.clear_and_dispose(Disposer()); + children.clear_and_dispose(DeleteDisposer()); } void @@ -61,7 +62,7 @@ Directory::Delete() assert(parent != nullptr); parent->children.erase_and_dispose(parent->children.iterator_to(*this), - Disposer()); + DeleteDisposer()); } const char * @@ -110,7 +111,8 @@ Directory::PruneEmpty() child->PruneEmpty(); if (child->IsEmpty()) - child = children.erase_and_dispose(child, Disposer()); + child = children.erase_and_dispose(child, + DeleteDisposer()); else ++child; } diff --git a/src/db/plugins/simple/Directory.hxx b/src/db/plugins/simple/Directory.hxx index 115030a8c..bad4647a0 100644 --- a/src/db/plugins/simple/Directory.hxx +++ b/src/db/plugins/simple/Directory.hxx @@ -53,12 +53,6 @@ struct Directory { typedef boost::intrusive::link_mode LinkMode; typedef boost::intrusive::list_member_hook Hook; - struct Disposer { - void operator()(Directory *directory) const { - delete directory; - } - }; - /** * Pointers to the siblings of this directory within the * parent directory. It is unused (undefined) in the root diff --git a/src/lib/nfs/Manager.cxx b/src/lib/nfs/Manager.cxx index 8b0a9ba9a..1cbf18ff1 100644 --- a/src/lib/nfs/Manager.cxx +++ b/src/lib/nfs/Manager.cxx @@ -21,6 +21,7 @@ #include "Manager.hxx" #include "event/Loop.hxx" #include "Log.hxx" +#include "util/DeleteDisposer.hxx" #include @@ -65,9 +66,7 @@ NfsManager::~NfsManager() CollectGarbage(); - connections.clear_and_dispose([](ManagedConnection *c){ - delete c; - }); + connections.clear_and_dispose(DeleteDisposer()); } NfsConnection & @@ -95,9 +94,7 @@ NfsManager::CollectGarbage() { assert(GetEventLoop().IsInside()); - garbage.clear_and_dispose([](ManagedConnection *c){ - delete c; - }); + garbage.clear_and_dispose(DeleteDisposer()); } void diff --git a/src/util/DeleteDisposer.hxx b/src/util/DeleteDisposer.hxx new file mode 100644 index 000000000..dd91e1299 --- /dev/null +++ b/src/util/DeleteDisposer.hxx @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DELETE_DISPOSER_HXX +#define DELETE_DISPOSER_HXX + +/** + * A disposer for boost::intrusive that invokes the "delete" operator + * on the given pointer. + */ +struct DeleteDisposer { + template + void operator()(T *t) { + delete t; + } +}; + +#endif -- cgit v1.2.3