Port _g_enum_nick_to_value() to C

This commit is contained in:
Gergely Polonkai 2017-11-03 07:19:29 +01:00
parent 091bb38286
commit 86147377d0
11 changed files with 85 additions and 55 deletions

View File

@ -42,12 +42,10 @@ public class Matrix.Event.CallAnswer : Matrix.Event.Call {
var answer_root = node.get_object(); var answer_root = node.get_object();
if ((node = answer_root.get_member("type")) != null) { if ((node = answer_root.get_member("type")) != null) {
CallAnswerType? typ = (CallAnswerType?)_g_enum_nick_to_value( try {
typeof(CallAnswerType), node.get_string()); _answer_type = (CallAnswerType)_g_enum_nick_to_value(
typeof(CallAnswerType), node.get_string());
if (typ != null) { } catch (Matrix.Error e) {
_answer_type = typ;
} else {
_answer_type = CallAnswerType.UNKNOWN; _answer_type = CallAnswerType.UNKNOWN;
if (Config.DEBUG) { if (Config.DEBUG) {

View File

@ -50,12 +50,10 @@ public class Matrix.Event.CallInvite : Matrix.Event.Call {
var offer_node = node.get_object(); var offer_node = node.get_object();
if ((node = offer_node.get_member("type")) != null) { if ((node = offer_node.get_member("type")) != null) {
CallOfferType? typ = (CallOfferType?)_g_enum_nick_to_value( try {
typeof(CallOfferType), node.get_string()); _offer_type = (CallOfferType)_g_enum_nick_to_value(
typeof(CallOfferType), node.get_string());
if (typ != null) { } catch (Matrix.Error e) {
_offer_type = typ;
} else {
_offer_type = CallOfferType.UNKNOWN; _offer_type = CallOfferType.UNKNOWN;
if (Config.DEBUG) { if (Config.DEBUG) {

View File

@ -95,12 +95,10 @@ public class Matrix.Event.Presence : Matrix.Event.Base {
} }
if ((node = content_root.get_member("presence")) != null) { if ((node = content_root.get_member("presence")) != null) {
Matrix.Presence? pres = (Matrix.Presence?)_g_enum_nick_to_value( try {
typeof(Matrix.Presence), node.get_string()); _presence = (Matrix.Presence)_g_enum_nick_to_value(
typeof(Matrix.Presence), node.get_string());
if (pres != null) { } catch (Matrix.Error e) {
_presence = pres;
} else {
_presence = Matrix.Presence.UNKNOWN; _presence = Matrix.Presence.UNKNOWN;
if (Config.DEBUG) { if (Config.DEBUG) {

View File

@ -48,12 +48,10 @@ public class Matrix.Event.RoomGuestAccess : Matrix.Event.State {
} }
if ((node = content_root.get_member("guest_access")) != null) { if ((node = content_root.get_member("guest_access")) != null) {
GuestAccess? rules = (GuestAccess?)_g_enum_nick_to_value( try {
typeof(GuestAccess), node.get_string()); _guest_access = (GuestAccess)_g_enum_nick_to_value(
typeof(GuestAccess), node.get_string());
if (rules != null) { } catch (Matrix.Error e) {
_guest_access = rules;
} else {
_guest_access = GuestAccess.UNKNOWN; _guest_access = GuestAccess.UNKNOWN;
if (Config.DEBUG) { if (Config.DEBUG) {

View File

@ -47,13 +47,10 @@ public class Matrix.Event.RoomHistoryVisibility : Matrix.Event.State {
} }
if ((node = content_root.get_member("history_visibility")) != null) { if ((node = content_root.get_member("history_visibility")) != null) {
Matrix.HistoryVisibility? vis = (Matrix.HistoryVisibility?)_g_enum_nick_to_value( try {
typeof(Matrix.HistoryVisibility), _visibility = (Matrix.HistoryVisibility)_g_enum_nick_to_value(
node.get_string()); typeof(Matrix.HistoryVisibility), node.get_string());
} catch (Matrix.Error e) {
if (vis != null) {
_visibility = vis;
} else {
_visibility = Matrix.HistoryVisibility.UNKNOWN; _visibility = Matrix.HistoryVisibility.UNKNOWN;
if (Config.DEBUG) { if (Config.DEBUG) {

View File

@ -43,12 +43,10 @@ public class Matrix.Event.RoomJoinRules : Matrix.Event.State {
} }
if ((node = content_root.get_member("join_rule")) != null) { if ((node = content_root.get_member("join_rule")) != null) {
Matrix.JoinRules? rules = (Matrix.JoinRules)_g_enum_nick_to_value( try {
typeof(Matrix.JoinRules), node.get_string()); _join_rules = (JoinRules)_g_enum_nick_to_value(
typeof(JoinRules), node.get_string());
if (rules != null) { } catch (Matrix.Error e) {
_join_rules = rules;
} else {
_join_rules = Matrix.JoinRules.UNKNOWN; _join_rules = Matrix.JoinRules.UNKNOWN;
if (Config.DEBUG) { if (Config.DEBUG) {

View File

@ -138,12 +138,15 @@ public class Matrix.Event.RoomMember : Matrix.Event.State {
} }
if ((node = content_root.get_member("membership")) != null) { if ((node = content_root.get_member("membership")) != null) {
Matrix.RoomMembership? mship = (Matrix.RoomMembership?)_g_enum_nick_to_value( try {
typeof(Matrix.RoomMembership), _membership = (Matrix.RoomMembership)_g_enum_nick_to_value(
node.get_string()); typeof(Matrix.RoomMembership), node.get_string());
} catch (Matrix.Error e) {
_membership = Matrix.RoomMembership.UNKNOWN;
if (mship != null) { if (Config.DEBUG) {
_membership = mship; warning("Unknown membership value %s", node.get_string());
}
} }
} else if (Config.DEBUG) { } else if (Config.DEBUG) {
warning("membership key is missing from the m.room.member event"); warning("membership key is missing from the m.room.member event");

View File

@ -295,17 +295,4 @@ namespace Matrix {
return node; return node;
} }
} }
private int?
_g_enum_nick_to_value(Type enum_type, string nick)
{
EnumClass enum_class = (EnumClass)enum_type.class_ref();
unowned EnumValue? enum_val = enum_class.get_value_by_nick(nick);
if (enum_val != null) {
return enum_val.value;
} else {
return null;
}
}
} }

View File

@ -17,6 +17,7 @@
*/ */
#include "utils.h" #include "utils.h"
#include "matrix-types.h"
// GTK-Doc looking comments in this file intentionally do not begin with a double star, as the // GTK-Doc looking comments in this file intentionally do not begin with a double star, as the
// functions here are internal // functions here are internal
@ -58,3 +59,50 @@ _matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes)
return nick; return nick;
} }
/*
* _matrix_g_enum_nick_to_value:
*
* @enum_type: a #GEnumType
* @nick: a value nick registered in @enum_type
* @error: a #GError, or NULL to ignore errors
*
* Get the integer value of the enum nick @nick from @enum_type. If the nick contains underscores
* (`_`), they will be converted to dashes (`-`) first.
*
* If @nick cannot be found in @enum_type, this function returns NULL, and sets @error to
* #MATRIX_ERROR_UNKNOWN_VALUE.
*
* Returns: the integer value of @nick, or 0 if not found
*/
gint
_matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error)
{
GEnumClass *enum_class = g_type_class_ref(enum_type);
GEnumValue *enum_value;
gchar *nick_c = NULL;
gchar *a;
gint ret = 0;
nick_c = g_strdup(nick);
for (a = nick_c; *a; a++) {
if (*a == '_') {
*a = '-';
}
}
enum_value = g_enum_get_value_by_nick(enum_class, nick_c);
g_free(nick_c);
if (enum_value) {
ret = enum_value->value;
} else {
g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_UNKNOWN_VALUE,
"Value %s is unknown", nick);
}
g_type_class_unref(enum_class);
return ret;
}

View File

@ -24,6 +24,7 @@
G_BEGIN_DECLS G_BEGIN_DECLS
gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes); gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes);
gint _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error);
G_END_DECLS G_END_DECLS

View File

@ -208,6 +208,10 @@ namespace Matrix {
[CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")] [CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")]
public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, bool convert_dashes = true); public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, bool convert_dashes = true);
[CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_nick_to_value")]
public int _g_enum_nick_to_value(GLib.Type enum_type, string nick)
throws Matrix.Error;
/** /**
* The major version number of the Matrix.org GLib SDK. * The major version number of the Matrix.org GLib SDK.
*/ */