From 33a7d898ca8d40d02cbd6a5b7547b7fa3958c3cf Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Fri, 14 Sep 2007 19:44:30 +0000 Subject: [PATCH] Merge more cocoa changes from Andrew McGeachie Reviewed By: dreiss Test Plan: To be provided later by McGeachie. Compiler builds fine. Does not affect other libraries. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665259 13f79535-47bb-0310-9956-ffa450edef68 --- .../cpp/src/generate/t_cocoa_generator.cc | 39 +++++++++++----- compiler/cpp/src/generate/t_cocoa_generator.h | 14 +++--- lib/cocoa/TApplicationException.h | 8 +++- lib/cocoa/TBinaryProtocol.m | 45 +++++++++---------- lib/cocoa/TException.h | 10 ++++- lib/cocoa/TProtocol.h | 30 +++++++------ lib/cocoa/TProtocolUtil.m | 10 ++--- lib/cocoa/TTransport.h | 6 +-- 8 files changed, 94 insertions(+), 68 deletions(-) diff --git a/compiler/cpp/src/generate/t_cocoa_generator.cc b/compiler/cpp/src/generate/t_cocoa_generator.cc index 14e0e610..c44d0614 100644 --- a/compiler/cpp/src/generate/t_cocoa_generator.cc +++ b/compiler/cpp/src/generate/t_cocoa_generator.cc @@ -66,14 +66,23 @@ string t_cocoa_generator::cocoa_imports() { * @return List of imports necessary for thrift runtime */ string t_cocoa_generator::cocoa_thrift_imports() { - return - string() + + string result = string() + "#import \n" + "#import \n" + "#import \n" + "\n"; + + // Include other Thrift includes + const vector& includes = program_->get_includes(); + for (size_t i = 0; i < includes.size(); ++i) { + result += "#import \"" + includes[i]->get_name() + ".h\"" + "\n"; + } + result += "\n"; + + return result; } + /** * Finish up generation. */ @@ -276,6 +285,11 @@ void t_cocoa_generator::generate_cocoa_struct_interface(ofstream &out, } out << endl; + // read and write + out << "- (void) read: (id ) inProtocol;" << endl; + out << "- (void) write: (id ) outProtocol;" << endl; + out << endl; + // getters and setters generate_cocoa_struct_field_accessor_declarations(out, tstruct, is_exception); @@ -430,7 +444,7 @@ void t_cocoa_generator::generate_cocoa_struct_reader(ofstream& out, indent(out) << "int fieldID;" << endl; out << endl; - indent(out) << "[inProtocol readStructBeginWithName: NULL];" << endl; + indent(out) << "[inProtocol readStructBeginReturningName: NULL];" << endl; // Loop over reading in fields indent(out) << @@ -439,7 +453,7 @@ void t_cocoa_generator::generate_cocoa_struct_reader(ofstream& out, // Read beginning field marker indent(out) << - "[inProtocol readFieldBeginWithName: &fieldName type: &fieldType fieldID: &fieldID];" << endl; + "[inProtocol readFieldBeginReturningName: &fieldName type: &fieldType fieldID: &fieldID];" << endl; // Check for field STOP marker and break indent(out) << @@ -525,6 +539,9 @@ void t_cocoa_generator::generate_cocoa_struct_writer(ofstream& out, indent() << "[outProtocol writeStructBeginWithName: @\"" << name << "\"];" << endl; for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { + out << + indent() << "if (__" << (*f_iter)->get_name() << "_isset) {" << endl; + indent_up(); bool null_allowed = type_can_be_null((*f_iter)->get_type()); if (null_allowed) { out << @@ -544,9 +561,9 @@ void t_cocoa_generator::generate_cocoa_struct_writer(ofstream& out, "[outProtocol writeFieldEnd];" << endl; if (null_allowed) { - indent_down(); - indent(out) << "}" << endl; + scope_down(out); } + scope_down(out); } // Write the struct map out << @@ -950,7 +967,7 @@ void t_cocoa_generator::generate_cocoa_service_client_implementation(ofstream& o // check for an exception out << indent() << "int msgType = 0;" << endl << - indent() << "[inProtocol readMessageBeginWithName: nil type: &msgType sequenceID: NULL];" << endl << + indent() << "[inProtocol readMessageBeginReturningName: nil type: &msgType sequenceID: NULL];" << endl << indent() << "if (msgType == TMessageType_EXCEPTION) {" << endl << indent() << " TApplicationException * x = [TApplicationException read: inProtocol];" << endl << indent() << " [inProtocol readMessageEnd];" << endl << @@ -990,7 +1007,7 @@ void t_cocoa_generator::generate_cocoa_service_client_implementation(ofstream& o } else { out << indent() << "@throw [TApplicationException exceptionWithType: TApplicationException_MISSING_RESULT" << endl << - indent() << " message: @\"" << (*f_iter)->get_name() << " failed: unknown result\"];" << endl; + indent() << " reason: @\"" << (*f_iter)->get_name() << " failed: unknown result\"];" << endl; } // Close function @@ -1130,18 +1147,18 @@ void t_cocoa_generator::generate_deserialize_container(ofstream& out, // Declare variables, read header if (ttype->is_map()) { indent(out) - << "[inProtocol readMapBeginWithKeyType: NULL valueType: NULL size: &" << + << "[inProtocol readMapBeginReturningKeyType: NULL valueType: NULL size: &" << size << "];" << endl; indent(out) << "NSMutableDictionary * " << fieldName << " = [[NSMutableDictionary alloc] initWithCapacity: " << size << "];" << endl; } else if (ttype->is_set()) { indent(out) - << "[inProtocol readSetBeginWithElementType: NULL size: &" << size << "];" << endl; + << "[inProtocol readSetBeginReturningElementType: NULL size: &" << size << "];" << endl; indent(out) << "NSMutableSet * " << fieldName << " = [[NSMutableSet alloc] initWithCapacity: " << size << "];" << endl; } else if (ttype->is_list()) { indent(out) - << "[inProtocol readListBeginWithElementType: NULL size: &" << size << "];" << endl; + << "[inProtocol readListBeginReturningElementType: NULL size: &" << size << "];" << endl; indent(out) << "NSMutableArray * " << fieldName << " = [[NSMutableArray alloc] initWithCapacity: " << size << "];" << endl; } diff --git a/compiler/cpp/src/generate/t_cocoa_generator.h b/compiler/cpp/src/generate/t_cocoa_generator.h index 1696c207..b34ba8b5 100644 --- a/compiler/cpp/src/generate/t_cocoa_generator.h +++ b/compiler/cpp/src/generate/t_cocoa_generator.h @@ -55,21 +55,21 @@ class t_cocoa_generator : public t_oop_generator { void generate_cocoa_struct_interface(std::ofstream& out, t_struct* tstruct, bool is_xception=false); void generate_cocoa_struct_implementation(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false); void generate_cocoa_struct_initializer_signature(std::ofstream& out, - t_struct* tstruct); + t_struct* tstruct); void generate_cocoa_struct_field_accessor_declarations(std::ofstream& out, - t_struct* tstruct, - bool is_exception); + t_struct* tstruct, + bool is_exception); void generate_cocoa_struct_field_accessor_implementations(std::ofstream& out, - t_struct* tstruct, - bool is_exception); + t_struct* tstruct, + bool is_exception); void generate_cocoa_struct_reader(std::ofstream& out, t_struct* tstruct); void generate_cocoa_struct_result_writer(std::ofstream& out, t_struct* tstruct); void generate_cocoa_struct_writer(std::ofstream& out, t_struct* tstruct); void generate_cocoa_struct_description(std::ofstream& out, t_struct* tstruct); - + std::string function_result_helper_struct_type(t_function* tfunction); void generate_function_helpers(t_function* tfunction); - + /** * Service-level generation functions */ diff --git a/lib/cocoa/TApplicationException.h b/lib/cocoa/TApplicationException.h index bb642820..1fee06e4 100644 --- a/lib/cocoa/TApplicationException.h +++ b/lib/cocoa/TApplicationException.h @@ -1,3 +1,6 @@ +#import "TException.h" +#import "TProtocol.h" + enum { TApplicationException_UNKNOWN = 0, TApplicationException_UNKNOWN_METHOD = 1, @@ -8,12 +11,13 @@ enum { }; // FIXME -@interface TApplicationException : NSException { +@interface TApplicationException : TException { + int mType; } + (TApplicationException *) read: (id ) protocol; + (TApplicationException *) exceptionWithType: (int) type - message: (NSString *) message; + reason: (NSString *) message; @end diff --git a/lib/cocoa/TBinaryProtocol.m b/lib/cocoa/TBinaryProtocol.m index cd239dee..2d2ca8ea 100644 --- a/lib/cocoa/TBinaryProtocol.m +++ b/lib/cocoa/TBinaryProtocol.m @@ -1,4 +1,5 @@ #import "TBinaryProtocol.h" +#import "TProtocolException.h" int32_t VERSION_1 = 0x80010000; int32_t VERSION_MASK = 0xffff0000; @@ -38,17 +39,15 @@ int32_t VERSION_MASK = 0xffff0000; } -- (void) readMessageBeginWithName: (NSString **) name - type: (int *) type - sequenceID: (int *) sequenceID +- (void) readMessageBeginReturningName: (NSString **) name + type: (int *) type + sequenceID: (int *) sequenceID { int size = [self readI32]; if (size < 0) { int version = size & VERSION_MASK; if (version != VERSION_1) { - @throw [NSException exceptionWithName: @"TProtocolException" - reason: @"Bad version in readMessageBegin" - userInfo: nil]; + @throw [TProtocolException exceptionWithName: @"Bad version in readMessageBegin"]; } if (type != NULL) { *type = version & 0x00FF; @@ -63,9 +62,7 @@ int32_t VERSION_MASK = 0xffff0000; } } else { if (mStrictRead) { - @throw [NSException exceptionWithName: @"TProtocolException" - reason: @"Missing version in readMessageBegin, old client?" - userInfo: nil]; + @throw [TProtocolException exceptionWithName: @"Missing version in readMessageBegin, old client?"]; } NSString * messageName = [self readStringBody: size]; if (name != NULL) { @@ -86,7 +83,7 @@ int32_t VERSION_MASK = 0xffff0000; - (void) readMessageEnd {} -- (void) readStructBeginWithName: (NSString **) name +- (void) readStructBeginReturningName: (NSString **) name { if (name != NULL) { *name = nil; @@ -97,9 +94,9 @@ int32_t VERSION_MASK = 0xffff0000; - (void) readStructEnd {} -- (void) readFieldBeginWithName: (NSString **) name - type: (int *) fieldType - fieldID: (int *) fieldID +- (void) readFieldBeginReturningName: (NSString **) name + type: (int *) fieldType + fieldID: (int *) fieldID { if (name != NULL) { *name = nil; @@ -189,19 +186,19 @@ int32_t VERSION_MASK = 0xffff0000; int32_t size = [self readI32]; uint8_t * buff = malloc(size); if (buff == NULL) { - @throw [NSException exceptionWithName: @"Out of memory" - reason: [NSString stringWithFormat: @"Unable to allocate %d bytes trying to read binary data.", - size] - userInfo: nil]; + @throw [TProtocolException + exceptionWithName: @"Out of memory" + reason: [NSString stringWithFormat: @"Unable to allocate %d bytes trying to read binary data.", + size]]; } [mTransport readAll: buff offset: 0 length: size]; return [NSData dataWithBytesNoCopy: buff length: size]; } -- (void) readMapBeginWithKeyType: (int *) keyType - valueType: (int *) valueType - size: (int *) size +- (void) readMapBeginReturningKeyType: (int *) keyType + valueType: (int *) valueType + size: (int *) size { int kt = [self readByte]; int vt = [self readByte]; @@ -220,8 +217,8 @@ int32_t VERSION_MASK = 0xffff0000; - (void) readMapEnd {} -- (void) readSetBeginWithElementType: (int *) elementType - size: (int *) size +- (void) readSetBeginReturningElementType: (int *) elementType + size: (int *) size { int et = [self readByte]; int s = [self readI32]; @@ -237,8 +234,8 @@ int32_t VERSION_MASK = 0xffff0000; - (void) readSetEnd {} -- (void) readListBeginWithElementType: (int *) elementType - size: (int *) size +- (void) readListBeginReturningElementType: (int *) elementType + size: (int *) size { int et = [self readByte]; int s = [self readI32]; diff --git a/lib/cocoa/TException.h b/lib/cocoa/TException.h index 95eaa834..ce3b4a5a 100644 --- a/lib/cocoa/TException.h +++ b/lib/cocoa/TException.h @@ -3,7 +3,13 @@ @interface TException : NSException { } -- (id) initWithType: (int) type - message: (NSString *) message; ++ (id) exceptionWithName: (NSString *) name; + ++ (id) exceptionWithName: (NSString *) name + reason: (NSString *) reason; + ++ (id) exceptionWithName: (NSString *) name + reason: (NSString *) reason + error: (NSError *) error; @end diff --git a/lib/cocoa/TProtocol.h b/lib/cocoa/TProtocol.h index 96d3af6a..bc64789e 100644 --- a/lib/cocoa/TProtocol.h +++ b/lib/cocoa/TProtocol.h @@ -30,17 +30,17 @@ enum { - (id ) transport; -- (void) readMessageBeginWithName: (NSString **) name - type: (int *) type - sequenceID: (int *) sequenceID; +- (void) readMessageBeginReturningName: (NSString **) name + type: (int *) type + sequenceID: (int *) sequenceID; - (void) readMessageEnd; -- (void) readStructBeginWithName: (NSString **) name; +- (void) readStructBeginReturningName: (NSString **) name; - (void) readStructEnd; -- (void) readFieldBeginWithName: (NSString **) name - type: (int *) fieldType - fieldID: (int *) fieldID; +- (void) readFieldBeginReturningName: (NSString **) name + type: (int *) fieldType + fieldID: (int *) fieldID; - (void) readFieldEnd; - (NSString *) readString; @@ -59,19 +59,19 @@ enum { - (NSData *) readBinary; -- (void) readMapBeginWithKeyType: (int *) keyType - valueType: (int *) valueType - size: (int *) size; +- (void) readMapBeginReturningKeyType: (int *) keyType + valueType: (int *) valueType + size: (int *) size; - (void) readMapEnd; -- (void) readSetBeginWithElementType: (int *) elementType - size: (int *) size; +- (void) readSetBeginReturningElementType: (int *) elementType + size: (int *) size; - (void) readSetEnd; -- (void) readListBeginWithElementType: (int *) elementType - size: (int *) size; +- (void) readListBeginReturningElementType: (int *) elementType + size: (int *) size; - (void) readListEnd; @@ -99,6 +99,8 @@ enum { - (void) writeDouble: (double) value; +- (void) writeBool: (BOOL) value; + - (void) writeBinary: (NSData *) data; - (void) writeFieldStop; diff --git a/lib/cocoa/TProtocolUtil.m b/lib/cocoa/TProtocolUtil.m index 746790f5..cf32aad6 100644 --- a/lib/cocoa/TProtocolUtil.m +++ b/lib/cocoa/TProtocolUtil.m @@ -27,10 +27,10 @@ [protocol readString]; break; case TType_STRUCT: - [protocol readStructBeginWithName: NULL]; + [protocol readStructBeginReturningName: NULL]; while (true) { int fieldType; - [protocol readFieldBeginWithName: nil type: &fieldType fieldID: nil]; + [protocol readFieldBeginReturningName: nil type: &fieldType fieldID: nil]; if (fieldType == TType_STOP) { break; } @@ -44,7 +44,7 @@ int keyType; int valueType; int size; - [protocol readMapBeginWithKeyType: &keyType valueType: &valueType size: &size]; + [protocol readMapBeginReturningKeyType: &keyType valueType: &valueType size: &size]; int i; for (i = 0; i < size; i++) { [TProtocolUtil skipType: keyType onProtocol: protocol]; @@ -57,7 +57,7 @@ { int elemType; int size; - [protocol readSetBeginWithElementType: &elemType size: &size]; + [protocol readSetBeginReturningElementType: &elemType size: &size]; int i; for (i = 0; i < size; i++) { [TProtocolUtil skipType: elemType onProtocol: protocol]; @@ -69,7 +69,7 @@ { int elemType; int size; - [protocol readListBeginWithElementType: &elemType size: &size]; + [protocol readListBeginReturningElementType: &elemType size: &size]; int i; for (i = 0; i < size; i++) { [TProtocolUtil skipType: elemType onProtocol: protocol]; diff --git a/lib/cocoa/TTransport.h b/lib/cocoa/TTransport.h index bb0270e0..1fa04f4c 100644 --- a/lib/cocoa/TTransport.h +++ b/lib/cocoa/TTransport.h @@ -1,10 +1,10 @@ @protocol TTransport /** - * Guarantees that all of len bytes are + * Guarantees that all of len bytes are read * - * @param buf Array to read into - * @param off Index to start reading at + * @param buf Buffer to read into + * @param off Index in buffer to start storing bytes at * @param len Maximum number of bytes to read * @return The number of bytes actually read, which must be equal to len * @throws TTransportException if there was an error reading data -- 2.17.1