From 5a915eb0e697fed503e07a25076af6d20cdbd405 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Wed, 24 Apr 2019 15:05:05 +0200
Subject: [PATCH] sticker/Database: return Sticker by value

---
 src/command/StickerCommands.cxx |  8 +++-----
 src/sticker/SongSticker.cxx     |  5 +++--
 src/sticker/SongSticker.hxx     |  6 +++---
 src/sticker/StickerDatabase.cxx | 14 ++------------
 src/sticker/StickerDatabase.hxx | 12 ++----------
 5 files changed, 13 insertions(+), 32 deletions(-)

diff --git a/src/command/StickerCommands.cxx b/src/command/StickerCommands.cxx
index 7ef3f4e6f..f68462798 100644
--- a/src/command/StickerCommands.cxx
+++ b/src/command/StickerCommands.cxx
@@ -21,6 +21,7 @@
 #include "Request.hxx"
 #include "SongPrint.hxx"
 #include "db/Interface.hxx"
+#include "sticker/Sticker.hxx"
 #include "sticker/SongSticker.hxx"
 #include "sticker/StickerPrint.hxx"
 #include "sticker/StickerDatabase.hxx"
@@ -76,11 +77,8 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
 		assert(song != nullptr);
 		AtScopeExit(&db, song) { db.ReturnSong(song); };
 
-		Sticker *sticker = sticker_song_get(*song);
-		if (sticker) {
-			sticker_print(r, *sticker);
-			sticker_free(sticker);
-		}
+		const auto sticker = sticker_song_get(*song);
+		sticker_print(r, sticker);
 
 		return CommandResult::OK;
 	/* set song song_id id key */
diff --git a/src/sticker/SongSticker.cxx b/src/sticker/SongSticker.cxx
index 06ef108c3..39b92f11d 100644
--- a/src/sticker/SongSticker.cxx
+++ b/src/sticker/SongSticker.cxx
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003-2018 The Music Player Daemon Project
+ * Copyright 2003-2019 The Music Player Daemon Project
  * http://www.musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -18,6 +18,7 @@
  */
 
 #include "SongSticker.hxx"
+#include "Sticker.hxx"
 #include "StickerDatabase.hxx"
 #include "song/LightSong.hxx"
 #include "db/Interface.hxx"
@@ -61,7 +62,7 @@ sticker_song_delete_value(const LightSong &song, const char *name)
 	return sticker_delete_value("song", uri.c_str(), name);
 }
 
-Sticker *
+Sticker
 sticker_song_get(const LightSong &song)
 {
 	const auto uri = song.GetURI();
diff --git a/src/sticker/SongSticker.hxx b/src/sticker/SongSticker.hxx
index c4250d787..28f3eca7c 100644
--- a/src/sticker/SongSticker.hxx
+++ b/src/sticker/SongSticker.hxx
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003-2018 The Music Player Daemon Project
+ * Copyright 2003-2019 The Music Player Daemon Project
  * http://www.musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -72,9 +72,9 @@ sticker_song_delete_value(const LightSong &song, const char *name);
  * Throws #SqliteError on error.
  *
  * @param song the song object
- * @return a sticker object, or nullptr if there is no sticker
+ * @return a sticker object
  */
-Sticker *
+Sticker
 sticker_song_get(const LightSong &song);
 
 /**
diff --git a/src/sticker/StickerDatabase.cxx b/src/sticker/StickerDatabase.cxx
index a649e4e72..ff59090fc 100644
--- a/src/sticker/StickerDatabase.cxx
+++ b/src/sticker/StickerDatabase.cxx
@@ -314,12 +314,6 @@ sticker_delete_value(const char *type, const char *uri, const char *name)
 	return modified;
 }
 
-void
-sticker_free(Sticker *sticker) noexcept
-{
-	delete sticker;
-}
-
 const char *
 sticker_get_value(const Sticker &sticker, const char *name) noexcept
 {
@@ -340,18 +334,14 @@ sticker_foreach(const Sticker &sticker,
 		func(i.first.c_str(), i.second.c_str(), user_data);
 }
 
-Sticker *
+Sticker
 sticker_load(const char *type, const char *uri)
 {
 	Sticker s;
 
 	sticker_list_values(s.table, type, uri);
 
-	if (s.table.empty())
-		/* don't return empty sticker objects */
-		return nullptr;
-
-	return new Sticker(std::move(s));
+	return s;
 }
 
 static sqlite3_stmt *
diff --git a/src/sticker/StickerDatabase.hxx b/src/sticker/StickerDatabase.hxx
index c05ce3558..134213bb6 100644
--- a/src/sticker/StickerDatabase.hxx
+++ b/src/sticker/StickerDatabase.hxx
@@ -108,14 +108,6 @@ sticker_delete(const char *type, const char *uri);
 bool
 sticker_delete_value(const char *type, const char *uri, const char *name);
 
-/**
- * Frees resources held by the sticker object.
- *
- * @param sticker the sticker object to be freed
- */
-void
-sticker_free(Sticker *sticker) noexcept;
-
 /**
  * Determines a single value in a sticker.
  *
@@ -147,9 +139,9 @@ sticker_foreach(const Sticker &sticker,
  *
  * @param type the resource type, e.g. "song"
  * @param uri the URI of the resource, e.g. the song path
- * @return a sticker object, or nullptr if there is no sticker
+ * @return a sticker object
  */
-Sticker *
+Sticker
 sticker_load(const char *type, const char *uri);
 
 /**