diff --git a/lib/mu-message-priority.cc b/lib/mu-message-priority.cc new file mode 100644 index 00000000..3358614f --- /dev/null +++ b/lib/mu-message-priority.cc @@ -0,0 +1,48 @@ +/* +** Copyright (C) 2022 Dirk-Jan C. Binnema +** +** This program is free software; you can redistribute it and/or modify it +** under the terms of the GNU General Public License as published by the +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + +#ifndef MU_MESSAGE_PRIORITY_CC__ +#define MU_MESSAGE_PRIORITY_CC__ + +#include "mu-message-priority.hh" + +using namespace Mu; + +std::string +to_string(MessagePriority prio) +{ + return std::string{message_priority_name(prio)}; +} + + +static_assert(to_char(MessagePriority::Low) == 'l'); +static_assert(to_char(MessagePriority::Normal) == 'n'); +static_assert(to_char(MessagePriority::High) == 'h'); + +static_assert(message_priority_from_char('l') == MessagePriority::Low); +static_assert(message_priority_from_char('n') == MessagePriority::Normal); +static_assert(message_priority_from_char('h') == MessagePriority::High); +static_assert(message_priority_from_char('x') == MessagePriority::Normal); + +static_assert(message_priority_name(MessagePriority::Low) == "low"); +static_assert(message_priority_name(MessagePriority::Normal) == "normal"); +static_assert(message_priority_name(MessagePriority::High) == "high"); + + +#endif /* MU_MESSAGE_PRIORITY_CC__ */ diff --git a/lib/mu-message-priority.hh b/lib/mu-message-priority.hh new file mode 100644 index 00000000..4c035642 --- /dev/null +++ b/lib/mu-message-priority.hh @@ -0,0 +1,128 @@ +/* +** Copyright (C) 2022 Dirk-Jan C. Binnema +** +** This program is free software; you can redistribute it and/or modify it +** under the terms of the GNU General Public License as published by the +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + +#ifndef MU_MESSAGE_PRIORITY_HH__ +#define MU_MESSAGE_PRIORITY_HH__ + +#include +#include +#include + +namespace Mu { +/** + * Message priorities + * + */ + +/** + * The priority ids + * + */ +enum struct MessagePriority : char { + Low = 'l', /**< Low priority */ + Normal = 'n', /**< Normal priority */ + High = 'h', /**< High priority */ +}; + +/** + * Sequence of all message priorities. + */ +static constexpr std::array AllMessagePriorities = { + MessagePriority::Low, MessagePriority::Normal, MessagePriority::High}; + +/** + * Get the char for some priority + * + * @param id an id + * + * @return the char + */ +constexpr char +to_char(MessagePriority prio) +{ + return static_cast(prio); +} + +/** + * Get the priority for some character; unknown onws + * become Normal. + * + * @param c some character + */ +constexpr MessagePriority +message_priority_from_char(char c) +{ + switch (c) { + case 'l': + return MessagePriority::Low; + case 'h': + return MessagePriority::High; + case 'n': + default: + return MessagePriority::Normal; + } +} + +/** + * Get the name for a given priority + * + * @return the name + */ +constexpr std::string_view +message_priority_name(MessagePriority prio) +{ + switch (prio) { + case MessagePriority::Low: + return "low"; + case MessagePriority::High: + return "high"; + case MessagePriority::Normal: + default: + return "normal"; + } +} + +/** + * Get the name for a given priority (backward compatibility) + * + * @return the name + */ +constexpr const char* +message_priority_name_c_str(MessagePriority prio) +{ + switch (prio) { + case MessagePriority::Low: return "low"; + case MessagePriority::High: return "high"; + case MessagePriority::Normal: + default: return "normal"; + } +} + +/** + * Get a the message priority as a string + * + * @param prio priority + * + * @return a string + */ +std::string to_string(MessagePriority prio); + +} // namespace Mu + +#endif /*MU_MESSAGE_PRIORITY_HH_*/ diff --git a/lib/mu-msg-prio.c b/lib/mu-msg-prio.c deleted file mode 100644 index 96a959ac..00000000 --- a/lib/mu-msg-prio.c +++ /dev/null @@ -1,65 +0,0 @@ -/* -** Copyright (C) 2012-2017 Dirk-Jan C. Binnema -** -** This program is free software; you can redistribute it and/or modify it -** under the terms of the GNU General Public License as published by the -** Free Software Foundation; either version 3, or (at your option) any -** later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software Foundation, -** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#include "mu-msg-prio.h" - - -const char* -mu_msg_prio_name (MuMsgPrio prio) -{ - switch (prio) { - case MU_MSG_PRIO_LOW : return "low"; - case MU_MSG_PRIO_NORMAL : return "normal"; - case MU_MSG_PRIO_HIGH : return "high"; - default : g_return_val_if_reached (NULL); - } -} - -MuMsgPrio -mu_msg_prio_from_char (char k) -{ - g_return_val_if_fail (k == 'l' || k == 'n' || k == 'h', - MU_MSG_PRIO_NONE); - return (MuMsgPrio)k; -} - -char -mu_msg_prio_char (MuMsgPrio prio) -{ - if (!(prio == 'l' || prio == 'n' || prio == 'h')) { - g_warning ("prio: %c", (char)prio); - } - - - g_return_val_if_fail (prio == 'l' || prio == 'n' || prio == 'h', - 0); - - return (char)prio; -} - - -void -mu_msg_prio_foreach (MuMsgPrioForeachFunc func, gpointer user_data) -{ - g_return_if_fail (func); - - func (MU_MSG_PRIO_LOW, user_data); - func (MU_MSG_PRIO_NORMAL, user_data); - func (MU_MSG_PRIO_HIGH, user_data); -} diff --git a/lib/mu-msg-prio.h b/lib/mu-msg-prio.h deleted file mode 100644 index df3e8c33..00000000 --- a/lib/mu-msg-prio.h +++ /dev/null @@ -1,84 +0,0 @@ -/* -** Copyright (C) 2008-2017 Dirk-Jan C. Binnema -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 3 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software Foundation, -** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#ifndef __MU_MSG_PRIO_H__ -#define __MU_MSG_PRIO_H__ - -#include - -G_BEGIN_DECLS - -enum _MuMsgPrio { - MU_MSG_PRIO_LOW = 'l', - MU_MSG_PRIO_NORMAL = 'n', - MU_MSG_PRIO_HIGH = 'h' -}; -typedef enum _MuMsgPrio MuMsgPrio; - -static const MuMsgPrio MU_MSG_PRIO_NONE = (MuMsgPrio)0; - - -/** - * get a printable name for the message priority - * (ie., MU_MSG_PRIO_LOW=>"low" etc.) - * - * @param prio a message priority - * - * @return a printable name for this priority - */ -const char* mu_msg_prio_name (MuMsgPrio prio) G_GNUC_CONST; - - -/** - * get the MuMsgPriority corresponding to a one-character shortcut - * ('l'=>MU_MSG_PRIO_, 'n'=>MU_MSG_PRIO_NORMAL or - * 'h'=>MU_MSG_PRIO_HIGH) - * - * @param k a character - * - * @return a message priority - */ -MuMsgPrio mu_msg_prio_from_char (char k) G_GNUC_CONST; - - -/** - * get the one-character shortcut corresponding to a message priority - * ('l'=>MU_MSG_PRIO_, 'n'=>MU_MSG_PRIO_NORMAL or - * 'h'=>MU_MSG_PRIO_HIGH) - * - * @param prio a message priority - * - * @return a shortcut character or 0 in case of error - */ -char mu_msg_prio_char (MuMsgPrio prio) G_GNUC_CONST; - -typedef void (*MuMsgPrioForeachFunc) (MuMsgPrio prio, gpointer user_data); -/** - * call a function for each message priority - * - * @param func a callback function - * @param user_data a user pointer to pass to the callback - */ -void mu_msg_prio_foreach (MuMsgPrioForeachFunc func, gpointer user_data); - - - -G_END_DECLS - -#endif /*__MU_MSG_PRIO_H__*/