Permalink
Browse files

Added NSDate parsing

  • Loading branch information...
1 parent f4ce178 commit 061314820d3adb011b34cb73f9b02b6c875b6731 @stevestreza stevestreza committed Sep 12, 2011
View
@@ -9,6 +9,7 @@ Features
- NSDictionary
- NSData
- NSString
+ - NSDate
- NSNumber for bool, UInt64, Int64, and double types
- NSFileHandle
- UUIDs (via a custom XPCUUID class)
@@ -20,6 +21,7 @@ Wish List
- Auto-boxing and auto-unboxing for
- NSData, backed by shared memory (some code for this exists but it is not reliable, causes crashes, doesn't return consistent data, etc.)
+ - IOSurfaceRef
- XPCConnection
- Compatibility mode for iOS/Snow Leopard
- Both client and service code lives within the app
@@ -33,11 +33,14 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
NSNumber *result = [message objectForKey:@"result"];
NSData *data = [message objectForKey:@"data"];
NSFileHandle *fileHandle = [message objectForKey:@"fileHandle"];
+ NSDate *date = [message objectForKey:@"date"];
if(result){
NSLog(@"We got a calculation result! %@", result);
}else if(data || fileHandle){
NSData *newData = [fileHandle readDataToEndOfFile];
NSLog(@"We got a file handle! Read %i bytes - %@", newData.length, fileHandle);
+ }else if(date){
+ NSLog(@"It is now %@", date);
}
};
@@ -75,6 +78,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[mathConnection sendMessage:multiplyData];
[readConnection sendMessage:readData];
+
+ [mathConnection sendMessage:[NSDictionary dictionaryWithObject:@"whatTimeIsIt" forKey:@"operation"]];
}
@end
View
@@ -51,6 +51,10 @@ int main(int argc, const char *argv[])
fileHandle, @"fileHandle",
nil]];
}
+
+ if([[message objectForKey:@"operation"] isEqual:@"whatTimeIsIt"]){
+ [connection sendMessage:[NSDictionary dictionaryWithObject:[NSDate date] forKey:@"date"]];
+ }
}];
}];
@@ -14,6 +14,10 @@
1E835CB113DF748000338391 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD0B613DE79D200D5AEC3 /* Foundation.framework */; };
1E835CB313DF74BF00338391 /* XPCKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD0CF13DE9FAB00D5AEC3 /* XPCKit.framework */; };
1E835CB513DF74C900338391 /* XPCKit.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD0CF13DE9FAB00D5AEC3 /* XPCKit.framework */; };
+ 1EB022A5141D9F3700BCC1FB /* NSDate+XPCParse.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EB022A3141D9F3700BCC1FB /* NSDate+XPCParse.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 1EB022A6141D9F3700BCC1FB /* NSDate+XPCParse.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EB022A3141D9F3700BCC1FB /* NSDate+XPCParse.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 1EB022A7141D9F3700BCC1FB /* NSDate+XPCParse.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB022A4141D9F3700BCC1FB /* NSDate+XPCParse.m */; };
+ 1EB022A8141D9F3700BCC1FB /* NSDate+XPCParse.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB022A4141D9F3700BCC1FB /* NSDate+XPCParse.m */; };
1EEDD03E13DD485400D5AEC3 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD03D13DD485400D5AEC3 /* SenTestingKit.framework */; };
1EEDD04213DD485400D5AEC3 /* libXPCKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD02813DD485400D5AEC3 /* libXPCKit.a */; };
1EEDD04813DD485400D5AEC3 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1EEDD04613DD485400D5AEC3 /* InfoPlist.strings */; };
@@ -141,6 +145,8 @@
/* Begin PBXFileReference section */
1E5F84A613E10DB700234F31 /* XPCService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPCService.h; sourceTree = "<group>"; };
1E5F84A713E10DB700234F31 /* XPCService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XPCService.m; sourceTree = "<group>"; };
+ 1EB022A3141D9F3700BCC1FB /* NSDate+XPCParse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDate+XPCParse.h"; sourceTree = "<group>"; };
+ 1EB022A4141D9F3700BCC1FB /* NSDate+XPCParse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDate+XPCParse.m"; sourceTree = "<group>"; };
1EEDD02813DD485400D5AEC3 /* libXPCKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libXPCKit.a; sourceTree = BUILT_PRODUCTS_DIR; };
1EEDD03313DD485400D5AEC3 /* XPCKit-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "XPCKit-Prefix.pch"; sourceTree = "<group>"; };
1EEDD03413DD485400D5AEC3 /* XPCKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPCKit.h; sourceTree = "<group>"; };
@@ -388,6 +394,8 @@
1EEDD0AB13DE669D00D5AEC3 /* NSString+XPCParse.m */,
1EF4D9DF141C38D0007BEEC0 /* NSFileHandle+XPCParse.h */,
1EF4D9E0141C38D0007BEEC0 /* NSFileHandle+XPCParse.m */,
+ 1EB022A3141D9F3700BCC1FB /* NSDate+XPCParse.h */,
+ 1EB022A4141D9F3700BCC1FB /* NSDate+XPCParse.m */,
);
name = Extensions;
sourceTree = "<group>";
@@ -411,6 +419,7 @@
1EEDD0F213DEB97000D5AEC3 /* XPCUUID.h in Headers */,
1E5F84A913E10DB700234F31 /* XPCService.h in Headers */,
1EF4D9E2141C38D0007BEEC0 /* NSFileHandle+XPCParse.h in Headers */,
+ 1EB022A6141D9F3700BCC1FB /* NSDate+XPCParse.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -431,6 +440,7 @@
1E5F84A813E10DB700234F31 /* XPCService.h in Headers */,
1EF4D9E1141C38D0007BEEC0 /* NSFileHandle+XPCParse.h in Headers */,
1EEDD0F113DEB97000D5AEC3 /* XPCUUID.h in Headers */,
+ 1EB022A5141D9F3700BCC1FB /* NSDate+XPCParse.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -630,6 +640,7 @@
1EEDD0B313DE684300D5AEC3 /* NSArray+XPCParse.m in Sources */,
1EEDD08D13DDC34800D5AEC3 /* NSDictionary+XPCParse.m in Sources */,
1EF4D9E4141C38D0007BEEC0 /* NSFileHandle+XPCParse.m in Sources */,
+ 1EB022A8141D9F3700BCC1FB /* NSDate+XPCParse.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -675,6 +686,7 @@
1EEDD0E013DEA04900D5AEC3 /* NSArray+XPCParse.m in Sources */,
1EEDD0DF13DEA04900D5AEC3 /* NSDictionary+XPCParse.m in Sources */,
1EF4D9E3141C38D0007BEEC0 /* NSFileHandle+XPCParse.m in Sources */,
+ 1EB022A7141D9F3700BCC1FB /* NSDate+XPCParse.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -0,0 +1,27 @@
+//
+// NSDate+XPCParse.h
+// XPCKit
+//
+// Created by Steve Streza on 9/11/11. Copyright 2011 XPCKit.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSDate (NSDate_XPCParse)
+
++(NSDate *)dateWithXPCObject:(xpc_object_t)xpc;
+-(xpc_object_t)newXPCObject;
+
+@end
View
@@ -0,0 +1,36 @@
+//
+// NSDate+XPCParse.m
+// XPCKit
+//
+// Created by Steve Streza on 9/11/11. Copyright 2011 XPCKit.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#import "NSDate+XPCParse.h"
+#import <xpc/xpc.h>
+
+@implementation NSDate (XPCParse)
+
++(NSDate *)dateWithXPCObject:(xpc_object_t)xpc{
+ int64_t nanosecondsInterval = xpc_date_get_value(xpc);
+ NSTimeInterval interval = nanosecondsInterval / 1000000000.;
+ return [NSDate dateWithTimeIntervalSince1970:interval];
+
+}
+
+-(xpc_object_t)newXPCObject{
+ return xpc_date_create((int64_t)([self timeIntervalSince1970] * 1000000000));
+}
+
+@end
@@ -30,6 +30,8 @@ +(id)objectWithXPCObject:(xpc_object_t)xpcObject{
object = [NSDictionary dictionaryWithContentsOfXPCObject:xpcObject];
}else if(type == XPC_TYPE_ARRAY){
object = [NSArray arrayWithContentsOfXPCObject:xpcObject];
+ }else if(type == XPC_TYPE_DATE){
+ object = [NSDate dateWithXPCObject:xpcObject];
}else if(type == XPC_TYPE_DATA || type == XPC_TYPE_SHMEM){
object = [NSData dataWithXPCObject:xpcObject];
}else if(type == XPC_TYPE_STRING){
@@ -38,7 +40,7 @@ +(id)objectWithXPCObject:(xpc_object_t)xpcObject{
object = [XPCUUID uuidWithXPCObject:xpcObject];
}else if(type == XPC_TYPE_FD){
object = [NSFileHandle fileHandleWithXPCObject:xpcObject];
- }else if(type == XPC_TYPE_BOOL || type == XPC_TYPE_UINT64 || type == XPC_TYPE_INT64 || XPC_TYPE_DOUBLE){
+ }else if(type == XPC_TYPE_BOOL || type == XPC_TYPE_UINT64 || type == XPC_TYPE_INT64 || type == XPC_TYPE_DOUBLE){
object = [NSNumber numberWithXPCObject:xpcObject];
}
return object;
View
@@ -28,6 +28,7 @@
#import "NSNumber+XPCParse.h"
#import "NSData+XPCParse.h"
#import "NSString+XPCParse.h"
+#import "NSDate+XPCParse.h"
#import "NSFileHandle+XPCParse.h"
View
@@ -70,6 +70,17 @@ -(void)testDictionaries{
nil]];
}
+-(void)testDates{
+ [self testEqualityOfXPCRoundtripForObject:[NSDate date]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSince1970:20.]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSince1970:2000000.]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSince1970:2000000000.]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSinceNow:10.]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSinceNow:-10.]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSinceNow:10000.]];
+ [self testEqualityOfXPCRoundtripForObject:[NSDate dateWithTimeIntervalSinceNow:-10000.]];
+}
+
-(void)testUUID{
// UUIDs are unique, so test a few at random
STAssertFalse([[XPCUUID uuid] isEqual:[XPCUUID uuid]], @"Two identical UUIDs");
@@ -90,7 +101,13 @@ -(void)testEqualityOfXPCRoundtripForObject:(id)object{
id outObject = [NSObject objectWithXPCObject:xpcObject];
STAssertNotNil(outObject, @"XPC-converted object is nil");
- STAssertEqualObjects(object, outObject, @"Object %@ was not equal to result %@", outObject);
+ if([object isKindOfClass:[NSDate class]]){
+ NSTimeInterval delta = fabsf([object timeIntervalSinceDate:outObject]);
+ BOOL smallEnough = (delta < 0.000001);
+ STAssertTrue(smallEnough, @"Date %@ was not equal to result %@", object, outObject);
+ }else{
+ STAssertEqualObjects(object, outObject, @"Object %@ was not equal to result %@", object, outObject);
+ }
xpc_release(xpcObject);
}

0 comments on commit 0613148

Please sign in to comment.