From d9ea3082fb608935c72620e272f4b3e74b2c3a44 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Wed, 30 Jan 2013 22:00:32 +0100
Subject: [PATCH] ConfigData: add constructors/destructors

---
 src/ConfigData.cxx           | 35 ++++++-----------------------------
 src/ConfigData.hxx           | 16 +++++++++-------
 src/ConfigFile.cxx           | 10 +++++-----
 src/ConfigGlobal.cxx         |  2 +-
 src/Main.cxx                 |  5 ++---
 test/DumpDatabase.cxx        |  9 ++++-----
 test/run_encoder.cxx         |  7 +++----
 test/test_vorbis_encoder.cxx |  6 +++---
 8 files changed, 33 insertions(+), 57 deletions(-)

diff --git a/src/ConfigData.cxx b/src/ConfigData.cxx
index b542dccd3..67f93a4ab 100644
--- a/src/ConfigData.cxx
+++ b/src/ConfigData.cxx
@@ -31,29 +31,12 @@ extern "C" {
 #include <string.h>
 #include <stdlib.h>
 
-struct config_param *
-config_new_param(const char *value, int line)
+config_param::config_param(const char *_value, int _line)
+	:value(g_strdup(_value)), line(_line) {}
+
+config_param::~config_param()
 {
-	config_param *ret = new config_param();
-
-	if (!value)
-		ret->value = NULL;
-	else
-		ret->value = g_strdup(value);
-
-	ret->line = line;
-
-	ret->used = false;
-
-	return ret;
-}
-
-void
-config_param_free(struct config_param *param)
-{
-	g_free(param->value);
-
-	delete param;
+	g_free(value);
 }
 
 void
@@ -62,13 +45,7 @@ config_add_block_param(struct config_param * param, const char *name,
 {
 	assert(config_get_block_param(param, name) == NULL);
 
-	param->block_params.push_back(block_param());
-	struct block_param *bp = &param->block_params.back();
-
-	bp->name = name;
-	bp->value = value;
-	bp->line = line;
-	bp->used = false;
+	param->block_params.emplace_back(name, value, line);
 }
 
 const struct block_param *
diff --git a/src/ConfigData.hxx b/src/ConfigData.hxx
index b71c0a9e4..36e3ea332 100644
--- a/src/ConfigData.hxx
+++ b/src/ConfigData.hxx
@@ -45,6 +45,10 @@ struct block_param {
 	 * this option yet.
 	 */
 	mutable bool used;
+
+	gcc_nonnull_all
+	block_param(const char *_name, const char *_value, int _line=-1)
+		:name(_name), value(_value), line(_line), used(false) {}
 };
 
 #endif
@@ -61,6 +65,11 @@ struct config_param {
 	 * this option yet.
 	 */
 	bool used;
+
+	config_param(int _line=-1)
+		:value(nullptr), line(_line), used(false) {}
+	config_param(const char *_value, int _line=-1);
+	~config_param();
 #endif
 };
 
@@ -76,13 +85,6 @@ struct ConfigData {
 extern "C" {
 #endif
 
-gcc_malloc
-struct config_param *
-config_new_param(const char *value, int line);
-
-void
-config_param_free(struct config_param *param);
-
 void
 config_add_block_param(struct config_param * param, const char *name,
 		       const char *value, int line);
diff --git a/src/ConfigFile.cxx b/src/ConfigFile.cxx
index aebc4dc0a..aea2e58ae 100644
--- a/src/ConfigFile.cxx
+++ b/src/ConfigFile.cxx
@@ -90,7 +90,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
 static struct config_param *
 config_read_block(FILE *fp, int *count, char *string, GError **error_r)
 {
-	struct config_param *ret = config_new_param(NULL, *count);
+	struct config_param *ret = new config_param(*count);
 	GError *error = NULL;
 
 	while (true) {
@@ -98,7 +98,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
 
 		line = fgets(string, MAX_STRING_SIZE, fp);
 		if (line == NULL) {
-			config_param_free(ret);
+			delete ret;
 			g_set_error(error_r, config_quark(), 0,
 				    "Expected '}' before end-of-file");
 			return NULL;
@@ -115,7 +115,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
 
 			line = strchug_fast(line + 1);
 			if (*line != 0 && *line != CONF_COMMENT) {
-				config_param_free(ret);
+				delete ret;
 				g_set_error(error_r, config_quark(), 0,
 					    "line %i: Unknown tokens after '}'",
 					    *count);
@@ -129,7 +129,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
 
 		if (!config_read_name_value(ret, line, *count, &error)) {
 			assert(*line != 0);
-			config_param_free(ret);
+			delete ret;
 			g_propagate_prefixed_error(error_r, error,
 						   "line %i: ", *count);
 			return NULL;
@@ -241,7 +241,7 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
 				return false;
 			}
 
-			param = config_new_param(value, count);
+			param = new config_param(value, count);
 		}
 
 		params = g_slist_append(params, param);
diff --git a/src/ConfigGlobal.cxx b/src/ConfigGlobal.cxx
index 92e0b5410..0c9af0563 100644
--- a/src/ConfigGlobal.cxx
+++ b/src/ConfigGlobal.cxx
@@ -44,7 +44,7 @@ config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
 {
 	struct config_param *param = (struct config_param *)data;
 
-	config_param_free(param);
+	delete param;
 }
 
 void config_global_finish(void)
diff --git a/src/Main.cxx b/src/Main.cxx
index 63a78d0e4..613a253b2 100644
--- a/src/Main.cxx
+++ b/src/Main.cxx
@@ -189,7 +189,7 @@ glue_db_init_and_load(void)
 	struct config_param *allocated = NULL;
 
 	if (param == NULL && path != NULL) {
-		allocated = config_new_param("database", path->line);
+		allocated = new config_param("database", path->line);
 		config_add_block_param(allocated, "path",
 				       path->value, path->line);
 		param = allocated;
@@ -198,8 +198,7 @@ glue_db_init_and_load(void)
 	if (!DatabaseGlobalInit(param, &error))
 		MPD_ERROR("%s", error->message);
 
-	if (allocated != NULL)
-		config_param_free(allocated);
+	delete allocated;
 
 	ret = DatabaseGlobalOpen(&error);
 	if (!ret)
diff --git a/test/DumpDatabase.cxx b/test/DumpDatabase.cxx
index 501c80206..172d00491 100644
--- a/test/DumpDatabase.cxx
+++ b/test/DumpDatabase.cxx
@@ -106,13 +106,12 @@ main(int argc, char **argv)
 	/* do it */
 
 	const struct config_param *path = config_get_param(CONF_DB_FILE);
-	struct config_param *param = config_new_param("database", path->line);
+	config_param param("database", path->line);
 	if (path != nullptr)
-		config_add_block_param(param, "path", path->value, path->line);
+		config_add_block_param(&param, "path", path->value,
+				       path->line);
 
-	Database *db = plugin->create(param, &error);
-
-	config_param_free(param);
+	Database *db = plugin->create(&param, &error);
 
 	if (db == nullptr) {
 		cerr << error->message << endl;
diff --git a/test/run_encoder.cxx b/test/run_encoder.cxx
index 3d500d0b2..e2ecab583 100644
--- a/test/run_encoder.cxx
+++ b/test/run_encoder.cxx
@@ -49,7 +49,6 @@ int main(int argc, char **argv)
 	const char *encoder_name;
 	const struct encoder_plugin *plugin;
 	struct encoder *encoder;
-	struct config_param *param;
 	static char buffer[32768];
 
 	/* parse command line */
@@ -74,10 +73,10 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	param = config_new_param(NULL, -1);
-	config_add_block_param(param, "quality", "5.0", -1);
+	config_param param;
+	config_add_block_param(&param, "quality", "5.0", -1);
 
-	encoder = encoder_init(plugin, param, &error);
+	encoder = encoder_init(plugin, &param, &error);
 	if (encoder == NULL) {
 		g_printerr("Failed to initialize encoder: %s\n",
 			   error->message);
diff --git a/test/test_vorbis_encoder.cxx b/test/test_vorbis_encoder.cxx
index 887fd1a77..aad48acde 100644
--- a/test/test_vorbis_encoder.cxx
+++ b/test/test_vorbis_encoder.cxx
@@ -53,10 +53,10 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
 	const struct encoder_plugin *plugin = encoder_plugin_get("vorbis");
 	assert(plugin != NULL);
 
-	struct config_param *param = config_new_param(NULL, -1);
-	config_add_block_param(param, "quality", "5.0", -1);
+	config_param param;
+	config_add_block_param(&param, "quality", "5.0", -1);
 
-	struct encoder *encoder = encoder_init(plugin, param, NULL);
+	struct encoder *encoder = encoder_init(plugin, &param, NULL);
 	assert(encoder != NULL);
 
 	/* open the encoder */