Skip to content

Instantly share code, notes, and snippets.

@billinghamj
Last active August 29, 2015 14:03
Show Gist options
  • Save billinghamj/8219637a149ad8f93d73 to your computer and use it in GitHub Desktop.
Save billinghamj/8219637a149ad8f93d73 to your computer and use it in GitHub Desktop.
Object Oriented bridging through C
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, BatteryStatus)
{
BatteryStatusUnknown,
BatteryStatusOkay,
BatteryStatusLow,
BatteryStatusEmpty
};
@protocol MyClassDelegate <NSObject>
@optional
- (void)batteryStatusDidChange:(BatteryStatus)status;
@required
- (void)audioWasPaused;
- (void)audioWasResumed;
@end
@interface MyClass : NSObject
- (NSString *)doSomething;
- (void)somethingElse:(int)number;
- (void)start:(void (^)(BOOL success))completion;
@property (nonatomic, weak) id <MyClassDelegate> delegate;
@property (nonatomic, assign) int myNumber;
@property (nonatomic, strong) NSString *myString;
@end
/////
typedef void (*MyClass_completionDelegate)(BOOL success);
typedef void (*MyClassDelegate_batteryStatusDidChangeDelegate)(BatteryStatus status);
typedef void (*MyClassDelegate_audioWasPausedDelegate)();
typedef void (*MyClassDelegate_audioWasResumedDelegate)();
@interface MyClassDelegateBridge : NSObject <MyClassDelegate>
@property (nonatomic, assign) MyClassDelegate_batteryStatusDidChangeDelegate batteryStatusDidChangeCallback;
@property (nonatomic, assign) MyClassDelegate_audioWasPausedDelegate audioWasPausedCallback;
@property (nonatomic, assign) MyClassDelegate_audioWasResumedDelegate audioWasResumedCallback;
@end
#if __cplusplus
extern "C" {
#endif
extern void *MyClass_init();
extern char *MyClass_doSomething(void *instance);
extern void MyClass_somethingElse(void *instance, int number);
extern void MyClass_start(void *instance, MyClass_completionDelegate completion);
extern void *MyClass_delegate(void *instance);
extern void MyClass_setDelegate(void *instance, void *value);
extern int MyClass_myNumber(void *instance);
extern void MyClass_setMyNumber(void *instance, int value);
extern char *MyClass_myString(void *instance);
extern void MyClass_setMyString(void *instance, char *value);
extern void *MyClassDelegate_init();
extern MyClassDelegate_batteryStatusDidChangeDelegate MyClassDelegate_batteryStatusDidChangeCallback(void *instance);
extern void MyClassDelegate_setBatteryStatusDidChangeCallback(void *instance, MyClassDelegate_batteryStatusDidChangeDelegate value);
extern MyClassDelegate_audioWasPausedDelegate MyClassDelegate_audioWasPausedCallback(void *instance);
extern void MyClassDelegate_setAudioWasPausedCallback(void *instance, MyClassDelegate_audioWasPausedDelegate value);
extern MyClassDelegate_audioWasResumedDelegate MyClassDelegate_audioWasResumedCallback(void *instance);
extern void MyClassDelegate_setAudioWasResumedCallback(void *instance, MyClassDelegate_audioWasResumedDelegate value);
#if __cplusplus
}
#endif
using System;
using System.Runtime.InteropServices;
public enum BatteryStatus : int
{
Unknown,
Okay,
Low,
Empty
}
public class MyClassDelegate
{
public delegate void BatteryStatusDidChangeDelegate(BatteryStatus status);
public delegate void AudioWasPausedDelegate();
public delegate void AudioWasResumedDelegate();
private static class C
{
[DllImport("__Internal", EntryPoint="MyClassDelegate_init")]
public static extern IntPtr init();
[DllImport("__Internal", EntryPoint="MyClassDelegate_batteryStatusDidChangeCallback")]
public static extern BatteryStatusDidChangeDelegate batteryStatusDidChange(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClassDelegate_setBatteryStatusDidChangeCallback")]
public static extern void setBatteryStatusDidChange(IntPtr instance, BatteryStatusDidChangeDelegate value);
[DllImport("__Internal", EntryPoint="MyClassDelegate_audioWasPausedCallback")]
public static extern AudioWasPausedDelegate audioWasPaused(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClassDelegate_setAudioWasPausedCallback")]
public static extern void setAudioWasPaused(IntPtr instance, AudioWasPausedDelegate value);
[DllImport("__Internal", EntryPoint="MyClassDelegate_audioWasResumedCallback")]
public static extern AudioWasResumedDelegate audioWasResumed(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClassDelegate_setAudioWasResumedCallback")]
public static extern void setAudioWasResumed(IntPtr instance, AudioWasResumedDelegate value);
}
public readonly IntPtr _instance;
public MyClassDelegate()
{
_instance = C.init();
}
public MyClassDelegate(IntPtr instance)
{
_instance = instance;
}
public BatteryStatusDidChangeDelegate BatteryStatusDidChange
{
get { return C.batteryStatusDidChange(_instance); }
set { C.setBatteryStatusDidChange(_instance, value); }
}
public AudioWasPausedDelegate AudioWasPaused
{
get { return C.audioWasPaused(_instance); }
set { C.setAudioWasPaused(_instance, value); }
}
public AudioWasResumedDelegate AudioWasResumed
{
get { return C.audioWasResumed(_instance); }
set { C.setAudioWasResumed(_instance, value); }
}
}
public class MyClass
{
public delegate void CompletionDelegate(bool success);
private static class C
{
[DllImport("__Internal", EntryPoint="MyClass_init")]
public static extern IntPtr init();
[DllImport("__Internal", EntryPoint="MyClass_doSomething")]
public static extern string doSomething(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClass_somethingElse")]
public static extern void somethingElse(IntPtr instance, int number);
[DllImport("__Internal", EntryPoint="MyClass_start")]
public static extern void start(IntPtr instance, CompletionDelegate completion);
[DllImport("__Internal", EntryPoint="MyClass_delegate")]
public static extern IntPtr @delegate(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClass_setDelegate")]
public static extern void setDelegate(IntPtr instance, IntPtr value);
[DllImport("__Internal", EntryPoint="MyClass_myNumber")]
public static extern int myNumber(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClass_setMyNumber")]
public static extern void setMyNumber(IntPtr instance, int value);
[DllImport("__Internal", EntryPoint="MyClass_myString")]
public static extern string myString(IntPtr instance);
[DllImport("__Internal", EntryPoint="MyClass_setMyString")]
public static extern void setMyString(IntPtr instance, string value);
}
public readonly IntPtr _instance;
public MyClass()
{
_instance = C.init();
}
public MyClass(IntPtr instance)
{
_instance = instance;
}
public string DoSomething()
{
return C.doSomething(_instance);
}
public void SomethingElse(int number)
{
C.somethingElse(_instance, number);
}
public void Start(CompletionDelegate completion)
{
C.start(_instance, completion);
}
public MyClassDelegate Delegate
{
get { return new MyClassDelegate(C.@delegate(_instance)); }
set { C.setDelegate(_instance, value._instance); }
}
public int MyNumber
{
get { return C.myNumber(_instance); }
set { C.setMyNumber(_instance, value); }
}
public string MyString
{
get { return C.myString(_instance); }
set { C.setMyString(_instance, value); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment