Skip to content

Commit

Permalink
Merge pull request #982 from gircore/gvalue-support-enum
Browse files Browse the repository at this point in the history
GValue: Support setting an enumeration / bitfield
  • Loading branch information
badcel authored Nov 27, 2023
2 parents aec2125 + a1d0b02 commit c918bf6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Libs/GObject-2.0/Public/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public void Set(object? value)
SetDouble(d);
break;
case Enum e:
SetEnum(e);
if (e.GetType().IsDefined(typeof(FlagsAttribute), false))
SetFlags(e);
else
SetEnum(e);
break;
case long l:
SetLong(l);
Expand Down Expand Up @@ -225,6 +228,7 @@ public ParamSpec GetParam()
private void SetFloat(float f) => Internal.Value.SetFloat(Handle, f);
private void SetLong(long l) => Internal.Value.SetLong(Handle, l);
private void SetEnum(Enum e) => Internal.Value.SetEnum(Handle, Convert.ToInt32(e));
private void SetFlags(Enum e) => Internal.Value.SetFlags(Handle, Convert.ToUInt32(e));
private void SetString(string s) => Internal.Value.SetString(Handle, GLib.Internal.NullableUtf8StringOwnedHandle.Create(s));
private void SetVariant(GLib.Variant v) => Internal.Value.SetVariant(Handle, v.Handle);
private void SetObject(Object o) => Internal.Value.SetObject(Handle, o.Handle);
Expand Down
24 changes: 24 additions & 0 deletions src/Native/GirTestLib/girtest-enum-tester.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "girtest-enum-tester.h"

/**
* GirTestEnumTester:
*
* Contains functions for testing enumerations.
*/

struct _GirTestEnumTester
{
GObject parent_instance;
};

G_DEFINE_TYPE(GirTestEnumTester, girtest_enum_tester, G_TYPE_OBJECT)

static void
girtest_enum_tester_init(GirTestEnumTester *value)
{
}

static void
girtest_enum_tester_class_init(GirTestEnumTesterClass *class)
{
}
25 changes: 25 additions & 0 deletions src/Native/GirTestLib/girtest-enum-tester.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <glib-object.h>

G_BEGIN_DECLS

/**
* EnumTesterSimpleEnum:
* @A: 1
* @B: 2
* @C: 3
*
* Enum to test bindings
*/
typedef enum {
SIMPLE_ENUM_A = 1,
SIMPLE_ENUM_B = 2,
SIMPLE_ENUM_C = 3
} GirTestEnumTesterSimpleEnum;

#define GIRTEST_TYPE_ENUM_TESTER girtest_enum_tester_get_type()

G_DECLARE_FINAL_TYPE(GirTestEnumTester, girtest_enum_tester, GIRTEST, ENUM_TESTER, GObject)

G_END_DECLS
1 change: 1 addition & 0 deletions src/Native/GirTestLib/girtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "girtest-callback-tester.h"
#include "girtest-class-tester.h"
#include "girtest-constant-tester.h"
#include "girtest-enum-tester.h"
#include "girtest-error-tester.h"
#include "girtest-integer-array-tester.h"
#include "girtest-opaque-typed-record-tester.h"
Expand Down
2 changes: 2 additions & 0 deletions src/Native/GirTestLib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ header_files = [
'girtest-callback-tester.h',
'girtest-class-tester.h',
'girtest-constant-tester.h',
'girtest-enum-tester.h',
'girtest-error-tester.h',
'girtest-integer-array-tester.h',
'girtest-opaque-typed-record-tester.h',
Expand All @@ -33,6 +34,7 @@ source_files = [
'girtest-byte-array-tester.c',
'girtest-callback-tester.c',
'girtest-class-tester.c',
'girtest-enum-tester.c',
'girtest-error-tester.c',
'girtest-integer-array-tester.c',
'girtest-opaque-typed-record-tester.c',
Expand Down
11 changes: 11 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/BitfieldTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using GObject;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GirTest.Tests;
Expand All @@ -14,4 +15,14 @@ public void SupportsPointedBitfields()

flags.Should().Be(BitfieldTesterSimpleFlags.Zero);
}

[TestMethod]
public void CanBeUsedInGValue()
{
var flags = BitfieldTesterSimpleFlags.One | BitfieldTesterSimpleFlags.Two;
var value = new Value(Type.Flags);
value.Set(flags);
var result = value.Extract<BitfieldTesterSimpleFlags>();
result.Should().Be(flags);
}
}
19 changes: 19 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/EnumerationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FluentAssertions;
using GObject;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GirTest.Tests;

[TestClass, TestCategory("BindingTest")]
public class EnumerationTest : Test
{
[TestMethod]
public void CanBeUsedInGValue()
{
var e = EnumTesterSimpleEnum.A;
var value = new Value(Type.Enum);
value.Set(e);
var result = value.Extract<EnumTesterSimpleEnum>();
result.Should().Be(e);
}
}

0 comments on commit c918bf6

Please sign in to comment.