]> git.sesse.net Git - vlc/blob - bindings/cil/src/exception.cs
0e76a242c14c4b675ee9f0c5b6733f0bd6f6c02b
[vlc] / bindings / cil / src / exception.cs
1 /*
2  * libvlc.cs - libvlc CIL bindings
3  *
4  * $Id$
5  */
6
7 /**********************************************************************
8  *  Copyright (C) 2007 RĂ©mi Denis-Courmont.                           *
9  *  This program is free software; you can redistribute and/or modify *
10  *  it under the terms of the GNU General Public License as published *
11  *  by the Free Software Foundation; version 2 of the license, or (at *
12  *  your option) any later version.                                   *
13  *                                                                    *
14  *  This program is distributed in the hope that it will be useful,   *
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of    *
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
17  *  See the GNU General Public License for more details.              *
18  *                                                                    *
19  *  You should have received a copy of the GNU General Public License *
20  *  along with this program; if not, you can get it from:             *
21  *  http://www.gnu.org/copyleft/gpl.html                              *
22  **********************************************************************/
23
24 using System;
25 using System.Runtime.InteropServices;
26
27 namespace VideoLAN.LibVLC
28 {
29     /**
30      * VLCException: managed base class for LibVLC exceptions
31      */
32     public class VLCException : Exception
33     {
34         /**
35          * Creates a managed VLC exception.
36          */
37         public VLCException ()
38         {
39         }
40
41         /**
42          * Creates a managed VLC exception.
43          * @param message exception error message
44          */
45         public VLCException (string message)
46             : base (message)
47         {
48         }
49
50         /**
51          * Creates a managed VLC exception wrapping another exception.
52          * @param message exception error message
53          * @param inner inner wrapped exception
54          */
55         public VLCException (string message, Exception inner)
56            : base (message, inner)
57         {
58         }
59     };
60
61     /**
62      * libvlc_exception_t: structure for unmanaged LibVLC exceptions
63      */
64     [StructLayout (LayoutKind.Sequential)]
65     public sealed class NativeException : IDisposable
66     {
67         int raised;
68         int code;
69         IntPtr message;
70
71         [DllImport ("libvlc.dll", EntryPoint="libvlc_exception_init")]
72         static extern void Init (NativeException e);
73         [DllImport ("libvlc.dll", EntryPoint="libvlc_exception_clear")]
74         static extern void Clear (NativeException e);
75         /*[DllImport ("libvlc.dll",
76                     EntryPoint="libvlc_exception_raised")]
77         static extern int Raised (NativeException e);*/
78         [DllImport ("libvlc.dll",
79                     EntryPoint="libvlc_exception_get_message")]
80         static extern IntPtr GetMessage (NativeException e);
81
82         public NativeException ()
83         {
84             Init (this);
85         }
86
87         /**
88          * Throws a managed exception if LibVLC has returned a native
89          * unmanaged exception. Clears the native exception.
90          */
91         public void Raise ()
92         {
93             try
94             {
95                 string msg = U8String.FromNative (GetMessage (this));
96                 if (msg != null)
97                     throw new VLCException (msg);
98             }
99             finally
100             {
101                 Clear (this);
102             }
103         }
104
105         /** IDisposable implementation. */
106         public void Dispose ()
107         {
108             Clear (this);
109         }
110     };
111 };