Skip to content
This repository
Browse code

Implemented <OHBridged> for NSValue

  • Loading branch information...
commit 96b35783e936422115a8142aa6d447c5d02f725c 1 parent 2da504e
Justin Spahr-Summers authored August 08, 2012
8  ObjectiveHaskell.xcodeproj/project.pbxproj
@@ -46,6 +46,8 @@
46 46
 		D0F3A31C15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0F3A31A15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m */; };
47 47
 		D0F3A31F15D3717C0032B8D9 /* NSData+OHExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0F3A31D15D3717C0032B8D9 /* NSData+OHExtensions.h */; };
48 48
 		D0F3A32015D3717C0032B8D9 /* NSData+OHExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0F3A31E15D3717C0032B8D9 /* NSData+OHExtensions.m */; };
  49
+		D0F3A33015D3736B0032B8D9 /* NSValue+OHExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0F3A32E15D3736B0032B8D9 /* NSValue+OHExtensions.h */; settings = {ATTRIBUTES = (Public, ); }; };
  50
+		D0F3A33115D3736B0032B8D9 /* NSValue+OHExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0F3A32F15D3736B0032B8D9 /* NSValue+OHExtensions.m */; };
49 51
 /* End PBXBuildFile section */
50 52
 
51 53
 /* Begin PBXBuildRule section */
@@ -175,6 +177,8 @@
175 177
 		D0F3A31A15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+OHExtensions.m"; sourceTree = "<group>"; };
176 178
 		D0F3A31D15D3717C0032B8D9 /* NSData+OHExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+OHExtensions.h"; sourceTree = "<group>"; };
177 179
 		D0F3A31E15D3717C0032B8D9 /* NSData+OHExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+OHExtensions.m"; sourceTree = "<group>"; };
  180
+		D0F3A32E15D3736B0032B8D9 /* NSValue+OHExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSValue+OHExtensions.h"; sourceTree = "<group>"; };
  181
+		D0F3A32F15D3736B0032B8D9 /* NSValue+OHExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSValue+OHExtensions.m"; sourceTree = "<group>"; };
178 182
 /* End PBXFileReference section */
179 183
 
180 184
 /* Begin PBXFrameworksBuildPhase section */
@@ -214,6 +218,8 @@
214 218
 				D090624E15B0E613001CAC43 /* NSObject+OHExtensions.m */,
215 219
 				D0F3A2FB15D359650032B8D9 /* NSString+OHExtensions.h */,
216 220
 				D0F3A2FC15D359650032B8D9 /* NSString+OHExtensions.m */,
  221
+				D0F3A32E15D3736B0032B8D9 /* NSValue+OHExtensions.h */,
  222
+				D0F3A32F15D3736B0032B8D9 /* NSValue+OHExtensions.m */,
217 223
 				D0F3A2F015D353CB0032B8D9 /* OHBridged.h */,
218 224
 				D08A8AE715CBBC6D0084E989 /* OHModel.h */,
219 225
 				D08A8AE815CBBC6D0084E989 /* OHModel.m */,
@@ -445,6 +451,7 @@
445 451
 				D0F3A31715D366730032B8D9 /* NSArray+OHExtensions.h in Headers */,
446 452
 				D0F3A31B15D36F5D0032B8D9 /* NSDictionary+OHExtensions.h in Headers */,
447 453
 				D0F3A31F15D3717C0032B8D9 /* NSData+OHExtensions.h in Headers */,
  454
+				D0F3A33015D3736B0032B8D9 /* NSValue+OHExtensions.h in Headers */,
448 455
 			);
449 456
 			runOnlyForDeploymentPostprocessing = 0;
450 457
 		};
@@ -592,6 +599,7 @@
592 599
 				D0F3A31815D366730032B8D9 /* NSArray+OHExtensions.m in Sources */,
593 600
 				D0F3A31C15D36F5D0032B8D9 /* NSDictionary+OHExtensions.m in Sources */,
594 601
 				D0F3A32015D3717C0032B8D9 /* NSData+OHExtensions.m in Sources */,
  602
+				D0F3A33115D3736B0032B8D9 /* NSValue+OHExtensions.m in Sources */,
595 603
 			);
596 604
 			runOnlyForDeploymentPostprocessing = 0;
597 605
 		};
18  ObjectiveHaskell/NSValue+OHExtensions.h
... ...
@@ -0,0 +1,18 @@
  1
+//
  2
+//  NSValue+OHExtensions.h
  3
+//  ObjectiveHaskell
  4
+//
  5
+//  Created by Justin Spahr-Summers on 08.08.12.
  6
+//  Copyright (c) 2012 Justin Spahr-Summers. All rights reserved.
  7
+//
  8
+
  9
+#import <Foundation/Foundation.h>
  10
+#import "OHBridged.h"
  11
+
  12
+/*
  13
+ * Bridges between pointer NSValues and Haskell Ptr values.
  14
+ *
  15
+ * Other NSValue types are not supported.
  16
+ */
  17
+@interface NSValue (OHExtensions) <OHBridged>
  18
+@end
23  ObjectiveHaskell/NSValue+OHExtensions.m
... ...
@@ -0,0 +1,23 @@
  1
+//
  2
+//  NSValue+OHExtensions.m
  3
+//  ObjectiveHaskell
  4
+//
  5
+//  Created by Justin Spahr-Summers on 08.08.12.
  6
+//  Copyright (c) 2012 Justin Spahr-Summers. All rights reserved.
  7
+//
  8
+
  9
+#import "NSValue+OHExtensions.h"
  10
+#import "NSValue_stub.h"
  11
+
  12
+@implementation NSValue (OHExtensions)
  13
+
  14
++ (instancetype)objectWithHaskellPointer:(OHHaskellPtr)haskellPointer {
  15
+	return OHNSValueFromHaskellPtr(haskellPointer);
  16
+}
  17
+
  18
+- (OHHaskellPtr)haskellPointer {
  19
+	NSAssert(self.objCType[0] == '^', @"Can only get the Haskell pointer for pointer NSValues, not %@", self);
  20
+	return OHHaskellPtrFromNSValue(self);
  21
+}
  22
+
  23
+@end
10  ObjectiveHaskell/NSValue.hs
@@ -7,6 +7,7 @@ module ObjectiveHaskell.NSValue (
7 7
     ) where
8 8
 
9 9
 import Foreign.Ptr
  10
+import Foreign.StablePtr
10 11
 import ObjectiveHaskell.MsgSend
11 12
 import ObjectiveHaskell.ObjC
12 13
 
@@ -25,3 +26,12 @@ toNSValue ptr = getClass "NSValue" >>= valueWithPointer ptr
25 26
 instance Bridged (Ptr ()) where
26 27
     toObjC = toNSValue
27 28
     fromObjC = fromNSValue
  29
+
  30
+fromNSValueObjC :: Id -> IO (StablePtr (Ptr ()))
  31
+fromNSValueObjC obj = fromNSValue obj >>= newStablePtr
  32
+
  33
+toNSValueObjC :: StablePtr (Ptr ()) -> IO Id
  34
+toNSValueObjC ptr = deRefStablePtr ptr >>= toNSValue
  35
+
  36
+exportFunc "OHHaskellPtrFromNSValue" [t| UnsafeId -> IO (StablePtr (Ptr ())) |] 'fromNSValueObjC
  37
+exportFunc "OHNSValueFromHaskellPtr" [t| StablePtr (Ptr ()) -> IO UnsafeId |] 'toNSValueObjC
1  ObjectiveHaskell/ObjectiveHaskell.h
@@ -11,6 +11,7 @@
11 11
 #import "NSDictionary+OHExtensions.h"
12 12
 #import "NSNumber+OHExtensions.h"
13 13
 #import "NSString+OHExtensions.h"
  14
+#import "NSValue+OHExtensions.h"
14 15
 #import "OHBridged.h"
15 16
 #import "OHModel.h"
16 17
 #import "OHTypes.h"
3  ObjectiveHaskellTests/ObjectiveHaskellTests.m
@@ -71,6 +71,9 @@ - (void)testNSValueBridging {
71 71
 
72 72
 	uintptr_t addr = ptrAddress(value);
73 73
 	STAssertEquals(addr, (uintptr_t)0, @"");
  74
+
  75
+	NSValue *passthroughValue = [NSValue objectWithHaskellPointer:value.haskellPointer];
  76
+	STAssertEqualObjects(value, passthroughValue, @"");
74 77
 }
75 78
 
76 79
 - (void)testNSURLConversion {

0 notes on commit 96b3578

Please sign in to comment.
Something went wrong with that request. Please try again.