util/DeleteDisposer: new utility class
This commit is contained in:
@@ -391,6 +391,7 @@ libutil_a_SOURCES = \
|
|||||||
src/util/Macros.hxx \
|
src/util/Macros.hxx \
|
||||||
src/util/Cast.hxx \
|
src/util/Cast.hxx \
|
||||||
src/util/Clamp.hxx \
|
src/util/Clamp.hxx \
|
||||||
|
src/util/DeleteDisposer.hxx \
|
||||||
src/util/Alloc.cxx src/util/Alloc.hxx \
|
src/util/Alloc.cxx src/util/Alloc.hxx \
|
||||||
src/util/VarSize.hxx \
|
src/util/VarSize.hxx \
|
||||||
src/util/Error.cxx src/util/Error.hxx \
|
src/util/Error.cxx src/util/Error.hxx \
|
||||||
|
@@ -51,12 +51,6 @@ public:
|
|||||||
struct playlist &playlist;
|
struct playlist &playlist;
|
||||||
struct PlayerControl &player_control;
|
struct PlayerControl &player_control;
|
||||||
|
|
||||||
struct Disposer {
|
|
||||||
void operator()(Client *client) const {
|
|
||||||
delete client;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned permission;
|
unsigned permission;
|
||||||
|
|
||||||
/** the uid of the client process, or -1 if unknown */
|
/** the uid of the client process, or -1 if unknown */
|
||||||
|
@@ -20,8 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "ClientList.hxx"
|
#include "ClientList.hxx"
|
||||||
#include "ClientInternal.hxx"
|
#include "ClientInternal.hxx"
|
||||||
|
#include "util/DeleteDisposer.hxx"
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
@@ -36,7 +35,7 @@ ClientList::Remove(Client &client)
|
|||||||
void
|
void
|
||||||
ClientList::CloseAll()
|
ClientList::CloseAll()
|
||||||
{
|
{
|
||||||
list.clear_and_dispose(Client::Disposer());
|
list.clear_and_dispose(DeleteDisposer());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@@ -31,6 +31,7 @@
|
|||||||
#include "lib/icu/Collate.hxx"
|
#include "lib/icu/Collate.hxx"
|
||||||
#include "fs/Traits.hxx"
|
#include "fs/Traits.hxx"
|
||||||
#include "util/Alloc.hxx"
|
#include "util/Alloc.hxx"
|
||||||
|
#include "util/DeleteDisposer.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -51,7 +52,7 @@ Directory::~Directory()
|
|||||||
delete mounted_database;
|
delete mounted_database;
|
||||||
|
|
||||||
songs.clear_and_dispose(Song::Disposer());
|
songs.clear_and_dispose(Song::Disposer());
|
||||||
children.clear_and_dispose(Disposer());
|
children.clear_and_dispose(DeleteDisposer());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -61,7 +62,7 @@ Directory::Delete()
|
|||||||
assert(parent != nullptr);
|
assert(parent != nullptr);
|
||||||
|
|
||||||
parent->children.erase_and_dispose(parent->children.iterator_to(*this),
|
parent->children.erase_and_dispose(parent->children.iterator_to(*this),
|
||||||
Disposer());
|
DeleteDisposer());
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
@@ -110,7 +111,8 @@ Directory::PruneEmpty()
|
|||||||
child->PruneEmpty();
|
child->PruneEmpty();
|
||||||
|
|
||||||
if (child->IsEmpty())
|
if (child->IsEmpty())
|
||||||
child = children.erase_and_dispose(child, Disposer());
|
child = children.erase_and_dispose(child,
|
||||||
|
DeleteDisposer());
|
||||||
else
|
else
|
||||||
++child;
|
++child;
|
||||||
}
|
}
|
||||||
|
@@ -53,12 +53,6 @@ struct Directory {
|
|||||||
typedef boost::intrusive::link_mode<link_mode> LinkMode;
|
typedef boost::intrusive::link_mode<link_mode> LinkMode;
|
||||||
typedef boost::intrusive::list_member_hook<LinkMode> Hook;
|
typedef boost::intrusive::list_member_hook<LinkMode> Hook;
|
||||||
|
|
||||||
struct Disposer {
|
|
||||||
void operator()(Directory *directory) const {
|
|
||||||
delete directory;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pointers to the siblings of this directory within the
|
* Pointers to the siblings of this directory within the
|
||||||
* parent directory. It is unused (undefined) in the root
|
* parent directory. It is unused (undefined) in the root
|
||||||
|
@@ -21,6 +21,7 @@
|
|||||||
#include "Manager.hxx"
|
#include "Manager.hxx"
|
||||||
#include "event/Loop.hxx"
|
#include "event/Loop.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
#include "util/DeleteDisposer.hxx"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -65,9 +66,7 @@ NfsManager::~NfsManager()
|
|||||||
|
|
||||||
CollectGarbage();
|
CollectGarbage();
|
||||||
|
|
||||||
connections.clear_and_dispose([](ManagedConnection *c){
|
connections.clear_and_dispose(DeleteDisposer());
|
||||||
delete c;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NfsConnection &
|
NfsConnection &
|
||||||
@@ -95,9 +94,7 @@ NfsManager::CollectGarbage()
|
|||||||
{
|
{
|
||||||
assert(GetEventLoop().IsInside());
|
assert(GetEventLoop().IsInside());
|
||||||
|
|
||||||
garbage.clear_and_dispose([](ManagedConnection *c){
|
garbage.clear_and_dispose(DeleteDisposer());
|
||||||
delete c;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
44
src/util/DeleteDisposer.hxx
Normal file
44
src/util/DeleteDisposer.hxx
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Max Kellermann <max@duempel.org>
|
||||||
|
*
|
||||||
|
* 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<typename T>
|
||||||
|
void operator()(T *t) {
|
||||||
|
delete t;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user