aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-03 17:58:22 +0100
committerMax Kellermann <max@duempel.org>2013-01-03 17:58:22 +0100
commit5155b0f6087b5929838f6594a719011e3e704244 (patch)
treee924eff40e0330d4a4129213f32dc6f03dadcd16 /src
parentd919f8d50ab32a94b8965330297985cd14c45a42 (diff)
downloadmpd-5155b0f6087b5929838f6594a719011e3e704244.tar.gz
mpd-5155b0f6087b5929838f6594a719011e3e704244.tar.xz
mpd-5155b0f6087b5929838f6594a719011e3e704244.zip
ClientMessage: turn into a class
Diffstat (limited to 'src')
-rw-r--r--src/ClientMessage.cxx54
-rw-r--r--src/ClientMessage.hxx55
-rw-r--r--src/ClientSubscribe.cxx9
-rw-r--r--src/ClientSubscribe.hxx4
-rw-r--r--src/MessageCommands.cxx23
5 files changed, 35 insertions, 110 deletions
diff --git a/src/ClientMessage.cxx b/src/ClientMessage.cxx
index c15ea0802..6fbcf3371 100644
--- a/src/ClientMessage.cxx
+++ b/src/ClientMessage.cxx
@@ -40,57 +40,3 @@ client_message_valid_channel_name(const char *name)
return true;
}
-
-void
-client_message_init_null(struct client_message *msg)
-{
- assert(msg != NULL);
-
- msg->channel = NULL;
- msg->message = NULL;
-}
-
-void
-client_message_init(struct client_message *msg,
- const char *channel, const char *message)
-{
- assert(msg != NULL);
-
- msg->channel = g_strdup(channel);
- msg->message = g_strdup(message);
-}
-
-void
-client_message_copy(struct client_message *dest,
- const struct client_message *src)
-{
- assert(dest != NULL);
- assert(src != NULL);
- assert(client_message_defined(src));
-
- client_message_init(dest, src->channel, src->message);
-}
-
-struct client_message *
-client_message_dup(const struct client_message *src)
-{
- struct client_message *dest = g_slice_new(struct client_message);
- client_message_copy(dest, src);
- return dest;
-}
-
-void
-client_message_deinit(struct client_message *msg)
-{
- assert(msg != NULL);
-
- g_free(msg->channel);
- g_free(msg->message);
-}
-
-void
-client_message_free(struct client_message *msg)
-{
- client_message_deinit(msg);
- g_slice_free(struct client_message, msg);
-}
diff --git a/src/ClientMessage.hxx b/src/ClientMessage.hxx
index 6d1e734be..2a929d445 100644
--- a/src/ClientMessage.hxx
+++ b/src/ClientMessage.hxx
@@ -22,52 +22,31 @@
#include "gcc.h"
-#include <assert.h>
-#include <stdbool.h>
-#include <stddef.h>
+#include <string>
/**
* A client-to-client message.
*/
-struct client_message {
- char *channel;
-
- char *message;
+class ClientMessage {
+ std::string channel, message;
+
+public:
+ template<typename T, typename U>
+ ClientMessage(T &&_channel, U &&_message)
+ :channel(std::forward<T>(_channel)),
+ message(std::forward<U>(_message)) {}
+
+ const char *GetChannel() const {
+ return channel.c_str();
+ }
+
+ const char *GetMessage() const {
+ return message.c_str();
+ }
};
gcc_pure
bool
client_message_valid_channel_name(const char *name);
-gcc_pure
-static inline bool
-client_message_defined(const struct client_message *msg)
-{
- assert(msg != NULL);
- assert((msg->channel == NULL) == (msg->message == NULL));
-
- return msg->channel != NULL;
-}
-
-void
-client_message_init_null(struct client_message *msg);
-
-void
-client_message_init(struct client_message *msg,
- const char *channel, const char *message);
-
-void
-client_message_copy(struct client_message *dest,
- const struct client_message *src);
-
-gcc_malloc
-struct client_message *
-client_message_dup(const struct client_message *src);
-
-void
-client_message_deinit(struct client_message *msg);
-
-void
-client_message_free(struct client_message *msg);
-
#endif
diff --git a/src/ClientSubscribe.cxx b/src/ClientSubscribe.cxx
index 3f4714958..4f1471474 100644
--- a/src/ClientSubscribe.cxx
+++ b/src/ClientSubscribe.cxx
@@ -25,6 +25,7 @@ extern "C" {
#include "idle.h"
}
+#include <assert.h>
#include <string.h>
enum client_subscribe_result
@@ -78,21 +79,19 @@ client_unsubscribe_all(Client *client)
}
bool
-client_push_message(Client *client, const struct client_message *msg)
+client_push_message(Client *client, const ClientMessage &msg)
{
assert(client != NULL);
- assert(msg != NULL);
- assert(client_message_defined(msg));
if (client->num_messages >= CLIENT_MAX_MESSAGES ||
- !client->IsSubscribed(msg->channel))
+ !client->IsSubscribed(msg.GetChannel()))
return false;
if (client->messages == NULL)
client_idle_add(client, IDLE_MESSAGE);
client->messages = g_slist_prepend(client->messages,
- client_message_dup(msg));
+ new ClientMessage(msg));
++client->num_messages;
return true;
diff --git a/src/ClientSubscribe.hxx b/src/ClientSubscribe.hxx
index 9b62374a9..42c8ed8fd 100644
--- a/src/ClientSubscribe.hxx
+++ b/src/ClientSubscribe.hxx
@@ -24,7 +24,7 @@
typedef struct _GSList GSList;
class Client;
-struct client_message;
+class ClientMessage;
enum client_subscribe_result {
/** success */
@@ -50,7 +50,7 @@ void
client_unsubscribe_all(Client *client);
bool
-client_push_message(Client *client, const struct client_message *msg);
+client_push_message(Client *client, const ClientMessage &msg);
gcc_malloc
GSList *
diff --git a/src/MessageCommands.cxx b/src/MessageCommands.cxx
index 6ac5912e7..738c2eab9 100644
--- a/src/MessageCommands.cxx
+++ b/src/MessageCommands.cxx
@@ -112,11 +112,12 @@ handle_read_messages(Client *client,
GSList *messages = client_read_messages(client);
for (GSList *i = messages; i != NULL; i = g_slist_next(i)) {
- struct client_message *msg = (struct client_message *)i->data;
+ ClientMessage *msg = (ClientMessage *)i->data;
client_printf(client, "channel: %s\nmessage: %s\n",
- msg->channel, msg->message);
- client_message_free(msg);
+ msg->GetChannel(),
+ msg->GetMessage());
+ delete msg;
}
g_slist_free(messages);
@@ -125,9 +126,14 @@ handle_read_messages(Client *client,
}
struct send_message_context {
- struct client_message msg;
+ ClientMessage msg;
bool sent;
+
+ template<typename T, typename U>
+ send_message_context(T &&_channel, U &&_message)
+ :msg(std::forward<T>(_channel), std::forward<U>(_message)),
+ sent(false) {}
};
static void
@@ -137,7 +143,7 @@ send_message(gpointer data, gpointer user_data)
(struct send_message_context *)user_data;
Client *client = (Client *)data;
- if (client_push_message(client, &context->msg))
+ if (client_push_message(client, context->msg))
context->sent = true;
}
@@ -153,15 +159,10 @@ handle_send_message(Client *client,
return COMMAND_RETURN_ERROR;
}
- struct send_message_context context;
- context.sent = false;
-
- client_message_init(&context.msg, argv[1], argv[2]);
+ struct send_message_context context(argv[1], argv[2]);
client_list_foreach(send_message, &context);
- client_message_deinit(&context.msg);
-
if (context.sent)
return COMMAND_RETURN_OK;
else {