Opentk/Source/OpenTK/Platform/MacOS/Cocoa/Class.cs

63 lines
2 KiB
C#
Raw Normal View History

2014-04-18 20:30:50 +00:00
using System.Runtime.InteropServices;
using System;
2014-04-21 17:40:44 +00:00
using System.Collections.Generic;
2014-04-18 20:30:50 +00:00
namespace OpenTK.Platform.MacOS
{
static class Class
{
2014-04-19 10:17:14 +00:00
[DllImport (Cocoa.LibObjC)]
extern static IntPtr class_getName(IntPtr handle);
[DllImport (Cocoa.LibObjC)]
extern static bool class_addMethod(IntPtr classHandle, IntPtr selector, IntPtr method, string types);
2014-04-18 20:30:50 +00:00
[DllImport (Cocoa.LibObjC)]
extern static IntPtr objc_getClass(string name);
2014-04-19 10:17:14 +00:00
[DllImport (Cocoa.LibObjC)]
extern static IntPtr objc_allocateClassPair(IntPtr parentClass, string name, int extraBytes);
[DllImport (Cocoa.LibObjC)]
extern static void objc_registerClassPair(IntPtr classToRegister);
2014-04-18 20:30:50 +00:00
public static IntPtr Get(string name)
{
var id = objc_getClass(name);
if (id == IntPtr.Zero)
{
throw new ArgumentException("Unknown class: " + name);
}
return id;
}
2014-04-19 10:17:14 +00:00
public static IntPtr AllocateClass(string className, string parentClass)
{
return objc_allocateClassPair(Get(parentClass), className, 0);
}
public static void RegisterClass(IntPtr handle)
{
objc_registerClassPair(handle);
}
2014-04-21 17:40:44 +00:00
static List<Delegate> storedDelegates = new List<Delegate>();
2014-04-19 10:17:14 +00:00
public static void RegisterMethod(IntPtr handle, Delegate d, string selector, string typeString)
{
// TypeString info:
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
IntPtr p = Marshal.GetFunctionPointerForDelegate(d);
bool r = class_addMethod(handle, Selector.Get(selector), p, typeString);
if (!r)
{
throw new ArgumentException("Could not register method " + d + " in class + " + class_getName(handle));
}
2014-04-21 17:40:44 +00:00
storedDelegates.Add(d); // Don't let the garbage collector eat our delegates.
2014-04-19 10:17:14 +00:00
}
2014-04-18 20:30:50 +00:00
}
}