]> git.sesse.net Git - vlc/blob - bindings/cil/src/exception.cs
Provide the exception code, handle lack of message.
[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         int code;
36         /**
37          * VLC exception code.
38          */
39         public int Code
40         {
41             get
42             {
43                 return code;
44             }
45         }
46
47         /**
48          * Creates a managed VLC exception.
49          */
50         public VLCException ()
51         {
52         }
53
54         /**
55          * Creates a managed VLC exception.
56          * @param message exception error message
57          */
58         public VLCException (string message)
59             : base (message)
60         {
61         }
62
63         /**
64          * Creates a managed VLC exception wrapping another exception.
65          * @param message exception error message
66          * @param inner inner wrapped exception
67          */
68         public VLCException (string message, Exception inner)
69            : base (message, inner)
70         {
71         }
72
73         /**
74          * Creates a VLC exception
75          * @param code VLC exception code
76          * @param message VLC exception message
77          */
78         public VLCException (int code, string message) : base (message)
79         {
80             this.code = code;
81         }
82
83         /**
84          * Creates a VLC exception
85          * @param code VLC exception code
86          */
87         public VLCException (int code) : base ()
88         {
89             this.code = code;
90         }
91     };
92
93     /**
94      * @brief NativeException: CIL representation for libvlc_exception_t.
95      * @ingroup Internals
96      */
97     [StructLayout (LayoutKind.Sequential)]
98     public sealed class NativeException : IDisposable
99     {
100         int raised;
101         int code;
102         IntPtr message;
103
104         [DllImport ("libvlc.dll", EntryPoint="libvlc_exception_init")]
105         private static extern void Init (NativeException e);
106         [DllImport ("libvlc.dll", EntryPoint="libvlc_exception_clear")]
107         private static extern void Clear (NativeException e);
108         /*[DllImport ("libvlc.dll",
109                     EntryPoint="libvlc_exception_raised")]
110         private static extern int Raised (NativeException e);*/
111         [DllImport ("libvlc.dll",
112                     EntryPoint="libvlc_exception_get_message")]
113         private static extern IntPtr GetMessage (NativeException e);
114
115         public NativeException ()
116         {
117             Init (this);
118         }
119
120         /**
121          * Throws a managed exception if LibVLC has returned a native
122          * unmanaged exception. Clears the native exception.
123          */
124         public void Raise ()
125         {
126             if (raised == 0)
127                 return;
128
129             string msg = U8String.FromNative (message);
130             try
131             {
132                 if (msg != null)
133                     throw new VLCException (code, msg);
134                 else
135                     throw new VLCException (code);
136             }
137             finally
138             {
139                 Clear (this);
140             }
141         }
142
143         /** IDisposable implementation. */
144         public void Dispose ()
145         {
146             Clear (this);
147         }
148     };
149 };