* @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.
*/
}
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);
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) <<
// 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) <<
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 <<
"[outProtocol writeFieldEnd];" << endl;
if (null_allowed) {
- indent_down();
- indent(out) << "}" << endl;
+ scope_down(out);
}
+ scope_down(out);
}
// Write the struct map
out <<
// 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 <<
} 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
// 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;
}
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
*/
+#import "TException.h"
+#import "TProtocol.h"
+
enum {
TApplicationException_UNKNOWN = 0,
TApplicationException_UNKNOWN_METHOD = 1,
};
// 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
#import "TBinaryProtocol.h"
+#import "TProtocolException.h"
int32_t VERSION_1 = 0x80010000;
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;
}
} 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) {
- (void) readMessageEnd {}
-- (void) readStructBeginWithName: (NSString **) name
+- (void) readStructBeginReturningName: (NSString **) name
{
if (name != NULL) {
*name = nil;
- (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;
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];
- (void) readMapEnd {}
-- (void) readSetBeginWithElementType: (int *) elementType
- size: (int *) size
+- (void) readSetBeginReturningElementType: (int *) elementType
+ size: (int *) size
{
int et = [self readByte];
int s = [self readI32];
- (void) readSetEnd {}
-- (void) readListBeginWithElementType: (int *) elementType
- size: (int *) size
+- (void) readListBeginReturningElementType: (int *) elementType
+ size: (int *) size
{
int et = [self readByte];
int s = [self readI32];
@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
- (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;
- (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;
- (void) writeDouble: (double) value;
+- (void) writeBool: (BOOL) value;
+
- (void) writeBinary: (NSData *) data;
- (void) writeFieldStop;
[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;
}
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];
{
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];
{
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];
@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