lib: replace MuMsgPrio with Mu::MessagePriority

Rework good-old MuMsgPrio into Mu::MessagePriority, which looks a bit more like
modern C++.
This commit is contained in:
Dirk-Jan C. Binnema 2022-02-13 14:23:09 +02:00
parent 5fbebbff86
commit 13bcc6eb5d
4 changed files with 176 additions and 149 deletions

View File

@ -0,0 +1,48 @@
/*
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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__ */

128
lib/mu-message-priority.hh Normal file
View File

@ -0,0 +1,128 @@
/*
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 <array>
#include <string>
#include <string_view>
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<MessagePriority, 3> 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<char>(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_*/

View File

@ -1,65 +0,0 @@
/*
** Copyright (C) 2012-2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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);
}

View File

@ -1,84 +0,0 @@
/*
** Copyright (C) 2008-2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 <glib.h>
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__*/