github.com/F4RD1N/gomobile@v1.0.1/bind/objc/seq_darwin.go.support (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  // Go support functions for Objective-C. Note that this
     8  // file is copied into and compiled with the generated
     9  // bindings.
    10  
    11  /*
    12  #cgo CFLAGS: -x objective-c -fobjc-arc -fmodules -fblocks -Werror
    13  #cgo LDFLAGS: -framework Foundation
    14  
    15  #include <stdint.h>
    16  #include <stdlib.h>
    17  #include "seq.h"
    18  */
    19  import "C"
    20  
    21  import (
    22  	"unsafe"
    23  
    24  	"github.com/F4RD1N/gomobile/bind/seq"
    25  )
    26  
    27  // DestroyRef is called by Objective-C to inform Go it is done with a reference.
    28  //export DestroyRef
    29  func DestroyRef(refnum C.int32_t) {
    30  	seq.Delete(int32(refnum))
    31  }
    32  
    33  // encodeString copies a Go string and returns it as a nstring.
    34  func encodeString(s string) C.nstring {
    35  	n := C.int(len(s))
    36  	if n == 0 {
    37  		return C.nstring{}
    38  	}
    39  	ptr := C.malloc(C.size_t(n))
    40  	if ptr == nil {
    41  		panic("encodeString: malloc failed")
    42  	}
    43  	copy((*[1<<31 - 1]byte)(ptr)[:n], s)
    44  	return C.nstring{ptr: ptr, len: n}
    45  }
    46  
    47  // decodeString converts a nstring to a Go string. The
    48  // data in str is freed after use.
    49  func decodeString(str C.nstring) string {
    50  	if str.ptr == nil {
    51  		return ""
    52  	}
    53  	s := C.GoStringN((*C.char)(str.ptr), str.len)
    54  	C.free(str.ptr)
    55  	return s
    56  }
    57  
    58  // fromSlice converts a slice to a nbyteslice.
    59  // If cpy is set, a malloc'ed copy of the data is returned.
    60  func fromSlice(s []byte, cpy bool) C.nbyteslice {
    61  	if s == nil || len(s) == 0 {
    62  		return C.nbyteslice{}
    63  	}
    64  	ptr, n := unsafe.Pointer(&s[0]), C.int(len(s))
    65  	if cpy {
    66  		nptr := C.malloc(C.size_t(n))
    67  		if nptr == nil {
    68  			panic("fromSlice: malloc failed")
    69  		}
    70  		copy((*[1<<31 - 1]byte)(nptr)[:n], (*[1<<31 - 1]byte)(ptr)[:n])
    71  		ptr = nptr
    72  	}
    73  	return C.nbyteslice{ptr: ptr, len: n}
    74  }
    75  
    76  // toSlice takes a nbyteslice and returns a byte slice with the data. If cpy is
    77  // set, the slice contains a copy of the data. If not, the generated Go code
    78  // calls releaseByteSlice after use.
    79  func toSlice(s C.nbyteslice, cpy bool) []byte {
    80  	if s.ptr == nil || s.len == 0 {
    81  		return nil
    82  	}
    83  	var b []byte
    84  	if cpy {
    85  		b = C.GoBytes(s.ptr, C.int(s.len))
    86  		C.free(s.ptr)
    87  	} else {
    88  		b = (*[1<<31 - 1]byte)(unsafe.Pointer(s.ptr))[:s.len:s.len]
    89  	}
    90  	return b
    91  }