diff options
author | Max Kellermann <max@duempel.org> | 2012-08-15 21:59:56 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2012-08-15 23:01:08 +0200 |
commit | fcf0f8291dc23d5db329cd44119d69dde76238b2 (patch) | |
tree | 9d8417c51c4afc16f80abc21da2f186f9eef7720 | |
parent | 0ed71fa0274abc78efb6e2d649cd302ff68b3930 (diff) | |
download | mpd-fcf0f8291dc23d5db329cd44119d69dde76238b2.tar.gz mpd-fcf0f8291dc23d5db329cd44119d69dde76238b2.tar.xz mpd-fcf0f8291dc23d5db329cd44119d69dde76238b2.zip |
ProxyDatabasePlugin: add OO wrapper for mpd_entity
Let the C++ compiler take care for freeing the objects safely.
Diffstat (limited to '')
-rw-r--r-- | src/db/ProxyDatabasePlugin.cxx | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/src/db/ProxyDatabasePlugin.cxx b/src/db/ProxyDatabasePlugin.cxx index f09bb39db..6211f5c74 100644 --- a/src/db/ProxyDatabasePlugin.cxx +++ b/src/db/ProxyDatabasePlugin.cxx @@ -267,13 +267,39 @@ Visit(struct directory &parent, const struct mpd_playlist *playlist, return success; } -static std::list<struct mpd_entity *> +class ProxyEntity { + struct mpd_entity *entity; + +public: + explicit ProxyEntity(struct mpd_entity *_entity) + :entity(_entity) {} + + ProxyEntity(const ProxyEntity &other) = delete; + + ProxyEntity(ProxyEntity &&other) + :entity(other.entity) { + other.entity = nullptr; + } + + ~ProxyEntity() { + if (entity != nullptr) + mpd_entity_free(entity); + } + + ProxyEntity &operator=(const ProxyEntity &other) = delete; + + operator const struct mpd_entity *() const { + return entity; + } +}; + +static std::list<ProxyEntity> ReceiveEntities(struct mpd_connection *connection) { - std::list<struct mpd_entity *> entities; + std::list<ProxyEntity> entities; struct mpd_entity *entity; while ((entity = mpd_recv_entity(connection)) != NULL) - entities.push_back(entity); + entities.push_back(ProxyEntity(entity)); mpd_response_finish(connection); return entities; @@ -287,14 +313,11 @@ Visit(struct mpd_connection *connection, struct directory &parent, if (!mpd_send_list_meta(connection, directory_get_path(&parent))) return CheckError(connection, error_r); - std::list<struct mpd_entity *> entities = ReceiveEntities(connection); - if (!CheckError(connection, error_r)) { - for (auto entity : entities) - mpd_entity_free(entity); + std::list<ProxyEntity> entities(ReceiveEntities(connection)); + if (!CheckError(connection, error_r)) return false; - } - for (auto entity : entities) { + for (const auto &entity : entities) { switch (mpd_entity_get_type(entity)) { case MPD_ENTITY_TYPE_UNKNOWN: break; @@ -316,8 +339,6 @@ Visit(struct mpd_connection *connection, struct directory &parent, visit_playlist, error_r); break; } - - mpd_entity_free(entity); } return CheckError(connection, error_r); |