Mercurial > projects > dstep
diff dstep/foundation/NSData.d @ 16:19885b43130e
Huge update, the bridge actually works now
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sun, 03 Jan 2010 22:06:11 +0100 |
parents | 89f3c3ef1fd2 |
children | b9de51448c6b |
line wrap: on
line diff
--- a/dstep/foundation/NSData.d Mon Aug 03 15:31:48 2009 +0200 +++ b/dstep/foundation/NSData.d Sun Jan 03 22:06:11 2010 +0100 @@ -6,13 +6,16 @@ */ module dstep.foundation.NSData; +import dstep.foundation.NSCoder; import dstep.foundation.NSError; +import dstep.foundation.NSObjCRuntime; import dstep.foundation.NSObject; import dstep.foundation.NSRange; import dstep.foundation.NSString; import dstep.foundation.NSURL; +import dstep.foundation.NSZone; import dstep.objc.bridge.Bridge; -import dstep.objc.objc : id; +import dstep.objc.objc; enum { @@ -25,116 +28,71 @@ NSAtomicWrite = 1 } -class NSData : NSObject, INSCopying, INSMutableCopying, INSCoding -{ - mixin ObjcWrap; - mixin TNSDataCreation; - mixin TNSExtendedData; - - NSUInteger length () - { - return invokeObjcSelf!(NSUInteger, "length"); - } - - void* bytes () - { - return invokeObjcSelf!(void*, "bytes"); - } - - Object copyWithZone (NSZone* zone) - { - return invokeObjcSelf!(Object, "copyWithZone:", NSZone*)(zone); - } - - Object mutableCopyWithZone (NSZone* zone) - { - return invokeObjcSelf!(Object, "mutableCopyWithZone:", NSZone*)(zone); - } - - void encodeWithCoder (NSCoder aCoder) - { - return invokeObjcSelf!(void, "encodeWithCoder:", NSCoder)(aCoder); - } +const TNSDataCreation = ` - Object initWithCoder (NSCoder aDecoder) - { - return invokeObjcSelf!(Object, "initWithCoder:", NSCoder)(aDecoder); - } - - this (NSCoder aDecoder) - { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithCoder:", NSCoder)(objcObject, aDecoder); - - if (result) - objcObject = ret; - - dObject = this; - } -} - -class NSMutableData : NSData -{ - mixin ObjcWrap; - mixin TNSMutableDataCreation; - mixin TNSExtendedMutableData; - - void* mutableBytes () - { - return invokeObjcSelf!(void*, "mutableBytes"); - } - - void setLength (NSUInteger length) - { - return invokeObjcSelf!(void, "setLength:", NSUInteger)(length); - } -} - -template TNSDataCreation () -{ static Object data () { - return invokeObjcSelfClass!(Object, "data"); + return invokeObjcSuperClass!(Object, "data"); } static Object dataWithBytes (void* bytes, NSUInteger length) { - return invokeObjcSelfClass!(Object, "dataWithBytes:length:", void*, NSUInteger)(bytes, length); + return invokeObjcSuperClass!(Object, "dataWithBytes:length:", void*, NSUInteger)(bytes, length); } static Object dataWithBytesNoCopy (void* bytes, NSUInteger length) { - return invokeObjcSelfClass!(Object, "dataWithBytesNoCopy:length:", void*, NSUInteger)(bytes, length); + return invokeObjcSuperClass!(Object, "dataWithBytesNoCopy:length:", void*, NSUInteger)(bytes, length); } static Object dataWithBytesNoCopy (void* bytes, NSUInteger length, bool b) { - return invokeObjcSelfClass!(Object, "dataWithBytesNoCopy:length:freeWhenDone:", void*, NSUInteger, bool)(bytes, length, b); + return invokeObjcSuperClass!(Object, "dataWithBytesNoCopy:length:freeWhenDone:", void*, NSUInteger, bool)(bytes, length, b); } - static Object dataWithContentsOfFile (NSString path, NSUInteger readOptionsMask, NSError** errorPtr) + static Object dataWithContentsOfFile (NSString path, NSUInteger readOptionsMask, ref NSError errorPtr) { - return invokeObjcSelfClass!(Object, "dataWithContentsOfFile:options:error:", NSString, NSUInteger, NSError**)(path, readOptionsMask, errorPtr); + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSuperClass!(Object, "dataWithContentsOfFile:options:error:", NSString, NSUInteger, id*)(path, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; } - static Object dataWithContentsOfURL (NSURL url, NSUInteger readOptionsMask, NSError** errorPtr) + static Object dataWithContentsOfURL (NSURL url, NSUInteger readOptionsMask, ref NSError errorPtr) { - return invokeObjcSelfClass!(Object, "dataWithContentsOfURL:options:error:", NSURL, NSUInteger, NSError**)(url, readOptionsMask, errorPtr); + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSuperClass!(Object, "dataWithContentsOfURL:options:error:", NSURL, NSUInteger, id*)(url, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; } static Object dataWithContentsOfFile (NSString path) { - return invokeObjcSelfClass!(Object, "dataWithContentsOfFile:", NSString)(path); + return invokeObjcSuperClass!(Object, "dataWithContentsOfFile:", NSString)(path); } static Object dataWithContentsOfURL (NSURL url) { - return invokeObjcSelfClass!(Object, "dataWithContentsOfURL:", NSURL)(url); + return invokeObjcSuperClass!(Object, "dataWithContentsOfURL:", NSURL)(url); } static Object dataWithContentsOfMappedFile (NSString path) { - return invokeObjcSelfClass!(Object, "dataWithContentsOfMappedFile:", NSString)(path); + return invokeObjcSuperClass!(Object, "dataWithContentsOfMappedFile:", NSString)(path); } Object initWithBytes (void* bytes, NSUInteger length) @@ -144,13 +102,7 @@ this (void* bytes, NSUInteger length) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithBytes:length:", void*, NSUInteger)(objcObject, bytes, length); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithBytes(bytes, length); } Object initWithBytesNoCopy (void* bytes, NSUInteger length) @@ -160,13 +112,7 @@ this (void* bytes, NSUInteger length) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithBytesNoCopy:length:", void*, NSUInteger)(objcObject, bytes, length); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithBytesNoCopy(bytes, length); } Object initWithBytesNoCopy (void* bytes, NSUInteger length, bool b) @@ -176,45 +122,47 @@ this (void* bytes, NSUInteger length, bool b) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithBytesNoCopy:length:freeWhenDone:", void*, NSUInteger, bool)(objcObject, bytes, length, b); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithBytesNoCopy(bytes, length, b); } - Object initWithContentsOfFile (NSString path, NSUInteger readOptionsMask, NSError** errorPtr) + Object initWithContentsOfFile (NSString path, NSUInteger readOptionsMask, ref NSError errorPtr) { - return invokeObjcSelf!(Object, "initWithContentsOfFile:options:error:", NSString, NSUInteger, NSError**)(path, readOptionsMask, errorPtr); + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSelf!(Object, "initWithContentsOfFile:options:error:", NSString, NSUInteger, id*)(path, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; } - this (NSString path, NSUInteger readOptionsMask, NSError** errorPtr) + this (NSString path, NSUInteger readOptionsMask, ref NSError errorPtr) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithContentsOfFile:options:error:", NSString, NSUInteger, NSError**)(objcObject, path, readOptionsMask, errorPtr); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithContentsOfFile(path, readOptionsMask, errorPtr); } - Object initWithContentsOfURL (NSURL url, NSUInteger readOptionsMask, NSError** errorPtr) + Object initWithContentsOfURL (NSURL url, NSUInteger readOptionsMask, ref NSError errorPtr) { - return invokeObjcSelf!(Object, "initWithContentsOfURL:options:error:", NSURL, NSUInteger, NSError**)(url, readOptionsMask, errorPtr); + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSelf!(Object, "initWithContentsOfURL:options:error:", NSURL, NSUInteger, id*)(url, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; } - this (NSURL url, NSUInteger readOptionsMask, NSError** errorPtr) + this (NSURL url, NSUInteger readOptionsMask, ref NSError errorPtr) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithContentsOfURL:options:error:", NSURL, NSUInteger, NSError**)(objcObject, url, readOptionsMask, errorPtr); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithContentsOfURL(url, readOptionsMask, errorPtr); } Object initWithContentsOfFile (NSString path) @@ -224,13 +172,7 @@ this (NSString path) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithContentsOfFile:", NSString)(objcObject, path); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithContentsOfFile(path); } Object initWithContentsOfURL (NSURL url) @@ -240,13 +182,7 @@ this (NSURL url) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithContentsOfURL:", NSURL)(objcObject, url); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithContentsOfURL(url); } Object initWithContentsOfMappedFile (NSString path) @@ -256,13 +192,7 @@ this (NSString path) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithContentsOfMappedFile:", NSString)(objcObject, path); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithContentsOfMappedFile(path); } Object initWithData (NSData data) @@ -272,31 +202,25 @@ this (NSData data) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithData:", NSData)(objcObject, data); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithData(data); } static Object dataWithData (NSData data) { - return invokeObjcSelfClass!(Object, "dataWithData:", NSData)(data); + return invokeObjcSuperClass!(Object, "dataWithData:", NSData)(data); } -} +`; -template TNSMutableDataCreation () -{ +const TNSMutableDataCreation = ` + static Object dataWithCapacity (NSUInteger aNumItems) { - return invokeObjcSelfClass!(Object, "dataWithCapacity:", NSUInteger)(aNumItems); + return invokeObjcSuperClass!(Object, "dataWithCapacity:", NSUInteger)(aNumItems); } static Object dataWithLength (NSUInteger length) { - return invokeObjcSelfClass!(Object, "dataWithLength:", NSUInteger)(length); + return invokeObjcSuperClass!(Object, "dataWithLength:", NSUInteger)(length); } Object initWithCapacity (NSUInteger capacity) @@ -306,13 +230,7 @@ this (NSUInteger capacity) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithCapacity:", NSUInteger)(objcObject, capacity); - - if (result) - objcObject = ret; - - dObject = this; + typeof(this).alloc.initWithCapacity(capacity); } Object initWithLength (NSUInteger length) @@ -322,18 +240,12 @@ this (NSUInteger length) { - objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); - id result = Bridge.invokeObjcMethod!(id, "initWithLength:", NSUInteger)(objcObject, length); - - if (result) - objcObject = ret; + typeof(this).alloc.initWithLength(length); + } +`; - dObject = this; - } -} +const TNSExtendedMutableData = ` -template TNSExtendedMutableData () -{ void appendBytes (void* bytes, NSUInteger length) { return invokeObjcSelf!(void, "appendBytes:length:", void*, NSUInteger)(bytes, length); @@ -368,10 +280,10 @@ { return invokeObjcSelf!(void, "replaceBytesInRange:withBytes:length:", NSRange, void*, NSUInteger)(range, replacementBytes, replacementLength); } -} +`; -template TNSExtendedData () -{ +const TNSExtendedData = ` + NSString description () { return invokeObjcSelf!(NSString, "description"); @@ -421,5 +333,363 @@ { return invokeObjcSelf!(bool, "writeToURL:options:error:", NSURL, NSUInteger, NSError**)(url, writeOptionsMask, errorPtr); } +`; + +class NSData : NSObject, INSCopying, INSMutableCopying, INSCoding +{ + mixin (ObjcWrap); + + this () + { + super(typeof(this).alloc.init.objcObject); + } + + typeof(this) init () + { + return invokeObjcSelf!(typeof(this), "init"); + } + + NSUInteger length () + { + return invokeObjcSelf!(NSUInteger, "length"); + } + + void* bytes () + { + return invokeObjcSelf!(void*, "bytes"); + } + + Object copyWithZone (NSZone* zone) + { + return invokeObjcSelf!(Object, "copyWithZone:", NSZone*)(zone); + } + + Object mutableCopyWithZone (NSZone* zone) + { + return invokeObjcSelf!(Object, "mutableCopyWithZone:", NSZone*)(zone); + } + + void encodeWithCoder (NSCoder aCoder) + { + return invokeObjcSelf!(void, "encodeWithCoder:", NSCoder)(aCoder); + } + + Object initWithCoder (NSCoder aDecoder) + { + return invokeObjcSelf!(Object, "initWithCoder:", NSCoder)(aDecoder); + } + + this (NSCoder aDecoder) + { + typeof(this).alloc.initWithCoder(aDecoder); + } + + // TNSDataCreation + static Object data () + { + return invokeObjcSuperClass!(Object, "data"); + } + + static Object dataWithBytes (void* bytes, NSUInteger length) + { + return invokeObjcSuperClass!(Object, "dataWithBytes:length:", void*, NSUInteger)(bytes, length); + } + + static Object dataWithBytesNoCopy (void* bytes, NSUInteger length) + { + return invokeObjcSuperClass!(Object, "dataWithBytesNoCopy:length:", void*, NSUInteger)(bytes, length); + } + + static Object dataWithBytesNoCopy (void* bytes, NSUInteger length, bool b) + { + return invokeObjcSuperClass!(Object, "dataWithBytesNoCopy:length:freeWhenDone:", void*, NSUInteger, bool)(bytes, length, b); + } + + static Object dataWithContentsOfFile (NSString path, NSUInteger readOptionsMask, ref NSError errorPtr) + { + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSuperClass!(Object, "dataWithContentsOfFile:options:error:", NSString, NSUInteger, id*)(path, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; + } + + static Object dataWithContentsOfURL (NSURL url, NSUInteger readOptionsMask, ref NSError errorPtr) + { + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSuperClass!(Object, "dataWithContentsOfURL:options:error:", NSURL, NSUInteger, id*)(url, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; + } + + static Object dataWithContentsOfFile (NSString path) + { + return invokeObjcSuperClass!(Object, "dataWithContentsOfFile:", NSString)(path); + } + + static Object dataWithContentsOfURL (NSURL url) + { + return invokeObjcSuperClass!(Object, "dataWithContentsOfURL:", NSURL)(url); + } + + static Object dataWithContentsOfMappedFile (NSString path) + { + return invokeObjcSuperClass!(Object, "dataWithContentsOfMappedFile:", NSString)(path); + } + + Object initWithBytes (void* bytes, NSUInteger length) + { + return invokeObjcSelf!(Object, "initWithBytes:length:", void*, NSUInteger)(bytes, length); + } + + this (void* bytes, NSUInteger length) + { + typeof(this).alloc.initWithBytes(bytes, length); + } + + Object initWithBytesNoCopy (void* bytes, NSUInteger length) + { + return invokeObjcSelf!(Object, "initWithBytesNoCopy:length:", void*, NSUInteger)(bytes, length); + } + + Object initWithBytesNoCopy (void* bytes, NSUInteger length, bool b) + { + return invokeObjcSelf!(Object, "initWithBytesNoCopy:length:freeWhenDone:", void*, NSUInteger, bool)(bytes, length, b); + } + + this (void* bytes, NSUInteger length, bool b) + { + typeof(this).alloc.initWithBytesNoCopy(bytes, length, b); + } + + Object initWithContentsOfFile (NSString path, NSUInteger readOptionsMask, ref NSError errorPtr) + { + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSelf!(Object, "initWithContentsOfFile:options:error:", NSString, NSUInteger, id*)(path, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; + } + + this (NSString path, NSUInteger readOptionsMask, ref NSError errorPtr) + { + typeof(this).alloc.initWithContentsOfFile(path, readOptionsMask, errorPtr); + } + + Object initWithContentsOfURL (NSURL url, NSUInteger readOptionsMask, ref NSError errorPtr) + { + id error; + + if (errorPtr) + error = new objc_object; + + Object result = invokeObjcSelf!(Object, "initWithContentsOfURL:options:error:", NSURL, NSUInteger, id*)(url, readOptionsMask, &error); + + if (error) + errorPtr = new NSError(error); + + return result; + } + + this (NSURL url, NSUInteger readOptionsMask, ref NSError errorPtr) + { + typeof(this).alloc.initWithContentsOfURL(url, readOptionsMask, errorPtr); + } + + Object initWithContentsOfFile (NSString path) + { + return invokeObjcSelf!(Object, "initWithContentsOfFile:", NSString)(path); + } + + this (NSString path) + { + typeof(this).alloc.initWithContentsOfFile(path); + } + + Object initWithContentsOfURL (NSURL url) + { + return invokeObjcSelf!(Object, "initWithContentsOfURL:", NSURL)(url); + } + + this (NSURL url) + { + typeof(this).alloc.initWithContentsOfURL(url); + } + + Object initWithContentsOfMappedFile (NSString path) + { + return invokeObjcSelf!(Object, "initWithContentsOfMappedFile:", NSString)(path); + } + + Object initWithData (NSData data) + { + return invokeObjcSelf!(Object, "initWithData:", NSData)(data); + } + + this (NSData data) + { + typeof(this).alloc.initWithData(data); + } + + static Object dataWithData (NSData data) + { + return invokeObjcSuperClass!(Object, "dataWithData:", NSData)(data); + } + + // TNSExtendedData + NSString description () + { + return invokeObjcSelf!(NSString, "description"); + } + + void getBytes (void* buffer) + { + return invokeObjcSelf!(void, "getBytes:", void*)(buffer); + } + + void getBytes (void* buffer, NSUInteger length) + { + return invokeObjcSelf!(void, "getBytes:length:", void*, NSUInteger)(buffer, length); + } + + void getBytes (void* buffer, NSRange range) + { + return invokeObjcSelf!(void, "getBytes:range:", void*, NSRange)(buffer, range); + } + + bool isEqualToData (NSData other) + { + return invokeObjcSelf!(bool, "isEqualToData:", NSData)(other); + } + + NSData subdataWithRange (NSRange range) + { + return invokeObjcSelf!(NSData, "subdataWithRange:", NSRange)(range); + } + + bool writeToFile (NSString path, bool useAuxiliaryFile) + { + return invokeObjcSelf!(bool, "writeToFile:atomically:", NSString, bool)(path, useAuxiliaryFile); + } + + bool writeToURL (NSURL url, bool atomically) + { + return invokeObjcSelf!(bool, "writeToURL:atomically:", NSURL, bool)(url, atomically); + } + + bool writeToFile (NSString path, NSUInteger writeOptionsMask, NSError** errorPtr) + { + return invokeObjcSelf!(bool, "writeToFile:options:error:", NSString, NSUInteger, NSError**)(path, writeOptionsMask, errorPtr); + } + + bool writeToURL (NSURL url, NSUInteger writeOptionsMask, NSError** errorPtr) + { + return invokeObjcSelf!(bool, "writeToURL:options:error:", NSURL, NSUInteger, NSError**)(url, writeOptionsMask, errorPtr); + } } +class NSMutableData : NSData +{ + mixin (ObjcWrap); + + this () + { + super(typeof(this).alloc.init.objcObject); + } + + typeof(this) init () + { + return invokeObjcSelf!(typeof(this), "init"); + } + + void* mutableBytes () + { + return invokeObjcSelf!(void*, "mutableBytes"); + } + + void setLength (NSUInteger length) + { + return invokeObjcSelf!(void, "setLength:", NSUInteger)(length); + } + + // TNSMutableDataCreation + static Object dataWithCapacity (NSUInteger aNumItems) + { + return invokeObjcSuperClass!(Object, "dataWithCapacity:", NSUInteger)(aNumItems); + } + + static Object dataWithLength (NSUInteger length) + { + return invokeObjcSuperClass!(Object, "dataWithLength:", NSUInteger)(length); + } + + Object initWithCapacity (NSUInteger capacity) + { + return invokeObjcSelf!(Object, "initWithCapacity:", NSUInteger)(capacity); + } + + Object initWithLength (NSUInteger length) + { + return invokeObjcSelf!(Object, "initWithLength:", NSUInteger)(length); + } + + this (NSUInteger length) + { + typeof(this).alloc.initWithLength(length); + } + + // TNSExtendedMutableData + void appendBytes (void* bytes, NSUInteger length) + { + return invokeObjcSelf!(void, "appendBytes:length:", void*, NSUInteger)(bytes, length); + } + + void appendData (NSData other) + { + return invokeObjcSelf!(void, "appendData:", NSData)(other); + } + + void increaseLengthBy (NSUInteger extraLength) + { + return invokeObjcSelf!(void, "increaseLengthBy:", NSUInteger)(extraLength); + } + + void replaceBytesInRange (NSRange range, void* bytes) + { + return invokeObjcSelf!(void, "replaceBytesInRange:withBytes:", NSRange, void*)(range, bytes); + } + + void resetBytesInRange (NSRange range) + { + return invokeObjcSelf!(void, "resetBytesInRange:", NSRange)(range); + } + + void setData (NSData data) + { + return invokeObjcSelf!(void, "setData:", NSData)(data); + } + + void replaceBytesInRange (NSRange range, void* replacementBytes, NSUInteger replacementLength) + { + return invokeObjcSelf!(void, "replaceBytesInRange:withBytes:length:", NSRange, void*, NSUInteger)(range, replacementBytes, replacementLength); + } +} \ No newline at end of file