summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eina_mono/eina_error.cs
blob: c83728047e2056c6f677e26f682e6ef22722ecb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma warning disable 1591

using System;
using System.Runtime.InteropServices;

namespace eina {

public struct Error : IComparable<Error>
{
    int code;

    public string Message
    {
        get { return MsgGet(this); }
    }

    public static Error EFL_ERROR;

    public static Error NO_ERROR = new Error(0);
    public static Error EPERM = new Error(1);
    public static Error ENOENT = new Error(2);

    public Error(int value) { code = value; }
    static public implicit operator Error(int val)
    {
        return new Error(val);
    }
    static public implicit operator int(Error error)
    {
        return error.code;
    }
    public int CompareTo(Error err)
    {
        return code.CompareTo(err.code);
    }
    public override string ToString()
    {
        return "eina.Error(" + code + ")";
    }

    public static void Init()
    {
        if (eina_init() == 0)
            throw (new efl.EflException("Failed to init Eina"));

        EFL_ERROR = eina_error_msg_register("Managed Code Error");
    }

    [DllImport(efl.Libs.Eina)] private static extern int eina_init();
    [DllImport(efl.Libs.Eina)] static extern Error eina_error_msg_register(string msg);
    [DllImport(efl.Libs.Eina)] static extern Error eina_error_get();
    [DllImport(efl.Libs.Eina)] static extern void eina_error_set(Error error);
    [DllImport(efl.Libs.Eina)] static extern IntPtr eina_error_msg_get(Error error);

    public static void Set(Error error)
    {
        eina_error_set(error);
    }

    public static Error Get()
    {
        return eina_error_get();
    }

    public static String MsgGet(Error error)
    {
        IntPtr cstr = eina_error_msg_get(error);
        return eina.StringConversion.NativeUtf8ToManagedString(cstr);
    }

    public static void RaiseIfOccurred()
    {
        Error e = Get();
        Clear();
        Raise(e);
    }

    public static void Raise(Error e)
    {
        if (e != 0)
            throw (new efl.EflException(MsgGet(e)));
    }

    public static void Clear()
    {
        Set(0);
    }

    public static Error Register(string msg)
    {
        return eina_error_msg_register(msg);
    }
}
}