Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Interacting with C APIs

Interacting with C APIs

shibuyaswift #4

nakajijapan

June 08, 2016
Tweet

More Decks by nakajijapan

Other Decks in Technology

Transcript

  1. Teiten Teiten is an app that fixed-point observation a lot

    earnestly yourself by using a PC camera. http://teiten.nakajijapan.net/
  2. Sengiri " Sengiri " from the screen recording until the

    animated GIF file , you can create as soon as a simple operation . Sengiri, in English, is the shred. http://sengiri.nakajijapan.net/
  3. But

  4. C

  5. C int number = 10; int *p; *p = 100;

    printf("%d %x \n", *p, p); p = &number; printf("%d %x \n", *p, p);
  6. C char c[] = “abcd"; char *p; p = &c;

    printf("*p = %c\n",*p); printf("p = %s\n", p); printf("*(p + 1) = %c\n", *(c + 1)); printf("*p + 1 = %c\n", *c + 1); printf("c[1] = %c\n", c[1]); // *p = a // p = abcd // *(p + 1) = b // *p + 1 = b // c[1] = b
  7. C 516918f8 p c[] 516918dc p = &c; a b

    c d *(c + 1) *(c + 2) *(c + 3)
  8. C int number = 10; int *p; *p = 100;

    printf("%d %x \n", *p, p); p = &number; printf("%d %x \n", *p, p);
  9. Swift // int *p; var p = UnsafeMutablePointer<CInt>.alloc(1) // *p

    = 100; p.memory = 100 WBSNFNPSZ.FNPSZ\HFUOPONVUBUJOHTFU^ 6OTBGF.VUBCMF1PJOUFS
  10. C unsigned char c[] = “abcd"; unsigned char *p; p

    = &c; printf("*p = %c\n",*p); printf("p = %s\n", p); printf("*(p + 1) = %c\n", *(c + 1)); printf("*p + 1 = %c\n", *c + 1); printf("c[1] = %c\n", c[1]); // *p = a // p = abcd // *(p + 1) = b // *p + 1 = b // c[1] = b
  11. C unsigned char c[] = “abcd"; unsigned char *p; p

    = &c; printf("*p = %c\n",*p); printf("p = %s\n", p); printf("*(p + 1) = %c\n", *(c + 1)); printf("*p + 1 = %c\n", *c + 1); printf("c[1] = %c\n", c[1]); // *p = a // p = abcd // *(p + 1) = b // *p + 1 = b // c[1] = b
  12. Swift for i in 0..<256 { colorTypes.advancedBy(i).memory.Red = pixelColorMaps.advancedBy(i).memory.red colorTypes.advancedBy(i).memory.Green

    = pixelColorMaps.advancedBy(i).memory.green colorTypes.advancedBy(i).memory.Blue = pixelColorMaps.advancedBy(i).memory.blue }
  13. Swift for i in 0..<256 { colorTypes[i].Red = pixelColorMaps[i].red colorTypes[i].Green

    = pixelColorMaps[i].green colorTypes[i].Blue = pixelColorMaps[i].blue }
  14. Type Casting /// Returns the bits of `x`, interpreted as

    having type `U`. /// /// - Warning: Breaks the guarantees of Swift's type system; use /// with extreme care. There's almost always a better way to do /// anything. /// @warn_unused_result public func unsafeBitCast<T, U>(x: T, _: U.Type) -> U
  15. Type Casting /// Returns the bits of `x`, interpreted as

    having type `U`. /// /// - Warning: Breaks the guarantees of Swift's type system; use /// with extreme care. There's almost always a better way to do /// anything. /// @warn_unused_result public func unsafeBitCast<T, U>(x: T, _: U.Type) -> U
  16. Memory Management /* ANSI-C */ public func memchr(_: UnsafePointer<Void>, _:

    Int32, _: Int) -> UnsafeMutablePointer<Void> public func memcmp(_: UnsafePointer<Void>, _: UnsafePointer<Void>, _: Int) -> Int32 public func memcpy(_: UnsafeMutablePointer<Void>, _: UnsafePointer<Void>, _: Int) -> UnsafeMutablePointer<Void> public func memmove(_: UnsafeMutablePointer<Void>, _: UnsafePointer<Void>, _: Int) -> UnsafeMutablePointer<Void> public func memset(_: UnsafeMutablePointer<Void>, _: Int32, _: Int) -> UnsafeMutablePointer<Void>
  17. Swift let buffer = UnsafeMutablePointer<UInt8>.alloc( width * height * 4

    ) memset(buffer, 0, width * height * 4) (…snip…) free(buffer)
  18. Swift let buffer = UnsafeMutablePointer<UInt8>.alloc( width * height * 4

    ) memset(buffer, 0, width * height * 4) (…snip…) free(buffer) buffer.destroy()