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
diff --git a/compiler/cpp/src/generate/t_cocoa_generator.cc b/compiler/cpp/src/generate/t_cocoa_generator.cc
index 14e0e61..c44d061 100644
--- a/compiler/cpp/src/generate/t_cocoa_generator.cc
+++ b/compiler/cpp/src/generate/t_cocoa_generator.cc
@@ -66,14 +66,23 @@
  * @return List of imports necessary for thrift runtime
  */
 string t_cocoa_generator::cocoa_thrift_imports() {
-  return
-    string() +
+  string result = string() +
     "#import <TProtocol.h>\n" +
     "#import <TApplicationException.h>\n" +
     "#import <TProtocolUtil.h>\n" +
     "\n";
+
+  // Include other Thrift includes
+  const vector<t_program*>& 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 @@
   }
   out << endl;
 
+  // read and write
+  out << "- (void) read: (id <TProtocol>) inProtocol;" << endl;
+  out << "- (void) write: (id <TProtocol>) outProtocol;" << endl;
+  out << endl;
+
   // getters and setters
   generate_cocoa_struct_field_accessor_declarations(out, tstruct, is_exception);
 
@@ -430,7 +444,7 @@
   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 @@
     
     // 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 @@
     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 @@
       "[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 @@
       // 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 @@
       } 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 @@
   // 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 1696c20..b34ba8b 100644
--- a/compiler/cpp/src/generate/t_cocoa_generator.h
+++ b/compiler/cpp/src/generate/t_cocoa_generator.h
@@ -55,21 +55,21 @@
   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 bb64282..1fee06e 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 @@
 };
 
 // FIXME
-@interface TApplicationException : NSException {
+@interface TApplicationException : TException {
+  int mType;
 }
 
 + (TApplicationException *) read: (id <TProtocol>) 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 cd239de..2d2ca8e 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 @@
 }
 
 
-- (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 @@
     }
   } 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 @@
 - (void) readMessageEnd {}
 
 
-- (void) readStructBeginWithName: (NSString **) name
+- (void) readStructBeginReturningName: (NSString **) name
 {
   if (name != NULL) {
     *name = nil;
@@ -97,9 +94,9 @@
 - (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 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 @@
 - (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 @@
 - (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 95eaa83..ce3b4a5 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 96d3af6..bc64789 100644
--- a/lib/cocoa/TProtocol.h
+++ b/lib/cocoa/TProtocol.h
@@ -30,17 +30,17 @@
 
 - (id <TTransport>) 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 @@
 
 - (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 @@
 
 - (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 746790f..cf32aad 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 bb0270e..1fa04f4 100644
--- a/lib/cocoa/TTransport.h
+++ b/lib/cocoa/TTransport.h
@@ -1,10 +1,10 @@
 @protocol TTransport <NSObject>
 
   /**
-   * 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