]> git.sesse.net Git - vlc/blob - bindings/cil/src/marshal.cs
Version informations
[vlc] / bindings / cil / src / marshal.cs
1 /**
2  * @file marshal.cs
3  * @brief Common LibVLC objects marshalling utilities
4  * @ingroup Internals
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 NonNullHandle: abstract safe handle class for non-NULL pointers
31      * @ingroup Internals
32      * Microsoft.* namespace has a similar class. However we want to use the
33      * System.* namespace only.
34      */
35     internal abstract class NonNullHandle : SafeHandle
36     {
37         protected NonNullHandle ()
38             : base (IntPtr.Zero, true)
39         {
40         }
41
42         /**
43          * System.Runtime.InteropServices.SafeHandle::IsInvalid.
44          */
45         public override bool IsInvalid
46         {
47             get
48             {
49                 return handle == IntPtr.Zero;
50             }
51         }
52
53         /**
54          * Destroys an handle. Cannot fail.
55          */
56         protected abstract void Destroy ();
57
58         /**
59          * System.Runtime.InteropServices.SafeHandle::ReleaseHandle.
60          */
61         protected override bool ReleaseHandle ()
62         {
63             Destroy ();
64             return true;
65         }
66
67     };
68
69     /**
70      * @brief BaseObject: generic wrapper around a safe LibVLC handle.
71      * @ingroup Internals
72      *
73      * This is the baseline for all managed LibVLC objects. It wraps:
74      *  - an unmanaged LibVLC pointer,
75      *  - a native exception structure, and
76      *  - the object's native event manager.
77      */
78     public class BaseObject : IDisposable
79     {
80         internal NativeException ex; /**< buffer for LibVLC exceptions */
81         internal SafeHandle handle; /**< wrapped safe handle */
82
83         internal BaseObject ()
84         {
85             ex = new NativeException ();
86             this.handle = null;
87         }
88
89         /**
90          * Checks if the LibVLC run-time raised an exception
91          * If so, raises a CIL exception.
92          */
93         internal void Raise ()
94         {
95             ex.Raise ();
96         }
97
98         /**
99          * IDisposable::Dispose.
100          */
101         public void Dispose ()
102         {
103             Dispose (true);
104             GC.SuppressFinalize (this);
105         }
106
107         /**
108          * Releases unmanaged resources associated with the object.
109          * @param disposing true if the disposing the object explicitly,
110          *                  false if finalizing the object inside the GC.
111          */
112         protected virtual void Dispose (bool disposing)
113         {
114             if (disposing)
115             {
116                 ex.Dispose ();
117                 if (handle != null)
118                     handle.Close ();
119             }
120             ex = null;
121             handle = null;
122         }
123     };
124 };