Permalink
Browse files

Implemented <OHBridged> for NSValue

  • Loading branch information...
jspahrsummers committed Aug 9, 2012
1 parent 2da504e commit 96b35783e936422115a8142aa6d447c5d02f725c
@@ -46,6 +46,8 @@
D0F3A31C15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0F3A31A15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m */; };
D0F3A31F15D3717C0032B8D9 /* NSData+OHExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0F3A31D15D3717C0032B8D9 /* NSData+OHExtensions.h */; };
D0F3A32015D3717C0032B8D9 /* NSData+OHExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0F3A31E15D3717C0032B8D9 /* NSData+OHExtensions.m */; };
+ D0F3A33015D3736B0032B8D9 /* NSValue+OHExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0F3A32E15D3736B0032B8D9 /* NSValue+OHExtensions.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0F3A33115D3736B0032B8D9 /* NSValue+OHExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0F3A32F15D3736B0032B8D9 /* NSValue+OHExtensions.m */; };
/* End PBXBuildFile section */
/* Begin PBXBuildRule section */
@@ -175,6 +177,8 @@
D0F3A31A15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+OHExtensions.m"; sourceTree = "<group>"; };
D0F3A31D15D3717C0032B8D9 /* NSData+OHExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+OHExtensions.h"; sourceTree = "<group>"; };
D0F3A31E15D3717C0032B8D9 /* NSData+OHExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+OHExtensions.m"; sourceTree = "<group>"; };
+ D0F3A32E15D3736B0032B8D9 /* NSValue+OHExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSValue+OHExtensions.h"; sourceTree = "<group>"; };
+ D0F3A32F15D3736B0032B8D9 /* NSValue+OHExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSValue+OHExtensions.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -214,6 +218,8 @@
D090624E15B0E613001CAC43 /* NSObject+OHExtensions.m */,
D0F3A2FB15D359650032B8D9 /* NSString+OHExtensions.h */,
D0F3A2FC15D359650032B8D9 /* NSString+OHExtensions.m */,
+ D0F3A32E15D3736B0032B8D9 /* NSValue+OHExtensions.h */,
+ D0F3A32F15D3736B0032B8D9 /* NSValue+OHExtensions.m */,
D0F3A2F015D353CB0032B8D9 /* OHBridged.h */,
D08A8AE715CBBC6D0084E989 /* OHModel.h */,
D08A8AE815CBBC6D0084E989 /* OHModel.m */,
@@ -445,6 +451,7 @@
D0F3A31715D366730032B8D9 /* NSArray+OHExtensions.h in Headers */,
D0F3A31B15D36F5D0032B8D9 /* NSDictionary+OHExtensions.h in Headers */,
D0F3A31F15D3717C0032B8D9 /* NSData+OHExtensions.h in Headers */,
+ D0F3A33015D3736B0032B8D9 /* NSValue+OHExtensions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -592,6 +599,7 @@
D0F3A31815D366730032B8D9 /* NSArray+OHExtensions.m in Sources */,
D0F3A31C15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m in Sources */,
D0F3A32015D3717C0032B8D9 /* NSData+OHExtensions.m in Sources */,
+ D0F3A33115D3736B0032B8D9 /* NSValue+OHExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,18 @@
+//
+// NSValue+OHExtensions.h
+// ObjectiveHaskell
+//
+// Created by Justin Spahr-Summers on 08.08.12.
+// Copyright (c) 2012 Justin Spahr-Summers. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "OHBridged.h"
+
+/*
+ * Bridges between pointer NSValues and Haskell Ptr values.
+ *
+ * Other NSValue types are not supported.
+ */
+@interface NSValue (OHExtensions) <OHBridged>
+@end
@@ -0,0 +1,23 @@
+//
+// NSValue+OHExtensions.m
+// ObjectiveHaskell
+//
+// Created by Justin Spahr-Summers on 08.08.12.
+// Copyright (c) 2012 Justin Spahr-Summers. All rights reserved.
+//
+
+#import "NSValue+OHExtensions.h"
+#import "NSValue_stub.h"
+
+@implementation NSValue (OHExtensions)
+
++ (instancetype)objectWithHaskellPointer:(OHHaskellPtr)haskellPointer {
+ return OHNSValueFromHaskellPtr(haskellPointer);
+}
+
+- (OHHaskellPtr)haskellPointer {
+ NSAssert(self.objCType[0] == '^', @"Can only get the Haskell pointer for pointer NSValues, not %@", self);
+ return OHHaskellPtrFromNSValue(self);
+}
+
+@end
@@ -7,6 +7,7 @@ module ObjectiveHaskell.NSValue (
) where
import Foreign.Ptr
+import Foreign.StablePtr
import ObjectiveHaskell.MsgSend
import ObjectiveHaskell.ObjC
@@ -25,3 +26,12 @@ toNSValue ptr = getClass "NSValue" >>= valueWithPointer ptr
instance Bridged (Ptr ()) where
toObjC = toNSValue
fromObjC = fromNSValue
+
+fromNSValueObjC :: Id -> IO (StablePtr (Ptr ()))
+fromNSValueObjC obj = fromNSValue obj >>= newStablePtr
+
+toNSValueObjC :: StablePtr (Ptr ()) -> IO Id
+toNSValueObjC ptr = deRefStablePtr ptr >>= toNSValue
+
+exportFunc "OHHaskellPtrFromNSValue" [t| UnsafeId -> IO (StablePtr (Ptr ())) |] 'fromNSValueObjC
+exportFunc "OHNSValueFromHaskellPtr" [t| StablePtr (Ptr ()) -> IO UnsafeId |] 'toNSValueObjC
@@ -11,6 +11,7 @@
#import "NSDictionary+OHExtensions.h"
#import "NSNumber+OHExtensions.h"
#import "NSString+OHExtensions.h"
+#import "NSValue+OHExtensions.h"
#import "OHBridged.h"
#import "OHModel.h"
#import "OHTypes.h"
@@ -71,6 +71,9 @@ - (void)testNSValueBridging {
uintptr_t addr = ptrAddress(value);
STAssertEquals(addr, (uintptr_t)0, @"");
+
+ NSValue *passthroughValue = [NSValue objectWithHaskellPointer:value.haskellPointer];
+ STAssertEqualObjects(value, passthroughValue, @"");
}
- (void)testNSURLConversion {

0 comments on commit 96b3578

Please sign in to comment.