diff options
author | Benoit Foucher <benoit@zeroc.com> | 2016-02-05 16:25:26 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2016-02-05 16:25:26 +0100 |
commit | 3cfb37df79a86ff279bee8e57bbe8105ec7edd5e (patch) | |
tree | de6e4384041940a162d2bf6711359186bc4240dd /objective-c/src | |
parent | Fix for java8 compilation failure (diff) | |
download | ice-3cfb37df79a86ff279bee8e57bbe8105ec7edd5e.tar.bz2 ice-3cfb37df79a86ff279bee8e57bbe8105ec7edd5e.tar.xz ice-3cfb37df79a86ff279bee8e57bbe8105ec7edd5e.zip |
Updated Objective-C mapping to use new stream classes
Diffstat (limited to 'objective-c/src')
-rw-r--r-- | objective-c/src/Ice/CommunicatorI.h | 9 | ||||
-rw-r--r-- | objective-c/src/Ice/CommunicatorI.mm | 156 | ||||
-rw-r--r-- | objective-c/src/Ice/Initialize.mm | 104 | ||||
-rw-r--r-- | objective-c/src/Ice/Makefile | 1 | ||||
-rw-r--r-- | objective-c/src/Ice/Object.mm | 58 | ||||
-rw-r--r-- | objective-c/src/Ice/ObjectAdapterI.mm | 28 | ||||
-rw-r--r-- | objective-c/src/Ice/ObjectI.h | 12 | ||||
-rw-r--r-- | objective-c/src/Ice/PropertiesI.h | 2 | ||||
-rw-r--r-- | objective-c/src/Ice/Proxy.mm | 173 | ||||
-rw-r--r-- | objective-c/src/Ice/Stream.mm | 377 | ||||
-rw-r--r-- | objective-c/src/Ice/StreamI.h | 18 | ||||
-rw-r--r-- | objective-c/src/Ice/Util.mm | 8 | ||||
-rw-r--r-- | objective-c/src/Ice/ValueFactoryI.h | 19 | ||||
-rw-r--r-- | objective-c/src/Ice/ValueFactoryI.mm | 161 |
14 files changed, 566 insertions, 560 deletions
diff --git a/objective-c/src/Ice/CommunicatorI.h b/objective-c/src/Ice/CommunicatorI.h index f1900a4f0e9..f29a5e58c8d 100644 --- a/objective-c/src/Ice/CommunicatorI.h +++ b/objective-c/src/Ice/CommunicatorI.h @@ -8,7 +8,6 @@ // ********************************************************************** #import <objc/Ice/Communicator.h> - #import <objc/Ice/LocalObject.h> #import <Foundation/NSSet.h> @@ -16,15 +15,17 @@ #include <Ice/Communicator.h> @class ICEObjectAdapter; +@class ICEValueFactoryManager; +@class ICEInitializationData; @interface ICECommunicator : ICELocalObject<ICECommunicator> { - NSMutableDictionary* valueFactories_; - NSMutableDictionary* objectFactories_; NSDictionary* prefixTable_; NSMutableDictionary* adminFacets_; + ICEValueFactoryManager* valueFactoryManager_; + NSMutableDictionary* objectFactories_; } --(void)setup:(NSDictionary*)prefixTable; +-(void)setup:(ICEInitializationData*)prefixTable; -(Ice::Communicator*)communicator; -(NSDictionary*)getPrefixTable; @end diff --git a/objective-c/src/Ice/CommunicatorI.mm b/objective-c/src/Ice/CommunicatorI.mm index 56f4a2bcd87..d6e1338ba69 100644 --- a/objective-c/src/Ice/CommunicatorI.mm +++ b/objective-c/src/Ice/CommunicatorI.mm @@ -20,120 +20,19 @@ #import <ProxyI.h> #import <LocalObjectI.h> #import <ObjectI.h> +#import <ValueFactoryI.h> #include <Ice/Router.h> #include <Ice/Locator.h> -#include <Ice/ObjectFactory.h> -#include <Ice/ValueFactory.h> #import <objc/Ice/Router.h> #import <objc/Ice/Locator.h> -#import <objc/Ice/ObjectFactory.h> -#import <objc/Ice/ValueFactory.h> +#import <objc/Ice/Initialize.h> #import <objc/runtime.h> #define COMMUNICATOR dynamic_cast<Ice::Communicator*>(static_cast<IceUtil::Shared*>(cxxObject_)) -namespace IceObjC -{ - -class UnknownSlicedValueFactoryI : public Ice::ValueFactory -{ -public: - - virtual Ice::ObjectPtr - create(const std::string&) - { - ICEUnknownSlicedObject* obj = [[ICEUnknownSlicedObject alloc] init]; - Ice::ObjectPtr o = [ICEInputStream createObjectReader:obj]; - [obj release]; - return o; - } -}; - -class ValueFactoryI : public Ice::ValueFactory -{ -public: - - // We must explicitely CFRetain/CFRelease so that the garbage - // collector does not trash the dictionaries. - ValueFactoryI(NSDictionary* factories, NSDictionary* prefixTable) : - _factories(factories), _prefixTable(prefixTable) - { - CFRetain(_factories); - CFRetain(_prefixTable); - } - - ~ValueFactoryI() - { - CFRelease(_factories); - CFRelease(_prefixTable); - } - - virtual Ice::ObjectPtr - create(const std::string& type) - { - NSString* sliceId = [[NSString alloc] initWithUTF8String:type.c_str()]; - @try - { - ICEValueFactory factory = nil; - @synchronized(_factories) - { - factory = [_factories objectForKey:sliceId]; - if(factory == nil) - { - factory = [_factories objectForKey:@""]; - } - } - - ICEObject* obj = nil; - if(factory != nil) - { - obj = [factory(sliceId) retain]; - } - - if(obj == nil) - { - std::string tId = toObjCSliceId(type, _prefixTable); - Class c = objc_lookUpClass(tId.c_str()); - if(c == nil) - { - return 0; // No value factory. - } - if([c isSubclassOfClass:[ICEObject class]]) - { - obj = (ICEObject*)[[c alloc] init]; - } - } - - Ice::ObjectPtr o; - if(obj != nil) - { - o = [ICEInputStream createObjectReader:obj]; - [obj release]; - } - return o; - } - @catch(id ex) - { - rethrowCxxException(ex); - } - @finally - { - [sliceId release]; - } - return nil; // Keep the compiler happy. - } - -private: - - NSDictionary* _factories; - NSDictionary* _prefixTable; -}; - -} - @interface ICEInternalPrefixTable(ICEInternal) +(NSDictionary*) newPrefixTable; @end @@ -161,27 +60,26 @@ private: @end @implementation ICECommunicator --(void)setup:(NSDictionary*)prefixTable +-(void)setup:(ICEInitializationData*)initData { - valueFactories_ = [[NSMutableDictionary alloc] init]; - objectFactories_ = [[NSMutableDictionary alloc] init]; - if(prefixTable) + if(initData.prefixTable__) { - prefixTable_ = [prefixTable retain]; + prefixTable_ = [initData.prefixTable__ retain]; } else { prefixTable_ = [ICEInternalPrefixTable newPrefixTable]; } adminFacets_ = [[NSMutableDictionary alloc] init]; - COMMUNICATOR->addValueFactory(new IceObjC::UnknownSlicedValueFactoryI, "::Ice::Object"); - COMMUNICATOR->addValueFactory(new IceObjC::ValueFactoryI(valueFactories_, prefixTable_), ""); + + valueFactoryManager_ = [[ICEValueFactoryManager alloc] init:COMMUNICATOR prefixTable:prefixTable_]; + objectFactories_ = [[NSMutableDictionary alloc] init]; } -(void) dealloc { - [prefixTable_ release]; - [valueFactories_ release]; + [valueFactoryManager_ release]; [objectFactories_ release]; + [prefixTable_ release]; [adminFacets_ release]; [super dealloc]; } @@ -421,44 +319,32 @@ private: @throw nsex; return nil; // Keep the compiler happy. } + -(void) addObjectFactory:(id<ICEObjectFactory>)factory sliceId:(NSString*)sliceId { - @synchronized(valueFactories_) + @synchronized(objectFactories_) { [objectFactories_ setObject:factory forKey:sliceId]; - - ICEValueFactory valueFactoryWrapper = ^(NSString* s) - { - return [factory create:s]; - }; - - [valueFactories_ setObject:ICE_AUTORELEASE([valueFactoryWrapper copy]) forKey:sliceId]; } + ICEValueFactory valueFactoryWrapper = ^(NSString* s) + { + return [factory create:s]; + }; + [valueFactoryManager_ add:valueFactoryWrapper sliceId:sliceId]; } + -(id<ICEObjectFactory>) findObjectFactory:(NSString*)sliceId { - @synchronized(valueFactories_) + @synchronized(objectFactories_) { return [objectFactories_ objectForKey:sliceId]; } return nil; // Keep the compiler happy. } --(void) addValueFactory:(ICEValueFactory)factory sliceId:(NSString*)sliceId -{ - @synchronized(valueFactories_) - { - [valueFactories_ setObject:ICE_AUTORELEASE([factory copy]) forKey:sliceId]; - } -} - --(ICEValueFactory) findValueFactory:(NSString*)sliceId +-(id<ICEValueFactoryManager>) getValueFactoryManager { - @synchronized(valueFactories_) - { - return [valueFactories_ objectForKey:sliceId]; - } - return nil; // Keep the compiler happy. + return valueFactoryManager_; } -(id<ICEImplicitContext>) getImplicitContext diff --git a/objective-c/src/Ice/Initialize.mm b/objective-c/src/Ice/Initialize.mm index f4bd535d367..07c5fba8bbb 100644 --- a/objective-c/src/Ice/Initialize.mm +++ b/objective-c/src/Ice/Initialize.mm @@ -389,7 +389,7 @@ private: } ICECommunicator* c = [ICECommunicator localObjectWithCxxObject:communicator.get()]; - [c setup:initData.prefixTable__]; + [c setup:initData]; return c; } catch(const std::exception& ex) @@ -400,23 +400,12 @@ private: return nil; // Keep the compiler happy. } -+(id<ICEInputStream>) createInputStream:(id<ICECommunicator>)communicator data:(NSData*)data ++(id<ICEInputStream>) createInputStream:(id<ICECommunicator>)c data:(NSData*)data { NSException* nsex = nil; try { - Ice::CommunicatorPtr com = [(ICECommunicator*)communicator communicator]; - Ice::Byte* start = (Ice::Byte*)[data bytes]; - Ice::Byte* end = (Ice::Byte*)[data bytes] + [data length]; - Ice::InputStreamPtr is = Ice::createInputStream(com, std::make_pair(start, end)); - if(is) - { - return [ICEInputStream localObjectWithCxxObject:is.get()]; - } - else - { - return nil; - } + return [[[ICEInputStream alloc] initWithCommunicator:c data:data encoding:nil] autorelease]; } catch(const std::exception& ex) { @@ -431,70 +420,7 @@ private: NSException* nsex = nil; try { - Ice::CommunicatorPtr com = [(ICECommunicator*)c communicator]; - Ice::Byte* start = (Ice::Byte*)[data bytes]; - Ice::Byte* end = (Ice::Byte*)[data bytes] + [data length]; - Ice::InputStreamPtr is = Ice::createInputStream(com, std::make_pair(start, end), [e encodingVersion]); - if(is) - { - return [ICEInputStream localObjectWithCxxObject:is.get()]; - } - else - { - return nil; - } - } - catch(const std::exception& ex) - { - nsex = toObjCException(ex); - } - @throw nsex; - return nil; // Keep the compiler happy. -} - -+(id<ICEInputStream>) wrapInputStream:(id<ICECommunicator>)communicator data:(NSData*)data -{ - NSException* nsex = nil; - try - { - Ice::CommunicatorPtr com = [(ICECommunicator*)communicator communicator]; - Ice::Byte* start = (Ice::Byte*)[data bytes]; - Ice::Byte* end = (Ice::Byte*)[data bytes] + [data length]; - Ice::InputStreamPtr is = Ice::wrapInputStream(com, std::make_pair(start, end)); - if(is) - { - return [ICEInputStream localObjectWithCxxObject:is.get()]; - } - else - { - return nil; - } - } - catch(const std::exception& ex) - { - nsex = toObjCException(ex); - } - @throw nsex; - return nil; // Keep the compiler happy. -} - -+(id<ICEInputStream>) wrapInputStream:(id<ICECommunicator>)c data:(NSData*)data encoding:(ICEEncodingVersion*)e -{ - NSException* nsex = nil; - try - { - Ice::CommunicatorPtr com = [(ICECommunicator*)c communicator]; - Ice::Byte* start = (Ice::Byte*)[data bytes]; - Ice::Byte* end = (Ice::Byte*)[data bytes] + [data length]; - Ice::InputStreamPtr is = Ice::wrapInputStream(com, std::make_pair(start, end), [e encodingVersion]); - if(is) - { - return [ICEInputStream localObjectWithCxxObject:is.get()]; - } - else - { - return nil; - } + return [[[ICEInputStream alloc] initWithCommunicator:c data:data encoding:e] autorelease]; } catch(const std::exception& ex) { @@ -509,16 +435,7 @@ private: NSException* nsex = nil; try { - Ice::CommunicatorPtr com = [(ICECommunicator*)communicator communicator]; - Ice::OutputStreamPtr os = Ice::createOutputStream(com); - if(os) - { - return [ICEOutputStream localObjectWithCxxObject:os.get()]; - } - else - { - return nil; - } + return [[[ICEOutputStream alloc] initWithCommunicator:communicator encoding:nil] autorelease]; } catch(const std::exception& ex) { @@ -533,16 +450,7 @@ private: NSException* nsex = nil; try { - Ice::CommunicatorPtr com = [(ICECommunicator*)communicator communicator]; - Ice::OutputStreamPtr os = Ice::createOutputStream(com, [encoding encodingVersion]); - if(os) - { - return [ICEOutputStream localObjectWithCxxObject:os.get()]; - } - else - { - return nil; - } + return [[[ICEOutputStream alloc] initWithCommunicator:communicator encoding:encoding] autorelease]; } catch(const std::exception& ex) { diff --git a/objective-c/src/Ice/Makefile b/objective-c/src/Ice/Makefile index cd1c5e0b9fc..10879dc6774 100644 --- a/objective-c/src/Ice/Makefile +++ b/objective-c/src/Ice/Makefile @@ -74,6 +74,7 @@ OBJCXX_OBJS = BatchRequestInterceptor.o \ Stream.o \ SlicedData.o \ Util.o \ + ValueFactoryI.o \ VersionI.o OBJS := $(OBJC_OBJS) $(OBJCXX_OBJS) diff --git a/objective-c/src/Ice/Object.mm b/objective-c/src/Ice/Object.mm index 3f571a7b084..466791070b3 100644 --- a/objective-c/src/Ice/Object.mm +++ b/objective-c/src/Ice/Object.mm @@ -25,7 +25,7 @@ namespace { -std::map<Ice::Object*, ICEObjectWrapper*> cachedObjects; +std::map<Ice::Object*, ICEServantWrapper*> cachedObjects; NSString* operationModeToString(ICEOperationMode mode) @@ -46,7 +46,7 @@ operationModeToString(ICEOperationMode mode) } } -class ObjectI : public IceObjC::ObjectWrapper, public Ice::BlobjectArrayAsync +class ObjectI : public IceObjC::ServantWrapper, public Ice::BlobjectArrayAsync { public: @@ -56,7 +56,7 @@ public: const std::pair<const Ice::Byte*, const Ice::Byte*>&, const Ice::Current&); - virtual ICEObject* getObject() + virtual ICEObject* getServant() { return _object; } @@ -78,7 +78,7 @@ private: ICEServant* _object; }; -class BlobjectI : public IceObjC::ObjectWrapper, public Ice::BlobjectArrayAsync +class BlobjectI : public IceObjC::ServantWrapper, public Ice::BlobjectArrayAsync { public: @@ -88,7 +88,7 @@ public: const std::pair<const Ice::Byte*, const Ice::Byte*>&, const Ice::Current&); - virtual ICEObject* getObject() + virtual ICEObject* getServant() { return _blobject; } @@ -117,19 +117,12 @@ ObjectI::ObjectI(ICEServant* object) : _object(object) void ObjectI::ice_invoke_async(const Ice::AMD_Object_ice_invokePtr& cb, - const std::pair<const Ice::Byte*, const Ice::Byte*>& inParams, - const Ice::Current& current) + const std::pair<const Ice::Byte*, const Ice::Byte*>& inParams, + const Ice::Current& current) { - ICEInputStream* is = nil; - ICEOutputStream* os = nil; - { - Ice::InputStreamPtr s = Ice::createInputStream(current.adapter->getCommunicator(), inParams); - is = [ICEInputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; - } - { - Ice::OutputStreamPtr s = Ice::createOutputStream(current.adapter->getCommunicator()); - os = [ICEOutputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; - } + Ice::Communicator* communicator = current.adapter->getCommunicator().get(); + ICEInputStream* is = [[ICEInputStream alloc] initWithCxxCommunicator:communicator data:inParams]; + ICEOutputStream* os = [[ICEOutputStream alloc] initWithCxxCommunicator:communicator]; NSException* exception = nil; BOOL ok = YES; // Keep the compiler happy @@ -157,11 +150,8 @@ ObjectI::ice_invoke_async(const Ice::AMD_Object_ice_invokePtr& cb, rethrowCxxException(exception, true); // True = release the exception. } - std::vector<Ice::Byte> outParams; - [os os]->finished(outParams); + cb->ice_response(ok, [os os]->finished()); [os release]; - - cb->ice_response(ok, std::make_pair(&outParams[0], &outParams[0] + outParams.size())); } BlobjectI::BlobjectI(ICEBlobject* blobject) : _blobject(blobject), _target([blobject target__]) @@ -170,8 +160,8 @@ BlobjectI::BlobjectI(ICEBlobject* blobject) : _blobject(blobject), _target([blob void BlobjectI::ice_invoke_async(const Ice::AMD_Object_ice_invokePtr& cb, - const std::pair<const Ice::Byte*, const Ice::Byte*>& inEncaps, - const Ice::Current& current) + const std::pair<const Ice::Byte*, const Ice::Byte*>& inEncaps, + const Ice::Current& current) { NSException* exception = nil; BOOL ok = YES; // Keep the compiler happy. @@ -412,7 +402,7 @@ static NSString* ICEObject_all__[4] = { if(object__) { - delete static_cast<IceObjC::ObjectWrapper*>(object__); + delete static_cast<IceObjC::ServantWrapper*>(object__); object__ = 0; } [delegate__ release]; @@ -570,10 +560,10 @@ static NSString* ICEObject_all__[4] = // reference to the object (without this, servants added to the object adapter // couldn't be retained or released easily). // - object__ = static_cast<IceObjC::ObjectWrapper*>(new ObjectI(self)); + object__ = static_cast<IceObjC::ServantWrapper*>(new ObjectI(self)); } } - return static_cast<IceObjC::ObjectWrapper*>(object__); + return static_cast<IceObjC::ServantWrapper*>(object__); } @end @@ -592,14 +582,14 @@ static NSString* ICEObject_all__[4] = // reference to the object (without this, servants added to the object adapter // couldn't be retained or released easily). // - object__ = static_cast<IceObjC::ObjectWrapper*>(new BlobjectI(self)); + object__ = static_cast<IceObjC::ServantWrapper*>(new BlobjectI(self)); } } - return static_cast<IceObjC::ObjectWrapper*>(object__); + return static_cast<IceObjC::ServantWrapper*>(object__); } @end -@implementation ICEObjectWrapper +@implementation ICEServantWrapper -(id) initWithCxxObject:(Ice::Object*)arg { self = [super init]; @@ -620,18 +610,18 @@ static NSString* ICEObject_all__[4] = object__->__decRef(); [super dealloc]; } -+(id) objectWrapperWithCxxObject:(Ice::Object*)arg ++(id) servantWrapperWithCxxObject:(Ice::Object*)arg { - @synchronized([ICEObjectWrapper class]) + @synchronized([ICEServantWrapper class]) { - std::map<Ice::Object*, ICEObjectWrapper*>::const_iterator p = cachedObjects.find(arg); + std::map<Ice::Object*, ICEServantWrapper*>::const_iterator p = cachedObjects.find(arg); if(p != cachedObjects.end()) { return [p->second retain]; } else { - return [[(ICEObjectWrapper*)[self alloc] initWithCxxObject:arg] autorelease]; + return [[(ICEServantWrapper*)[self alloc] initWithCxxObject:arg] autorelease]; } } } @@ -642,7 +632,7 @@ static NSString* ICEObject_all__[4] = } -(oneway void) release { - @synchronized([ICEObjectWrapper class]) + @synchronized([ICEServantWrapper class]) { if(NSDecrementExtraRefCountWasZero(self)) { diff --git a/objective-c/src/Ice/ObjectAdapterI.mm b/objective-c/src/Ice/ObjectAdapterI.mm index 8038bdd4f7b..911cec91c07 100644 --- a/objective-c/src/Ice/ObjectAdapterI.mm +++ b/objective-c/src/Ice/ObjectAdapterI.mm @@ -23,7 +23,7 @@ #include <Ice/Locator.h> #include <Ice/ServantLocator.h> -#include <Ice/Stream.h> +#include <Ice/OutputStream.h> namespace { @@ -88,12 +88,11 @@ private: }; typedef IceUtil::Handle<Cookie> CookiePtr; -class ExceptionWriter : public Ice::UserExceptionWriter +class ExceptionWriter : public Ice::UserException { public: - ExceptionWriter(const Ice::CommunicatorPtr& communicator, ICEUserException* ex) : - Ice::UserExceptionWriter(communicator), _ex(ex) + ExceptionWriter(ICEUserException* ex) : _ex(ex) { } @@ -103,15 +102,21 @@ public: } void - write(const Ice::OutputStreamPtr& s) const + __write(Ice::OutputStream* s) const { - ICEOutputStream* os = [ICEOutputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; + ICEOutputStream* os = [[ICEOutputStream alloc] initWithCxxStream:s]; [_ex write__:os]; [os release]; } + void + __read(Ice::InputStream*) + { + assert(false); + } + bool - usesClasses() const + __usesClasses() const { return [_ex usesClasses__]; } @@ -134,6 +139,11 @@ public: throw *this; } +protected: + + virtual void __writeImpl(Ice::OutputStream*) const {} + virtual void __readImpl(Ice::InputStream*) {} + private: ICEUserException* _ex; @@ -184,7 +194,7 @@ public: { if([ex isKindOfClass:[ICEUserException class]]) { - throw ExceptionWriter(current.adapter->getCommunicator(), (ICEUserException*)ex); + throw ExceptionWriter((ICEUserException*)ex); } rethrowCxxException(ex, true); // True = release the exception. } @@ -215,7 +225,7 @@ public: { if([ex isKindOfClass:[ICEUserException class]]) { - throw ExceptionWriter(current.adapter->getCommunicator(), (ICEUserException*)ex); + throw ExceptionWriter((ICEUserException*)ex); } rethrowCxxException(ex, true); // True = release the exception. } diff --git a/objective-c/src/Ice/ObjectI.h b/objective-c/src/Ice/ObjectI.h index 87e9d01bd7a..672514a7a15 100644 --- a/objective-c/src/Ice/ObjectI.h +++ b/objective-c/src/Ice/ObjectI.h @@ -21,14 +21,14 @@ namespace IceObjC { -class ObjectWrapper : virtual public Ice::Object +class ServantWrapper : virtual public Ice::Object { public: - virtual ~ObjectWrapper() { } - virtual ICEObject* getObject() = 0; + virtual ~ServantWrapper() { } + virtual ICEObject* getServant() = 0; }; -typedef IceUtil::Handle<ObjectWrapper> ObjectWrapperPtr; +typedef IceUtil::Handle<ServantWrapper> ServantWrapperPtr; }; @@ -36,9 +36,9 @@ typedef IceUtil::Handle<ObjectWrapper> ObjectWrapperPtr; -(Ice::Object*) object__; @end -@interface ICEObjectWrapper : ICEObject +@interface ICEServantWrapper : ICEObject { Ice::Object* object__; } -+(id) objectWrapperWithCxxObject:(Ice::Object*)arg; ++(id) servantWrapperWithCxxObject:(Ice::Object*)arg; @end diff --git a/objective-c/src/Ice/PropertiesI.h b/objective-c/src/Ice/PropertiesI.h index c7d54756cbc..8ceb643e235 100644 --- a/objective-c/src/Ice/PropertiesI.h +++ b/objective-c/src/Ice/PropertiesI.h @@ -24,7 +24,7 @@ -(Ice::Properties*)properties; @end -@interface ICENativePropertiesAdmin : ICEObjectWrapper<ICENativePropertiesAdmin> +@interface ICENativePropertiesAdmin : ICEServantWrapper<ICENativePropertiesAdmin> { IceUtil::Mutex mutex_; std::vector<Ice::PropertiesAdminUpdateCallbackPtr> callbacks_; diff --git a/objective-c/src/Ice/Proxy.mm b/objective-c/src/Ice/Proxy.mm index fbc246f9dc9..d8473dd84f3 100644 --- a/objective-c/src/Ice/Proxy.mm +++ b/objective-c/src/Ice/Proxy.mm @@ -45,10 +45,12 @@ class BeginInvokeAsyncCallback : public IceUtil::Shared { public: -BeginInvokeAsyncCallback(void (^completed)(id<ICEInputStream>, BOOL), +BeginInvokeAsyncCallback(const Ice::CommunicatorPtr& communicator, + void (^completed)(id<ICEInputStream>, BOOL), void (^exception)(ICEException*), void (^sent)(BOOL), BOOL returnsData) : + _communicator(communicator), _completed(Block_copy(completed)), _exception(Block_copy(exception)), _sent(Block_copy(sent)), @@ -63,41 +65,36 @@ virtual ~BeginInvokeAsyncCallback() Block_release(_sent); } -void completed(const Ice::AsyncResultPtr& result) +void response(bool ok, const std::pair<const Ice::Byte*, const Ice::Byte*>& outParams) { - BOOL ok = YES; // Keep the compiler happy. id<ICEInputStream> is = nil; NSException* nsex = nil; - Ice::ObjectPrx proxy = result->getProxy(); try { - std::vector<Ice::Byte> outParams; - ok = proxy->end_ice_invoke(outParams, result); if(_returnsData) { - Ice::InputStreamPtr s = Ice::createInputStream(proxy->ice_getCommunicator(), outParams); - is = [ICEInputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; + is = [[ICEInputStream alloc] initWithCxxCommunicator:_communicator.get() data:outParams]; } - else if(!outParams.empty()) + else if(outParams.first != outParams.second) { if(ok) { - if(outParams.size() != 6) + if((outParams.second - outParams.first) != 6) { throw Ice::EncapsulationException(__FILE__, __LINE__); } } else { - Ice::InputStreamPtr s = Ice::createInputStream(proxy->ice_getCommunicator(), outParams); + Ice::InputStream s(_communicator.get(), outParams); try { - s->startEncapsulation(); - s->throwException(); + s.startEncapsulation(); + s.throwException(); } catch(const Ice::UserException& ex) { - s->endEncapsulation(); + s.endEncapsulation(); throw Ice::UnknownUserException(__FILE__, __LINE__, ex.ice_id()); } } @@ -152,7 +149,38 @@ void completed(const Ice::AsyncResultPtr& result) } } -void sent(const Ice::AsyncResultPtr& result) +void exception(const Ice::Exception& ex) +{ + NSException* exception = nil; + @autoreleasepool + { + @try + { + @throw toObjCException(ex); + } + @catch(ICEException* ex) + { + if(_exception) + { + @try + { + _exception(ex); + } + @catch(id e) + { + exception = [e retain]; + } + } + return; + } + } + if(exception != nil) + { + rethrowCxxException(exception, true); // True = release the exception. + } +} + +void sent(bool sentSynchronously) { if(!_sent) { @@ -164,7 +192,7 @@ void sent(const Ice::AsyncResultPtr& result) { @try { - _sent(result->sentSynchronously()); + _sent(sentSynchronously); } @catch(id e) { @@ -180,6 +208,7 @@ void sent(const Ice::AsyncResultPtr& result) private: +const Ice::CommunicatorPtr _communicator; void (^_completed)(id<ICEInputStream>, BOOL); void (^_exception)(ICEException*); void (^_sent)(BOOL); @@ -444,8 +473,7 @@ BOOL _returnsData; NSException* nsex = nil; try { - Ice::OutputStreamPtr os = Ice::createOutputStream(OBJECTPRX->ice_getCommunicator()); - return [ICEOutputStream localObjectWithCxxObjectNoAutoRelease:os.get()]; + return [[ICEOutputStream alloc] initWithCxxCommunicator:OBJECTPRX->ice_getCommunicator().get()]; } catch(const std::exception& ex) { @@ -507,8 +535,6 @@ BOOL _returnsData; } } - BOOL ok = YES; // Keep the compiler happy. - ICEInputStream<ICEInputStream>* is = nil; NSException* nsex = nil; try { @@ -521,6 +547,7 @@ BOOL _returnsData; os = nil; } + BOOL ok = YES; // Keep the compiler happy. std::vector<Ice::Byte> outParams; if(context != nil) { @@ -535,8 +562,22 @@ BOOL _returnsData; if(unmarshal) { - Ice::InputStreamPtr s = Ice::createInputStream(OBJECTPRX->ice_getCommunicator(), outParams); - is = [ICEInputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; + if(outParams.empty()) + { + throw Ice::EncapsulationException(__FILE__, __LINE__); + } + std::pair<const Ice::Byte*, const Ice::Byte*> p(&outParams[0], &outParams[0] + outParams.size()); + ICEInputStream<ICEInputStream>* is; + is = [[ICEInputStream alloc] initWithCxxCommunicator:OBJECTPRX->ice_getCommunicator().get() data:p]; + @try + { + unmarshal(is, ok); + } + @catch(id e) + { + nsex = [e retain]; + } + [is release]; } else if(!outParams.empty()) { @@ -549,15 +590,15 @@ BOOL _returnsData; } else { - Ice::InputStreamPtr s = Ice::createInputStream(OBJECTPRX->ice_getCommunicator(), outParams); + Ice::InputStream s(OBJECTPRX->ice_getCommunicator(), outParams); try { - s->startEncapsulation(); - s->throwException(); + s.startEncapsulation(); + s.throwException(); } catch(const Ice::UserException& ex) { - s->endEncapsulation(); + s.endEncapsulation(); throw Ice::UnknownUserException(__FILE__, __LINE__, ex.ice_id()); } } @@ -570,11 +611,6 @@ BOOL _returnsData; [os release]; os = nil; } - if(is != nil) - { - [is release]; - is = nil; - } nsex = toObjCException(ex); } if(nsex != nil) @@ -583,17 +619,6 @@ BOOL _returnsData; } NSAssert(os == nil, @"output stream not cleared"); - if(is) - { - @try - { - unmarshal(is, ok); - } - @finally - { - [is release]; - } - } } -(id<ICEAsyncResult>) begin_invoke__:(NSString*)operation @@ -742,9 +767,12 @@ BOOL _returnsData; os = nil; } - Ice::CallbackPtr cb = Ice::newCallback(new BeginInvokeAsyncCallback(completed, exception, sent, returnsData), - &BeginInvokeAsyncCallback::completed, - &BeginInvokeAsyncCallback::sent); + Ice::Callback_Object_ice_invokePtr cb = Ice::newCallback_Object_ice_invoke( + new BeginInvokeAsyncCallback(OBJECTPRX->ice_getCommunicator(), completed, exception, sent, returnsData), + &BeginInvokeAsyncCallback::response, + &BeginInvokeAsyncCallback::exception, + &BeginInvokeAsyncCallback::sent); + Ice::AsyncResultPtr r; if(context != nil) { @@ -843,34 +871,33 @@ BOOL _returnsData; ICEInputStream* is = nil; try { - std::vector<Ice::Byte> outParams; - ok = OBJECTPRX->end_ice_invoke(outParams, [result asyncResult__]); + std::pair<const Ice::Byte*, const Ice::Byte*> outParams; + ok = OBJECTPRX->___end_ice_invoke(outParams, [result asyncResult__]); if(unmarshal) { - Ice::InputStreamPtr s = Ice::createInputStream(OBJECTPRX->ice_getCommunicator(), outParams); - is = [ICEInputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; + is = [[ICEInputStream alloc] initWithCxxCommunicator:OBJECTPRX->ice_getCommunicator().get() data:outParams]; } - else if(!outParams.empty()) + else if(outParams.first != outParams.second) { if(ok) { - if(outParams.size() != 6) + if((outParams.second - outParams.first) != 6) { throw Ice::EncapsulationException(__FILE__, __LINE__); } } else { - Ice::InputStreamPtr s = Ice::createInputStream(OBJECTPRX->ice_getCommunicator(), outParams); + Ice::InputStream s(OBJECTPRX->ice_getCommunicator(), outParams); try { - s->startEncapsulation(); - s->throwException(); + s.startEncapsulation(); + s.throwException(); } catch(const Ice::UserException& ex) { - s->endEncapsulation(); + s.endEncapsulation(); throw Ice::UnknownUserException(__FILE__, __LINE__, ex.ice_id()); } } @@ -1294,15 +1321,7 @@ BOOL _returnsData; inEncaps:(NSData*)inEncaps outEncaps:(NSMutableData**)outEncaps { - __block BOOL ret__; - cppCall(^ { - std::pair<const Ice::Byte*, const Ice::Byte*> inP((ICEByte*)[inEncaps bytes], - (ICEByte*)[inEncaps bytes] + [inEncaps length]); - std::vector<Ice::Byte> outP; - ret__ = OBJECTPRX->ice_invoke(fromNSString(operation), (Ice::OperationMode)mode, inP, outP); - *outEncaps = [NSMutableData dataWithBytes:&outP[0] length:outP.size()]; - }); - return ret__; + return [self end_ice_invoke:outEncaps result:[self begin_ice_invoke:operation mode:mode inEncaps:inEncaps]]; } -(BOOL) ice_invoke:(NSString*)operation @@ -1311,16 +1330,10 @@ BOOL _returnsData; outEncaps:(NSMutableData**)outEncaps context:(ICEContext*)context { - __block BOOL ret__; - cppCall(^(const Ice::Context& ctx) { - std::pair<const Ice::Byte*, const Ice::Byte*> inP((ICEByte*)[inEncaps bytes], - (ICEByte*)[inEncaps bytes] + [inEncaps length]); - std::vector<Ice::Byte> outP; - ret__ = OBJECTPRX->ice_invoke(fromNSString(operation), (Ice::OperationMode)mode, inP, outP, ctx); - *outEncaps = [NSMutableData dataWithBytes:&outP[0] length:outP.size()]; - }, context); - return ret__; + return [self end_ice_invoke:outEncaps + result:[self begin_ice_invoke:operation mode:mode inEncaps:inEncaps context:context]]; } + -(id<ICEAsyncResult>) begin_ice_invoke:(NSString*)operation mode:(ICEOperationMode)mode inEncaps:(NSData*)inEncaps { return beginCppCall(^(Ice::AsyncResultPtr& result) @@ -1384,10 +1397,10 @@ BOOL _returnsData; ^(const Ice::AsyncResultPtr& result) { std::pair<const ::Ice::Byte*, const ::Ice::Byte*> outP; BOOL ret__ = OBJECTPRX->___end_ice_invoke(outP, result); - NSMutableData* outEncaps = - [NSMutableData dataWithBytes:outP.first length:(outP.second - outP.first)]; if(response) { + NSMutableData* outEncaps = + [NSMutableData dataWithBytes:outP.first length:(outP.second - outP.first)]; response(ret__, outEncaps); } }, @@ -1415,10 +1428,10 @@ BOOL _returnsData; ^(const Ice::AsyncResultPtr& result) { std::pair<const ::Ice::Byte*, const ::Ice::Byte*> outP; BOOL ret__ = OBJECTPRX->___end_ice_invoke(outP, result); - NSMutableData* outEncaps = - [NSMutableData dataWithBytes:outP.first length:(outP.second - outP.first)]; if(response) { + NSMutableData* outEncaps = + [NSMutableData dataWithBytes:outP.first length:(outP.second - outP.first)]; response(ret__, outEncaps); } }, @@ -1430,9 +1443,9 @@ BOOL _returnsData; __block BOOL ret__; endCppCall(^(const Ice::AsyncResultPtr& r) { - std::vector<Ice::Byte> outP; - ret__ = OBJECTPRX->end_ice_invoke(outP, r); - *outEncaps = [NSMutableData dataWithBytes:&outP[0] length:outP.size()]; + std::pair<const ::Ice::Byte*, const ::Ice::Byte*> outP; + ret__ = OBJECTPRX->___end_ice_invoke(outP, r); + *outEncaps = [NSMutableData dataWithBytes:outP.first length:(outP.second - outP.first)]; }, result); return ret__; } diff --git a/objective-c/src/Ice/Stream.mm b/objective-c/src/Ice/Stream.mm index 9654fea4cc7..93b9d2260ce 100644 --- a/objective-c/src/Ice/Stream.mm +++ b/objective-c/src/Ice/Stream.mm @@ -18,7 +18,8 @@ #import <objc/Ice/LocalException.h> -#include <Ice/Stream.h> +#include <Ice/OutputStream.h> +#include <Ice/InputStream.h> #include <Ice/SlicedData.h> #import <objc/runtime.h> @@ -28,20 +29,28 @@ ICE_API id ICENone = nil; namespace IceObjC { -class ObjectWriter : public Ice::ObjectWriter +class ValueWrapper : public Ice::Object { public: - ObjectWriter(ICEObject* obj, ICEOutputStream* stream) : _obj(obj), _stream(stream) + // We must explicitely CFRetain/CFRelease so that the garbage + // collector does not trash the _obj. + ValueWrapper(ICEObject* obj) : _obj(obj) { + CFRetain(_obj); + } + + virtual ~ValueWrapper() + { + CFRelease(_obj); } virtual void - write(const Ice::OutputStreamPtr& stream) const + __write(Ice::OutputStream* stream) const { @try { - [_obj write__:_stream]; + [_obj write__:static_cast<ICEOutputStream*>(stream->getClosure())]; } @catch(id ex) { @@ -49,44 +58,12 @@ public: } } - virtual void ice_preMarshal() - { - [_obj ice_preMarshal]; - } - -private: - - ICEObject* _obj; - ICEOutputStream* _stream; -}; - -class ObjectReader : public Ice::ObjectReader -{ -public: - - // We must explicitely CFRetain/CFRelease so that the garbage - // collector does not trash the _obj. - ObjectReader(ICEObject* obj) : _obj(obj) - { - CFRetain(_obj); - } - - virtual ~ObjectReader() - { - CFRelease(_obj); - } - virtual void - read(const Ice::InputStreamPtr& stream) + __read(Ice::InputStream* stream) { @try { - // - // TODO: explain why calling getLocalObjectWithCxxObjectNoAutoRelease is safe here - // - ICEInputStream* is = [ICEInputStream getLocalObjectWithCxxObjectNoAutoRelease:stream.get()]; - assert(is != 0); - [_obj read__:is]; + [_obj read__:static_cast<ICEInputStream*>(stream->getClosure())]; } @catch(id ex) { @@ -94,10 +71,9 @@ public: } } - ICEObject* - getObject() + virtual void ice_preMarshal() { - return _obj; + [_obj ice_preMarshal]; } virtual void ice_postUnmarshal() @@ -105,20 +81,32 @@ public: [_obj ice_postUnmarshal]; } + ICEObject* + getValue() + { + return _obj; + } + private: ICEObject* _obj; }; -typedef IceUtil::Handle<ObjectReader> ObjectReaderPtr; +typedef IceUtil::Handle<ValueWrapper> ValueWrapperPtr; -class ReadObjectBase : public Ice::ReadObjectCallback +class ReadValueBase { public: - ReadObjectBase(Class expectedType) : _expectedType(expectedType) + ReadValueBase(Class expectedType) : _expectedType(expectedType) { } + virtual + ~ReadValueBase() + { + } + + virtual void invoke(const Ice::ValuePtr&) = 0; void checkType(ICEObject*); private: @@ -127,7 +115,15 @@ private: }; void -ReadObjectBase::checkType(ICEObject* o) +patchFunc(void* obj, const Ice::ValuePtr& value) +{ + ReadValueBase* reader = static_cast<ReadValueBase*>(obj); + reader->invoke(value); + delete reader; +} + +void +ReadValueBase::checkType(ICEObject* o) { if(o != nil && ![o isKindOfClass:_expectedType]) { @@ -144,23 +140,23 @@ ReadObjectBase::checkType(ICEObject* o) } } -class ReadObject : public ReadObjectBase +class ReadValue : public ReadValueBase { public: - ReadObject(ICEObject** addr, Class expectedType, bool autorelease) : - ReadObjectBase(expectedType), _addr(addr), _autorelease(autorelease) + ReadValue(ICEObject** addr, Class expectedType, bool autorelease) : + ReadValueBase(expectedType), _addr(addr), _autorelease(autorelease) { } virtual void - invoke(const Ice::ObjectPtr& obj) + invoke(const Ice::ValuePtr& obj) { @try { if(obj) { - ICEObject* o = ObjectReaderPtr::dynamicCast(obj)->getObject(); + ICEObject* o = ValueWrapperPtr::dynamicCast(obj)->getValue(); checkType(o); *_addr = [o retain]; if(_autorelease) @@ -185,23 +181,23 @@ private: bool _autorelease; }; -class ReadObjectAtIndex : public ReadObjectBase +class ReadValueAtIndex : public ReadValueBase { public: - ReadObjectAtIndex(NSMutableArray* array, ICEInt index, Class expectedType) : - ReadObjectBase(expectedType), _array(array), _index(index) + ReadValueAtIndex(NSMutableArray* array, ICEInt index, Class expectedType) : + ReadValueBase(expectedType), _array(array), _index(index) { } virtual void - invoke(const Ice::ObjectPtr& obj) + invoke(const Ice::ValuePtr& obj) { @try { if(obj) { - ICEObject* o = ObjectReaderPtr::dynamicCast(obj)->getObject(); + ICEObject* o = ValueWrapperPtr::dynamicCast(obj)->getValue(); checkType(o); [_array replaceObjectAtIndex:_index withObject:o]; } @@ -218,31 +214,31 @@ private: ICEInt _index; }; -class ReadObjectForKey : public ReadObjectBase +class ReadValueForKey : public ReadValueBase { public: // We must explicitely CFRetain/CFRelease so that the garbage // collector does not trash the _key. - ReadObjectForKey(NSMutableDictionary* dict, id key, Class expectedType) : - ReadObjectBase(expectedType), _dict(dict), _key(key) + ReadValueForKey(NSMutableDictionary* dict, id key, Class expectedType) : + ReadValueBase(expectedType), _dict(dict), _key(key) { CFRetain(_key); } - virtual ~ReadObjectForKey() + virtual ~ReadValueForKey() { CFRelease(_key); } virtual void - invoke(const Ice::ObjectPtr& obj) + invoke(const Ice::ValuePtr& obj) { @try { if(obj) { - ICEObject* o = ObjectReaderPtr::dynamicCast(obj)->getObject(); + ICEObject* o = ValueWrapperPtr::dynamicCast(obj)->getValue(); checkType(o); [_dict setObject:o forKey:_key]; } @@ -263,20 +259,16 @@ private: id _key; }; -class ExceptionWriter : public Ice::UserExceptionWriter +class ExceptionWrapper : public Ice::UserException { public: - ExceptionWriter(const Ice::CommunicatorPtr& communicator, ICEOutputStream* stream, ICEUserException* ex) : - Ice::UserExceptionWriter(communicator), - _stream(stream), - _ex(ex) + ExceptionWrapper(ICEUserException* ex) : _ex(ex) { - } virtual bool - usesClasses() const + __usesClasses() const { return [_ex usesClasses__]; } @@ -290,7 +282,7 @@ public: virtual Ice::UserException* ice_clone() const { - return new ExceptionWriter(*this); + return new ExceptionWrapper(*this); } virtual void @@ -300,97 +292,55 @@ public: } virtual void - write(const Ice::OutputStreamPtr& stream) const - { - [_ex write__:_stream]; - } - -private: - - ICEOutputStream* _stream; - ICEUserException* _ex; -}; - -class ExceptionReader : public Ice::UserExceptionReader -{ -public: - - ExceptionReader(const Ice::CommunicatorPtr& communicator, ICEInputStream* stream, ICEUserException* ex) : - Ice::UserExceptionReader(communicator), - _stream(stream), - _ex(ex) - { - } - - void - read(const Ice::InputStreamPtr& stream) const - { - [_ex read__:_stream]; - } - - virtual std::string - ice_id() const - { - return [[_ex ice_id] UTF8String]; - } - - virtual bool - usesClasses() const - { - return [_ex usesClasses__]; - } - - virtual Ice::UserException* - ice_clone() const + __write(Ice::OutputStream* s) const { - assert(false); - return 0; + [_ex write__:static_cast<ICEOutputStream*>(s->getClosure())]; } virtual void - ice_throw() const + __read(Ice::InputStream* s) { - throw *this; + [_ex read__:static_cast<ICEInputStream*>(s->getClosure())]; } - ICEUserException* getException() const + ICEUserException* + getException() const { return _ex; } +protected: + + virtual void __writeImpl(Ice::OutputStream*) const {} + virtual void __readImpl(Ice::InputStream*) {} + private: - ICEInputStream* _stream; ICEUserException* _ex; }; -class UserExceptionReaderFactoryI : public Ice::UserExceptionReaderFactory +class UserExceptionFactoryI : public Ice::UserExceptionFactory { public: - UserExceptionReaderFactoryI(const Ice::CommunicatorPtr& communicator, ICEInputStream* is) : - _communicator(communicator), - _is(is) + UserExceptionFactoryI(NSDictionary* prefixTable) : _prefixTable(prefixTable) { } - virtual void createAndThrow(const std::string& typeId) const + virtual void createAndThrow(const std::string& typeId) { ICEUserException* ex = nil; - std::string objcId = toObjCSliceId(typeId, - [[ICECommunicator localObjectWithCxxObject:_communicator.get()] getPrefixTable]); - Class c = objc_lookUpClass(objcId.c_str()); + Class c = objc_lookUpClass(toObjCSliceId(typeId, _prefixTable).c_str()); if(c != nil) { ex = [[c alloc] init]; - throw ExceptionReader(_communicator, _is, ex); + throw ExceptionWrapper(ex); } } private: - Ice::CommunicatorPtr _communicator; - ICEInputStream* _is; + NSDictionary* _prefixTable; }; } @@ -428,20 +378,55 @@ private: @end @implementation ICEInputStream --(id) initWithCxxObject:(IceUtil::Shared*)cxxObject +-(id) initWithCxxCommunicator:(Ice::Communicator*)com data:(const std::pair<const Byte*, const Byte*>&)data +{ + self = [super init]; + if(!self) + { + return nil; + } + Ice::InputStream(com, data).swap(stream_); + is_ = &stream_; + is_->setClosure(self); + data_ = nil; + prefixTable_ = [[[ICECommunicator localObjectWithCxxObject:com] getPrefixTable] retain]; + return self; +} + +-(id) initWithCommunicator:(id<ICECommunicator>)communicator data:(NSData*)data encoding:(ICEEncodingVersion*)e { - self = [super initWithCxxObject:cxxObject]; + self = [super init]; if(!self) { return nil; } - is_ = dynamic_cast<Ice::InputStream*>(cxxObject); + ICECommunicator* com = (ICECommunicator*)communicator; + std::pair<const Ice::Byte*, const Ice::Byte*> p((Ice::Byte*)[data bytes], (Ice::Byte*)[data bytes] + [data length]); + if(e != nil) + { + Ice::InputStream([com communicator], [e encodingVersion], p).swap(stream_); + } + else + { + Ice::InputStream([com communicator], p).swap(stream_); + } + is_ = &stream_; + is_->setClosure(self); + data_ = [data retain]; + prefixTable_ = [[com getPrefixTable] retain]; return self; } +-(void) dealloc +{ + [data_ release]; + [prefixTable_ release]; + [super dealloc]; +} + +(Ice::Object*)createObjectReader:(ICEObject*)obj { - return new IceObjC::ObjectReader(obj); + return new IceObjC::ValueWrapper(obj); } -(Ice::InputStream*) is @@ -451,17 +436,12 @@ private: // @protocol ICEInputStream methods --(id<ICECommunicator>) communicator -{ - return [ICECommunicator localObjectWithCxxObject:is_->communicator().get()]; -} - --(void) sliceObjects:(BOOL)b +-(void) setSliceObjects:(BOOL)b { NSException* nsex = nil; try { - is_->sliceObjects(b); + is_->setSliceObjects(b); } catch(const std::exception& ex) { @@ -934,15 +914,9 @@ private: NSException* nsex = nil; try { - Ice::ObjectPrx p = is_->readProxy(); - if(!p) - { - return nil; - } - else - { - return [[type alloc] initWithObjectPrx__:p]; - } + Ice::ObjectPrx p; + is_->read(p); + return p ? [[type alloc] initWithObjectPrx__:p] : nil; } catch(const std::exception& ex) { @@ -960,7 +934,7 @@ private: NSException* nsex = nil; try { - is_->readObject(new IceObjC::ReadObject(object, type, true)); + is_->read(IceObjC::patchFunc, new IceObjC::ReadValue(object, type, true)); } catch(const std::exception& ex) { @@ -980,7 +954,7 @@ private: NSException* nsex = nil; try { - is_->readObject(new IceObjC::ReadObject(object, type, false)); + is_->read(IceObjC::patchFunc, new IceObjC::ReadValue(object, type, false)); } catch(const std::exception& ex) { @@ -1009,7 +983,7 @@ private: for(i = 0; i < sz; i++) { [arr addObject:null]; - is_->readObject(new IceObjC::ReadObjectAtIndex(arr, i, type)); + is_->read(IceObjC::patchFunc, new IceObjC::ReadValueAtIndex(arr, i, type)); } } catch(const std::exception& ex) @@ -1049,7 +1023,7 @@ private: NSException* nsex = nil; try { - is_->readObject(new IceObjC::ReadObjectForKey(dictionary, key, type)); + is_->read(IceObjC::patchFunc, new IceObjC::ReadValueForKey(dictionary, key, type)); } catch(const std::exception& ex) { @@ -1145,7 +1119,7 @@ private: NSException* nsex = nil; try { - return is_->readOptional(tag, static_cast<Ice::OptionalFormat>(format)); + return is_->readOpt(tag, static_cast<Ice::OptionalFormat>(format)); } catch(const std::exception& ex) { @@ -1164,13 +1138,11 @@ private: NSException* nsex = nil; try { - Ice::UserExceptionReaderFactoryPtr factory = - new IceObjC::UserExceptionReaderFactoryI(is_->communicator().get(), self); - is_->throwException(factory); + is_->throwException(new IceObjC::UserExceptionFactoryI(prefixTable_)); } - catch(const IceObjC::ExceptionReader& reader) + catch(const IceObjC::ExceptionWrapper& e) { - ex = reader.getException(); + ex = e.getException(); @throw [ex autorelease]; // NOTE: exceptions are always auto-released, no need for the caller to do it. } catch(const std::exception& ex) @@ -1383,7 +1355,8 @@ private: NSException* nsex = nil; try { - is_->rewind(); + is_->pos(0); + is_->clear(); } catch(const std::exception& ex) { @@ -1395,6 +1368,7 @@ private: } } + -(void) skip:(ICEInt)sz { NSException* nsex = nil; @@ -1432,14 +1406,51 @@ private: @end @implementation ICEOutputStream --(id) initWithCxxObject:(IceUtil::Shared*)cxxObject +-(id) initWithCxxCommunicator:(Ice::Communicator*)com +{ + self = [super init]; + if(!self) + { + return nil; + } + Ice::OutputStream(com).swap(stream_); + os_ = &stream_; + os_->setClosure(self); + objectWriters_ = 0; + return self; +} + +-(id) initWithCxxStream:(Ice::OutputStream*)stream +{ + self = [super init]; + if(!self) + { + return nil; + } + os_ = stream; + os_->setClosure(self); + objectWriters_ = 0; + return self; +} + +-(id) initWithCommunicator:(id<ICECommunicator>)communicator encoding:(ICEEncodingVersion*)e { - self = [super initWithCxxObject:cxxObject]; + self = [super init]; if(!self) { return nil; } - os_ = dynamic_cast<Ice::OutputStream*>(cxxObject); + ICECommunicator* com = (ICECommunicator*)communicator; + if(e != nil) + { + Ice::OutputStream([com communicator], [e encodingVersion]).swap(stream_); + } + else + { + Ice::OutputStream([com communicator]).swap(stream_); + } + os_ = &stream_; + os_->setClosure(self); objectWriters_ = 0; return self; } @@ -1460,11 +1471,6 @@ private: // @protocol ICEOutputStream methods --(id<ICECommunicator>) communicator -{ - return [ICECommunicator localObjectWithCxxObject:os_->communicator().get()]; -} - -(void)writeBool:(BOOL)v { NSException* nsex = nil; @@ -1487,8 +1493,7 @@ private: NSException* nsex = nil; try { - v == nil ? os_->writeSize(0) - : os_->write((bool*)[v bytes], (bool*)[v bytes] + [v length] / sizeof(BOOL)); + v == nil ? os_->writeSize(0) : os_->write((bool*)[v bytes], (bool*)[v bytes] + [v length] / sizeof(BOOL)); } catch(const std::exception& ex) { @@ -1522,8 +1527,7 @@ private: NSException* nsex = nil; try { - v == nil ? os_->writeSize(0) - : os_->write((ICEByte*)[v bytes], (ICEByte*)[v bytes] + [v length]); + v == nil ? os_->writeSize(0) : os_->write((ICEByte*)[v bytes], (ICEByte*)[v bytes] + [v length]); } catch(const std::exception& ex) { @@ -1789,7 +1793,7 @@ private: NSException* nsex = nil; try { - return os_->writeOptional(tag, static_cast<Ice::OptionalFormat>(format)); + return os_->writeOpt(tag, static_cast<Ice::OptionalFormat>(format)); } catch(const std::exception& ex) { @@ -1899,7 +1903,7 @@ private: NSException* nsex = nil; try { - os_->writeProxy([(ICEObjectPrx*)v objectPrx__]); + os_->write(Ice::ObjectPrx([(ICEObjectPrx*)v objectPrx__])); } catch(const std::exception& ex) { @@ -1918,11 +1922,11 @@ private: { if(v == nil) { - os_->writeObject(0); + os_->write(Ice::ValuePtr()); } else { - os_->writeObject([self addObject:v]); + os_->write(Ice::ValuePtr([self addObject:v])); } } catch(const std::exception& ex) @@ -1975,7 +1979,7 @@ private: -(void) writeException:(ICEUserException*)ex { - IceObjC::ExceptionWriter writer(os_->communicator().get(), self, ex); + IceObjC::ExceptionWrapper writer(ex); os_->writeException(writer); } @@ -2206,7 +2210,8 @@ private: NSException* nsex = nil; try { - os_->reset(clearBuffer); + os_->clear(); + os_->resize(0); } catch(const std::exception& ex) { @@ -2221,23 +2226,23 @@ private: -(Ice::Object*) addObject:(ICEObject*)object { // - // Ice::ObjectWriter is a subclass of Ice::Object that wraps an Objective-C object for marshaling. + // Ice::ValueWrapper is a subclass of Ice::Object that wraps an Objective-C object for marshaling. // It is possible that this Objective-C object has already been marshaled, therefore we first must - // check the object map to see if this object is present. If so, we use the existing ObjectWriter, + // check the object map to see if this object is present. If so, we use the existing ValueWrapper, // otherwise we create a new one. // if(!objectWriters_) { - objectWriters_ = new std::map<ICEObject*, Ice::ObjectPtr>(); + objectWriters_ = new std::map<ICEObject*, Ice::ValuePtr>(); } - std::map<ICEObject*, Ice::ObjectPtr>::const_iterator p = objectWriters_->find(object); + std::map<ICEObject*, Ice::ValuePtr>::const_iterator p = objectWriters_->find(object); if(p != objectWriters_->end()) { return p->second.get(); } else { - Ice::ObjectWriterPtr writer = new IceObjC::ObjectWriter(object, self); + IceObjC::ValueWrapperPtr writer = new IceObjC::ValueWrapper(object); objectWriters_->insert(std::make_pair(object, writer)); return writer.get(); } @@ -2257,12 +2262,12 @@ private: info->hasOptionalMembers = (*p)->hasOptionalMembers; info->isLastSlice = (*p)->isLastSlice; - for(std::vector<Ice::ObjectPtr>::const_iterator q = (*p)->objects.begin(); q != (*p)->objects.end(); ++q) + for(std::vector<Ice::ValuePtr>::const_iterator q = (*p)->objects.begin(); q != (*p)->objects.end(); ++q) { if(*q) { - assert(IceObjC::ObjectReaderPtr::dynamicCast(*q)); - info->objects.push_back([self addObject:IceObjC::ObjectReaderPtr::dynamicCast(*q)->getObject()]); + assert(IceObjC::ValueWrapperPtr::dynamicCast(*q)); + info->objects.push_back([self addObject:IceObjC::ValueWrapperPtr::dynamicCast(*q)->getValue()]); } else { diff --git a/objective-c/src/Ice/StreamI.h b/objective-c/src/Ice/StreamI.h index 650e9c4eefb..564781c9f8d 100644 --- a/objective-c/src/Ice/StreamI.h +++ b/objective-c/src/Ice/StreamI.h @@ -10,20 +10,32 @@ #import <objc/Ice/Stream.h> #import <objc/Ice/LocalObject.h> -#include <Ice/Stream.h> +#include <Ice/InputStream.h> +#include <Ice/OutputStream.h> -@interface ICEInputStream : ICELocalObject<ICEInputStream> +@protocol ICECommunicator; + +@interface ICEInputStream : NSObject<ICEInputStream> { Ice::InputStream* is_; + Ice::InputStream stream_; + NSDictionary* prefixTable_; + NSData* data_; } +(Ice::Object*)createObjectReader:(ICEObject*)obj; +-initWithCxxCommunicator:(Ice::Communicator*)com data:(const std::pair<const Byte*, const Byte*>&)data; +-initWithCommunicator:(id<ICECommunicator>)com data:(NSData*)data encoding:(ICEEncodingVersion*)e; -(Ice::InputStream*) is; @end -@interface ICEOutputStream : ICELocalObject<ICEOutputStream> +@interface ICEOutputStream : NSObject<ICEOutputStream> { Ice::OutputStream* os_; + Ice::OutputStream stream_; std::map<ICEObject*, Ice::ObjectPtr>* objectWriters_; } +-initWithCxxCommunicator:(Ice::Communicator*)communicator; +-initWithCxxStream:(Ice::OutputStream*)stream; +-initWithCommunicator:(id<ICECommunicator>)com encoding:(ICEEncodingVersion*)e; -(Ice::OutputStream*) os; @end diff --git a/objective-c/src/Ice/Util.mm b/objective-c/src/Ice/Util.mm index 8ac8aa3f14b..27a555ff040 100644 --- a/objective-c/src/Ice/Util.mm +++ b/objective-c/src/Ice/Util.mm @@ -455,14 +455,14 @@ toObjC(const Ice::ObjectPtr& object) return nil; } - IceObjC::ObjectWrapperPtr wrapper = IceObjC::ObjectWrapperPtr::dynamicCast(object); + IceObjC::ServantWrapperPtr wrapper = IceObjC::ServantWrapperPtr::dynamicCast(object); if(wrapper) { // // Given object is an Objective-C servant wrapped into a C++ // object, return the wrapped Objective-C object. // - return [[wrapper->getObject() retain] autorelease]; + return [[wrapper->getServant() retain] autorelease]; } else if(Ice::NativePropertiesAdminPtr::dynamicCast(object)) { @@ -470,13 +470,13 @@ toObjC(const Ice::ObjectPtr& object) // Given object is a properties admin facet, return the // Objective-C wrapper. // - return [ICENativePropertiesAdmin objectWrapperWithCxxObject:object.get()]; + return [ICENativePropertiesAdmin servantWrapperWithCxxObject:object.get()]; } else { // // Given object is a C++ servant, return an Objective-C wrapper. // - return [ICEObjectWrapper objectWrapperWithCxxObject:object.get()]; + return [ICEServantWrapper servantWrapperWithCxxObject:object.get()]; } } diff --git a/objective-c/src/Ice/ValueFactoryI.h b/objective-c/src/Ice/ValueFactoryI.h new file mode 100644 index 00000000000..94fd250e015 --- /dev/null +++ b/objective-c/src/Ice/ValueFactoryI.h @@ -0,0 +1,19 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#import <objc/Ice/ValueFactory.h> + +#include <Ice/Communicator.h> + +@interface ICEValueFactoryManager : NSObject<ICEValueFactoryManager> +{ + NSMutableDictionary* valueFactories_; +} +-(id) init:(Ice::Communicator*)communicator prefixTable:(NSDictionary*)prefixTable; +@end diff --git a/objective-c/src/Ice/ValueFactoryI.mm b/objective-c/src/Ice/ValueFactoryI.mm new file mode 100644 index 00000000000..9468d009f0b --- /dev/null +++ b/objective-c/src/Ice/ValueFactoryI.mm @@ -0,0 +1,161 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#include <Ice/ObjectFactory.h> + +#import <ValueFactoryI.h> +#import <LocalObjectI.h> +#import <StreamI.h> +#import <Util.h> + +#import <objc/Ice/ObjectFactory.h> +#import <objc/Ice/SlicedData.h> + +#import <objc/runtime.h> + +namespace IceObjC +{ + +class UnknownSlicedValueFactoryI : public Ice::ValueFactory +{ +public: + + virtual Ice::ObjectPtr + create(const std::string&) + { + ICEUnknownSlicedObject* obj = [[ICEUnknownSlicedObject alloc] init]; + Ice::ObjectPtr o = [ICEInputStream createObjectReader:obj]; + [obj release]; + return o; + } +}; + +class ValueFactoryI : public Ice::ValueFactory +{ +public: + + // We must explicitely CFRetain/CFRelease so that the garbage + // collector does not trash the dictionaries. + ValueFactoryI(NSDictionary* factories, NSDictionary* prefixTable) : + _factories(factories), _prefixTable(prefixTable) + { + CFRetain(_factories); + CFRetain(_prefixTable); + } + + ~ValueFactoryI() + { + CFRelease(_factories); + CFRelease(_prefixTable); + } + + virtual Ice::ObjectPtr + create(const std::string& type) + { + NSString* sliceId = [[NSString alloc] initWithUTF8String:type.c_str()]; + @try + { + ICEValueFactory factory = nil; + @synchronized(_factories) + { + factory = [_factories objectForKey:sliceId]; + if(factory == nil) + { + factory = [_factories objectForKey:@""]; + } + } + + ICEObject* obj = nil; + if(factory != nil) + { + obj = [factory(sliceId) retain]; + } + + if(obj == nil) + { + std::string tId = toObjCSliceId(type, _prefixTable); + Class c = objc_lookUpClass(tId.c_str()); + if(c == nil) + { + return 0; // No value factory. + } + if([c isSubclassOfClass:[ICEObject class]]) + { + obj = (ICEObject*)[[c alloc] init]; + } + } + + Ice::ObjectPtr o; + if(obj != nil) + { + o = [ICEInputStream createObjectReader:obj]; + [obj release]; + } + return o; + } + @catch(id ex) + { + rethrowCxxException(ex); + } + @finally + { + [sliceId release]; + } + return nil; // Keep the compiler happy. + } + +private: + + NSDictionary* _factories; + NSDictionary* _prefixTable; +}; + +} + +@implementation ICEValueFactoryManager +-(id) init:(Ice::Communicator*)com prefixTable:(NSDictionary*)prefixTable +{ + self = [super init]; + if(!self) + { + return nil; + } + + valueFactories_ = [[NSMutableDictionary alloc] init]; + + com->getValueFactoryManager()->add(new IceObjC::UnknownSlicedValueFactoryI, "::Ice::Object"); + com->getValueFactoryManager()->add(new IceObjC::ValueFactoryI(valueFactories_, prefixTable), ""); + + return self; +} + +-(void) dealloc +{ + [valueFactories_ release]; + [super dealloc]; +} + +-(void) add:(ICEValueFactory)factory sliceId:(NSString*)sliceId +{ + @synchronized(valueFactories_) + { + [valueFactories_ setObject:ICE_AUTORELEASE([factory copy]) forKey:sliceId]; + } +} + +-(ICEValueFactory) find:(NSString*)sliceId +{ + @synchronized(valueFactories_) + { + return [valueFactories_ objectForKey:sliceId]; + } + return nil; // Keep the compiler happy. +} + +@end |