Commit 435e88ca authored by Jacob Potter's avatar Jacob Potter

Remove ObjC deep-copy constructor. Fixes #93

parent ed11cb63
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface TXSItemList : NSObject
- (nonnull id)initWithItemList:(nonnull TXSItemList *)itemList;
- (nonnull id)initWithItems:(nonnull NSArray *)items;
@property (nonatomic, readonly, nonnull) NSArray * items;
......
......@@ -6,20 +6,6 @@
@implementation TXSItemList
- (id)initWithItemList:(TXSItemList *)itemList
{
if (self = [super init]) {
NSMutableArray *_itemsTempArray = [NSMutableArray arrayWithCapacity:[itemList.items count]];
for (NSString *currentValue_0 in itemList.items) {
id copiedValue_0;
copiedValue_0 = [currentValue_0 copy];
[_itemsTempArray addObject:copiedValue_0];
}
_items = _itemsTempArray;
}
return self;
}
- (id)initWithItems:(nonnull NSArray *)items
{
if (self = [super init]) {
......
......@@ -195,8 +195,6 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
writeDoc(w, doc)
w.wl(s"@interface $self : NSObject")
// Deep copy construtor
w.wl(s"- (nonnull id)${idObjc.method("init_with_" + ident.name)}:(nonnull $self *)${idObjc.local(ident)};")
if (!r.fields.isEmpty) {
val head = r.fields.head
val skipFirst = SkipFirst()
......@@ -235,16 +233,6 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
w.wl
w.wl(s"@implementation $self")
w.wl
w.wl(s"- (id)${idObjc.method("init_with_" + ident.name)}:($noBaseSelf *)${idObjc.local(ident)}")
w.braced {
w.w("if (self = [super init])").braced {
for (f <- r.fields) {
copyObjcValue(s"_${idObjc.field(f.ident)}", s"${idObjc.local(ident)}.${idObjc.field(f.ident)}", f.ty, w)
}
}
w.wl("return self;")
}
w.wl
// Constructor from all fields (not copying)
if (!r.fields.isEmpty) {
val head = r.fields.head
......@@ -344,76 +332,6 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
})
}
def copyObjcValue(to: String, from: String, ty: TypeRef, w: IndentWriter): Unit =
copyObjcValue(to, from, ty.resolved, w)
def copyObjcValue(to: String, from: String, tm: MExpr, w: IndentWriter): Unit = {
def f(to: String, from: String, tm: MExpr, needRef: Boolean, w: IndentWriter, valueLevel: Int): Unit = {
tm.base match {
case MOptional => {
w.wl(s"if ($from == nil) {").nested {
w.wl(s"$to = nil;")
}
w.wl("} else {").nested {
f(to, from, tm.args.head, true, w, valueLevel)
}
w.wl("}")
}
case p: MPrimitive => w.wl(s"$to = $from;") // NSNumber is immutable, so are primitive values
case MString => w.wl(s"$to = [$from copy];")
case MDate => w.wl(s"$to = [$from copy];")
case MBinary => w.wl(s"$to = [$from copy];")
case MList => {
val copyName = "copiedValue_" + valueLevel
val currentName = "currentValue_" + valueLevel
w.wl(s"NSMutableArray *${to}TempArray = [NSMutableArray arrayWithCapacity:[$from count]];")
w.w(s"for (${toObjcTypeDef(tm.args.head, true)}$currentName in $from)").braced {
w.wl(s"id $copyName;")
f(copyName, currentName, tm.args.head, true, w, valueLevel + 1)
w.wl(s"[${to}TempArray addObject:$copyName];")
}
w.wl(s"$to = ${to}TempArray;")
}
case MSet => {
val copyName = "copiedValue_" + valueLevel
val currentName = "currentValue_" + valueLevel
w.wl(s"NSMutableSet *${to}TempSet = [NSMutableSet setWithCapacity:[$from count]];")
w.w(s"for (${toObjcTypeDef(tm.args.head, true)}$currentName in $from)").braced {
w.wl(s"id $copyName;")
f(copyName, currentName, tm.args.head, true, w, valueLevel + 1)
w.wl(s"[${to}TempSet addObject:$copyName];")
}
w.wl(s"$to = ${to}TempSet;")
}
case MMap => {
w.wl(s"NSMutableDictionary *${to}TempDictionary = [NSMutableDictionary dictionaryWithCapacity:[$from count]];")
val keyName = "key_" + valueLevel
val valueName = "value_" + valueLevel
val copiedKeyName = "copiedKey_" + valueLevel
val copiedValueName = "copiedValue_" + valueLevel
w.w(s"for (id $keyName in $from)").braced {
w.wl(s"id $copiedKeyName, $copiedValueName;")
f(copiedKeyName, keyName, tm.args.apply(0), true, w, valueLevel + 1)
w.wl(s"id $valueName = [$from objectForKey:$keyName];")
f(copiedValueName, valueName, tm.args.apply(1), true, w, valueLevel + 1)
w.wl(s"[${to}TempDictionary setObject:$copiedValueName forKey:$copiedKeyName];")
}
w.wl(s"$to = ${to}TempDictionary;")
}
case d: MDef => {
val typeName = d.name
val self = idObjc.ty(typeName)
d.defType match {
case DEnum => w.wl(s"$to = $from;")
case DRecord => w.wl(s"$to = [[${idObjc.ty(d.name)} alloc] ${idObjc.method("init_with_" + d.name)}:$from];")
case DInterface => w.wl(s"$to = $from;")
}
}
case p: MParam =>
}
}
f(to, from, tm, false, w, 0)
}
def writeObjcFile(fileName: String, origin: String, refs: Iterable[String], f: IndentWriter => Unit) {
createFile(spec.objcOutFolder.get, fileName, (w: IndentWriter) => {
w.wl("// AUTOGENERATED FILE - DO NOT MODIFY!")
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBAssortedIntegers : NSObject
- (nonnull id)initWithAssortedIntegers:(nonnull DBAssortedIntegers *)assortedIntegers;
- (nonnull id)initWithEight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
......
......@@ -6,37 +6,6 @@
@implementation DBAssortedIntegers
- (id)initWithAssortedIntegers:(DBAssortedIntegers *)assortedIntegers
{
if (self = [super init]) {
_eight = assortedIntegers.eight;
_sixteen = assortedIntegers.sixteen;
_thirtytwo = assortedIntegers.thirtytwo;
_sixtyfour = assortedIntegers.sixtyfour;
if (assortedIntegers.oEight == nil) {
_oEight = nil;
} else {
_oEight = assortedIntegers.oEight;
}
if (assortedIntegers.oSixteen == nil) {
_oSixteen = nil;
} else {
_oSixteen = assortedIntegers.oSixteen;
}
if (assortedIntegers.oThirtytwo == nil) {
_oThirtytwo = nil;
} else {
_oThirtytwo = assortedIntegers.oThirtytwo;
}
if (assortedIntegers.oSixtyfour == nil) {
_oSixtyfour = nil;
} else {
_oSixtyfour = assortedIntegers.oSixtyfour;
}
}
return self;
}
- (id)initWithEight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBClientReturnedRecord : NSObject
- (nonnull id)initWithClientReturnedRecord:(nonnull DBClientReturnedRecord *)clientReturnedRecord;
- (nonnull id)initWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc;
......
......@@ -6,20 +6,6 @@
@implementation DBClientReturnedRecord
- (id)initWithClientReturnedRecord:(DBClientReturnedRecord *)clientReturnedRecord
{
if (self = [super init]) {
_recordId = clientReturnedRecord.recordId;
_content = [clientReturnedRecord.content copy];
if (clientReturnedRecord.misc == nil) {
_misc = nil;
} else {
_misc = [clientReturnedRecord.misc copy];
}
}
return self;
}
- (id)initWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc
......
......@@ -5,7 +5,6 @@
#import <Foundation/Foundation.h>
@interface DBConstants : NSObject
- (nonnull id)initWithConstants:(nonnull DBConstants *)constants;
- (nonnull id)initWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString;
......
......@@ -29,15 +29,6 @@ DBConstants * __nonnull const DBConstantsObjectConstant = [[DBConstants alloc] i
@implementation DBConstants
- (id)initWithConstants:(DBConstants *)constants
{
if (self = [super init]) {
_someInteger = constants.someInteger;
_someString = [constants.someString copy];
}
return self;
}
- (id)initWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString
{
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBDateRecord : NSObject
- (nonnull id)initWithDateRecord:(nonnull DBDateRecord *)dateRecord;
- (nonnull id)initWithCreatedAt:(nonnull NSDate *)createdAt;
@property (nonatomic, readonly, nonnull) NSDate * createdAt;
......
......@@ -6,14 +6,6 @@
@implementation DBDateRecord
- (id)initWithDateRecord:(DBDateRecord *)dateRecord
{
if (self = [super init]) {
_createdAt = [dateRecord.createdAt copy];
}
return self;
}
- (id)initWithCreatedAt:(nonnull NSDate *)createdAt
{
if (self = [super init]) {
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBMapDateRecord : NSObject
- (nonnull id)initWithMapDateRecord:(nonnull DBMapDateRecord *)mapDateRecord;
- (nonnull id)initWithDatesById:(nonnull NSDictionary *)datesById;
@property (nonatomic, readonly, nonnull) NSDictionary * datesById;
......
......@@ -6,22 +6,6 @@
@implementation DBMapDateRecord
- (id)initWithMapDateRecord:(DBMapDateRecord *)mapDateRecord
{
if (self = [super init]) {
NSMutableDictionary *_datesByIdTempDictionary = [NSMutableDictionary dictionaryWithCapacity:[mapDateRecord.datesById count]];
for (id key_0 in mapDateRecord.datesById) {
id copiedKey_0, copiedValue_0;
copiedKey_0 = [key_0 copy];
id value_0 = [mapDateRecord.datesById objectForKey:key_0];
copiedValue_0 = [value_0 copy];
[_datesByIdTempDictionary setObject:copiedValue_0 forKey:copiedKey_0];
}
_datesById = _datesByIdTempDictionary;
}
return self;
}
- (id)initWithDatesById:(nonnull NSDictionary *)datesById
{
if (self = [super init]) {
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBMapListRecord : NSObject
- (nonnull id)initWithMapListRecord:(nonnull DBMapListRecord *)mapListRecord;
- (nonnull id)initWithMapList:(nonnull NSArray *)mapList;
@property (nonatomic, readonly, nonnull) NSArray * mapList;
......
......@@ -6,28 +6,6 @@
@implementation DBMapListRecord
- (id)initWithMapListRecord:(DBMapListRecord *)mapListRecord
{
if (self = [super init]) {
NSMutableArray *_mapListTempArray = [NSMutableArray arrayWithCapacity:[mapListRecord.mapList count]];
for (NSDictionary *currentValue_0 in mapListRecord.mapList) {
id copiedValue_0;
NSMutableDictionary *copiedValue_0TempDictionary = [NSMutableDictionary dictionaryWithCapacity:[currentValue_0 count]];
for (id key_1 in currentValue_0) {
id copiedKey_1, copiedValue_1;
copiedKey_1 = [key_1 copy];
id value_1 = [currentValue_0 objectForKey:key_1];
copiedValue_1 = value_1;
[copiedValue_0TempDictionary setObject:copiedValue_1 forKey:copiedKey_1];
}
copiedValue_0 = copiedValue_0TempDictionary;
[_mapListTempArray addObject:copiedValue_0];
}
_mapList = _mapListTempArray;
}
return self;
}
- (id)initWithMapList:(nonnull NSArray *)mapList
{
if (self = [super init]) {
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBMapRecord : NSObject
- (nonnull id)initWithMapRecord:(nonnull DBMapRecord *)mapRecord;
- (nonnull id)initWithMap:(nonnull NSDictionary *)map;
@property (nonatomic, readonly, nonnull) NSDictionary * map;
......
......@@ -6,22 +6,6 @@
@implementation DBMapRecord
- (id)initWithMapRecord:(DBMapRecord *)mapRecord
{
if (self = [super init]) {
NSMutableDictionary *_mapTempDictionary = [NSMutableDictionary dictionaryWithCapacity:[mapRecord.map count]];
for (id key_0 in mapRecord.map) {
id copiedKey_0, copiedValue_0;
copiedKey_0 = [key_0 copy];
id value_0 = [mapRecord.map objectForKey:key_0];
copiedValue_0 = value_0;
[_mapTempDictionary setObject:copiedValue_0 forKey:copiedKey_0];
}
_map = _mapTempDictionary;
}
return self;
}
- (id)initWithMap:(nonnull NSDictionary *)map
{
if (self = [super init]) {
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBNestedCollection : NSObject
- (nonnull id)initWithNestedCollection:(nonnull DBNestedCollection *)nestedCollection;
- (nonnull id)initWithSetList:(nonnull NSArray *)setList;
@property (nonatomic, readonly, nonnull) NSArray * setList;
......
......@@ -6,26 +6,6 @@
@implementation DBNestedCollection
- (id)initWithNestedCollection:(DBNestedCollection *)nestedCollection
{
if (self = [super init]) {
NSMutableArray *_setListTempArray = [NSMutableArray arrayWithCapacity:[nestedCollection.setList count]];
for (NSSet *currentValue_0 in nestedCollection.setList) {
id copiedValue_0;
NSMutableSet *copiedValue_0TempSet = [NSMutableSet setWithCapacity:[currentValue_0 count]];
for (NSString *currentValue_1 in currentValue_0) {
id copiedValue_1;
copiedValue_1 = [currentValue_1 copy];
[copiedValue_0TempSet addObject:copiedValue_1];
}
copiedValue_0 = copiedValue_0TempSet;
[_setListTempArray addObject:copiedValue_0];
}
_setList = _setListTempArray;
}
return self;
}
- (id)initWithSetList:(nonnull NSArray *)setList
{
if (self = [super init]) {
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBPrimitiveList : NSObject
- (nonnull id)initWithPrimitiveList:(nonnull DBPrimitiveList *)primitiveList;
- (nonnull id)initWithList:(nonnull NSArray *)list;
@property (nonatomic, readonly, nonnull) NSArray * list;
......
......@@ -6,20 +6,6 @@
@implementation DBPrimitiveList
- (id)initWithPrimitiveList:(DBPrimitiveList *)primitiveList
{
if (self = [super init]) {
NSMutableArray *_listTempArray = [NSMutableArray arrayWithCapacity:[primitiveList.list count]];
for (NSNumber *currentValue_0 in primitiveList.list) {
id copiedValue_0;
copiedValue_0 = currentValue_0;
[_listTempArray addObject:copiedValue_0];
}
_list = _listTempArray;
}
return self;
}
- (id)initWithList:(nonnull NSArray *)list
{
if (self = [super init]) {
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBRecordWithDerivings : NSObject
- (nonnull id)initWithRecordWithDerivings:(nonnull DBRecordWithDerivings *)recordWithDerivings;
- (nonnull id)initWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2;
......
......@@ -6,15 +6,6 @@
@implementation DBRecordWithDerivings
- (id)initWithRecordWithDerivings:(DBRecordWithDerivings *)recordWithDerivings
{
if (self = [super init]) {
_key1 = recordWithDerivings.key1;
_key2 = [recordWithDerivings.key2 copy];
}
return self;
}
- (id)initWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2
{
......
......@@ -5,7 +5,6 @@
#import <Foundation/Foundation.h>
@interface DBRecordWithNestedDerivings : NSObject
- (nonnull id)initWithRecordWithNestedDerivings:(nonnull DBRecordWithNestedDerivings *)recordWithNestedDerivings;
- (nonnull id)initWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec;
......
......@@ -6,15 +6,6 @@
@implementation DBRecordWithNestedDerivings
- (id)initWithRecordWithNestedDerivings:(DBRecordWithNestedDerivings *)recordWithNestedDerivings
{
if (self = [super init]) {
_key = recordWithNestedDerivings.key;
_rec = [[DBRecordWithDerivings alloc] initWithRecordWithDerivings:recordWithNestedDerivings.rec];
}
return self;
}
- (id)initWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec
{
......
......@@ -4,7 +4,6 @@
#import <Foundation/Foundation.h>
@interface DBSetRecord : NSObject
- (nonnull id)initWithSetRecord:(nonnull DBSetRecord *)setRecord;
- (nonnull id)initWithSet:(nonnull NSSet *)set;
@property (nonatomic, readonly, nonnull) NSSet * set;
......
......@@ -6,20 +6,6 @@
@implementation DBSetRecord
- (id)initWithSetRecord:(DBSetRecord *)setRecord
{
if (self = [super init]) {
NSMutableSet *_setTempSet = [NSMutableSet setWithCapacity:[setRecord.set count]];
for (NSString *currentValue_0 in setRecord.set) {
id copiedValue_0;
copiedValue_0 = [currentValue_0 copy];
[_setTempSet addObject:copiedValue_0];
}
_set = _setTempSet;
}
return self;
}
- (id)initWithSet:(nonnull NSSet *)set
{
if (self = [super init]) {
......
......@@ -26,13 +26,6 @@ static DBPrimitiveList *objcPrimitiveList = [[DBPrimitiveList alloc] initWithLis
[super tearDown];
}
- (void)testObjcCopyConstructor
{
DBPrimitiveList *copy = [[DBPrimitiveList alloc] initWithPrimitiveList:objcPrimitiveList];
XCTAssertNotEqual(copy.list, objcPrimitiveList.list, @"The two NSArrays should not be the same object.");
XCTAssertEqualObjects(copy.list, objcPrimitiveList.list, @"The two NSArrays should have the same content.");
}
- (void)testObjcToCppConverter
{
PrimitiveList convert = djinni_generated::PrimitiveList::toCpp(objcPrimitiveList);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment