]> git.sesse.net Git - vlc/blob - bindings/cil/src/exception.cs
9a9ce5a2bacfa61ec51b483157b214d562021abb
[vlc] / bindings / cil / src / exception.cs
1 /**
2  * @file exception.cs
3  * @brief LibVLC exceptions
4  * @ingroup API
5  */
6
7 /**********************************************************************
8  *  Copyright (C) 2007-2009 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      * @brief VLCException: base class for LibVLC exceptions
31      * @ingroup API
32      */
33     public class VLCException : Exception
34     {
35         /**
36          * Creates a managed VLC exception.
37          */
38         public VLCException ()
39         {
40         }
41
42         /**
43          * Creates a managed VLC exception.
44          * @param message exception error message
45          */
46         public VLCException (string message)
47             : base (message)
48         {
49         }
50
51         /**
52          * Creates a managed VLC exception wrapping another exception.
53          * @param message exception error message
54          * @param inner inner wrapped exception
55          */
56         public VLCException (string message, Exception inner)
57            : base (message, inner)
58         {
59         }
60     };
61
62     /**
63      * @brief NativeException: CIL representation for libvlc_exception_t.
64      * @ingroup Internals
65      */
66     [StructLayout (LayoutKind.Sequential)]
67     public sealed class NativeException : IDisposable
68     {
69         int raised;
70         int code;
71         IntPtr message;
72
73         [DllImport ("libvlc.dll", EntryPoint="libvlc_exception_init")]
74         private static extern void Init (NativeException e);
75         [DllImport ("libvlc.dll", EntryPoint="libvlc_exception_clear")]
76         private static extern void Clear (NativeException e);
77         /*[DllImport ("libvlc.dll",
78                     EntryPoint="libvlc_exception_raised")]
79         private static extern int Raised (NativeException e);*/
80         [DllImport ("libvlc.dll",
81                     EntryPoint="libvlc_exception_get_message")]
82         private static extern IntPtr GetMessage (NativeException e);
83
84         public NativeException ()
85         {
86             Init (this);
87         }
88
89         /**
90          * Throws a managed exception if LibVLC has returned a native
91          * unmanaged exception. Clears the native exception.
92          */
93         public void Raise ()
94         {
95             try
96             {
97                 string msg = U8String.FromNative (GetMessage (this));
98                 if (msg != null)
99                     throw new VLCException (msg);
100             }
101             finally
102             {
103                 Clear (this);
104             }
105         }
106
107         /** IDisposable implementation. */
108         public void Dispose ()
109         {
110             Clear (this);
111         }
112     };
113 };