~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/ObjcHelper.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ObjcHelper.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using MonoMac.ObjCRuntime;
 
29
using System.Runtime.InteropServices;
 
30
 
 
31
namespace Xwt.Mac
 
32
{
 
33
        public static class ObjcHelper
 
34
        {
 
35
                [DllImport ("/usr/lib/libobjc.dylib")]
 
36
                static extern bool class_addMethod (IntPtr cls, IntPtr sel, Delegate method, string argTypes);
 
37
                
 
38
                [DllImport ("/usr/lib/libobjc.dylib")]
 
39
                static extern IntPtr objc_getMetaClass (string name);
 
40
                
 
41
                [DllImport ("/usr/lib/libobjc.dylib")]
 
42
                static extern IntPtr class_getInstanceMethod (IntPtr cls, IntPtr sel);
 
43
                
 
44
                [DllImport ("/usr/lib/libobjc.dylib")]
 
45
                static extern IntPtr class_getClassMethod (IntPtr cls, IntPtr sel);
 
46
                
 
47
                [System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
 
48
                static extern bool method_exchangeImplementations (IntPtr m1, IntPtr m2);
 
49
                
 
50
                [System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
 
51
                static extern IntPtr method_setImplementations (IntPtr m1, Delegate impl);
 
52
                
 
53
                [System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
 
54
                static extern IntPtr object_getClass (IntPtr obj);
 
55
                
 
56
                [System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
 
57
                static extern IntPtr objc_getProtocol (string name);
 
58
                
 
59
                [System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
 
60
                static extern bool class_conformsToProtocol(IntPtr cls, IntPtr protocol);
 
61
                
 
62
                [System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
 
63
                static extern bool class_addProtocol(IntPtr cls, IntPtr protocol);
 
64
                
 
65
                public static Class GetObjectClass (IntPtr obj)
 
66
                {
 
67
                        return new Class (object_getClass (obj));
 
68
                }
 
69
                
 
70
                public static Class GetMetaClass (string name)
 
71
                {
 
72
                        return new Class (objc_getMetaClass (name));
 
73
                }
 
74
                
 
75
                public static void InstanceMethodExchange (this Class cls, IntPtr sel1, IntPtr sel2)
 
76
                {
 
77
                        IntPtr m1 = class_getInstanceMethod (cls.Handle, sel1);
 
78
                        if (m1 == IntPtr.Zero)
 
79
                                throw new Exception ("Class did not have a method for the first selector");
 
80
                        IntPtr m2 = class_getInstanceMethod (cls.Handle, sel2);
 
81
                        if (m2 == IntPtr.Zero)
 
82
                                throw new Exception ("Class did not have a method for the second selector");
 
83
                        if (!method_exchangeImplementations (m1, m2))
 
84
                                throw new Exception ("Failed to exchange implementations");
 
85
                }
 
86
                
 
87
                public static void MethodExchange (this Class cls, IntPtr sel1, IntPtr sel2)
 
88
                {
 
89
                        IntPtr m1 = class_getClassMethod (cls.Handle, sel1);
 
90
                        if (m1 == IntPtr.Zero)
 
91
                                throw new Exception ("Class did not have a method for the first selector");
 
92
                        IntPtr m2 = cls.GetMethod (sel2);
 
93
                        if (m2 == IntPtr.Zero)
 
94
                                throw new Exception ("Class did not have a method for the second selector");
 
95
                        if (!method_exchangeImplementations (m1, m2))
 
96
                                throw new Exception ("Failed to exchange implementations");
 
97
                }
 
98
                
 
99
                public static IntPtr SetMethodImplementation (this Class cls, IntPtr method, Delegate impl)
 
100
                {
 
101
                        IntPtr m1 = class_getClassMethod (cls.Handle, method);
 
102
                        if (m1 == IntPtr.Zero)
 
103
                                throw new Exception ("Class did not have a method for the first selector");
 
104
                        return method_setImplementations (m1, impl);
 
105
                }
 
106
                
 
107
                public static IntPtr GetMethod (this Class cls, IntPtr sel)
 
108
                {
 
109
                        return class_getClassMethod (cls.Handle, sel);
 
110
                }
 
111
 
 
112
                public static bool AddMethod (this Class cls, IntPtr sel, Delegate method, string argTypes)
 
113
                {
 
114
                        return class_addMethod (cls.Handle, sel, method, argTypes);
 
115
                }
 
116
                
 
117
                public static IntPtr GetProtocol (string name)
 
118
                {
 
119
                        return objc_getProtocol (name);
 
120
                }
 
121
                
 
122
                public static bool ConformsToProtocol(this Class cls, IntPtr protocol)
 
123
                {
 
124
                        return class_conformsToProtocol (cls.Handle, protocol);
 
125
                }
 
126
                
 
127
                public static bool AddProtocol(this Class cls, IntPtr protocol)
 
128
                {
 
129
                        return class_addProtocol (cls.Handle, protocol);
 
130
                }
 
131
        }
 
132
}
 
133