Thrift-1340: Add support of ARC to Objective-C
Client: Objective-c
Patch: Hirano Satoshi
Adds -objc-arc flag to compiler and if used removes the retain/release/autorelease from generated code
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1210732 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cocoa/src/server/TSocketServer.m b/lib/cocoa/src/server/TSocketServer.m
index 5f7fa3c..057f41f 100644
--- a/lib/cocoa/src/server/TSocketServer.m
+++ b/lib/cocoa/src/server/TSocketServer.m
@@ -22,6 +22,7 @@
#import "TNSFileHandleTransport.h"
#import "TProtocol.h"
#import "TTransportException.h"
+#import "TObjective-C.h"
#import <sys/socket.h>
#include <netinet/in.h>
@@ -40,9 +41,9 @@
{
self = [super init];
- mInputProtocolFactory = [protocolFactory retain];
- mOutputProtocolFactory = [protocolFactory retain];
- mProcessorFactory = [processorFactory retain];
+ mInputProtocolFactory = [protocolFactory retain_stub];
+ mOutputProtocolFactory = [protocolFactory retain_stub];
+ mProcessorFactory = [processorFactory retain_stub];
// create a socket.
int fd = -1;
@@ -60,7 +61,7 @@
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)];
- if (CFSocketSetAddress(socket, (CFDataRef)address) != kCFSocketSuccess) {
+ if (CFSocketSetAddress(socket, (bridge_stub CFDataRef)address) != kCFSocketSuccess) {
CFSocketInvalidate(socket);
CFRelease(socket);
NSLog(@"*** Could not bind to address");
@@ -96,11 +97,11 @@
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObject: self];
- [mInputProtocolFactory release];
- [mOutputProtocolFactory release];
- [mProcessorFactory release];
- [mSocketFileHandle release];
- [super dealloc];
+ [mInputProtocolFactory release_stub];
+ [mOutputProtocolFactory release_stub];
+ [mProcessorFactory release_stub];
+ [mSocketFileHandle release_stub];
+ [super dealloc_stub];
}
@@ -119,6 +120,38 @@
- (void) handleClientConnection: (NSFileHandle *) clientSocket
{
+#if __has_feature(objc_arc)
+ @autoreleasepool {
+ TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket];
+ id<TProcessor> processor = [mProcessorFactory processorForTransport: transport];
+
+ id <TProtocol> inProtocol = [mInputProtocolFactory newProtocolOnTransport: transport];
+ id <TProtocol> outProtocol = [mOutputProtocolFactory newProtocolOnTransport: transport];
+
+ @try {
+ BOOL result = NO;
+ do {
+ @autoreleasepool {
+ result = [processor processOnInputProtocol: inProtocol outputProtocol: outProtocol];
+ }
+ } while (result);
+ }
+ @catch (TTransportException * te) {
+ //NSLog(@"Caught transport exception, abandoning client connection: %@", te);
+ }
+
+ NSNotification * n = [NSNotification notificationWithName: kTSocketServer_ClientConnectionFinishedForProcessorNotification
+ object: self
+ userInfo: [NSDictionary dictionaryWithObjectsAndKeys:
+ processor,
+ kTSocketServer_ProcessorKey,
+ transport,
+ kTSockerServer_TransportKey,
+ nil]];
+ [[NSNotificationCenter defaultCenter] performSelectorOnMainThread: @selector(postNotification:) withObject: n waitUntilDone: YES];
+
+ }
+#else
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket];
@@ -150,6 +183,7 @@
[[NSNotificationCenter defaultCenter] performSelectorOnMainThread: @selector(postNotification:) withObject: n waitUntilDone: YES];
[pool release];
+#endif
}