| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 seq | |
| 6 | |
| 7 //#cgo LDFLAGS: -llog | |
| 8 //#include <android/log.h> | |
| 9 //#include <string.h> | |
| 10 //import "C" | |
| 11 | |
| 12 import ( | |
| 13 "fmt" | |
| 14 "sync" | |
| 15 ) | |
| 16 | |
| 17 // refs stores Go objects that have been passed to another language. | |
| 18 var refs struct { | |
| 19 sync.Mutex | |
| 20 next int32 // next reference number to use for Go object, always negativ
e | |
| 21 refs map[interface{}]int32 | |
| 22 objs map[int32]interface{} | |
| 23 } | |
| 24 | |
| 25 func init() { | |
| 26 refs.Lock() | |
| 27 refs.next = -24 // Go objects get negative reference numbers. Arbitrary
starting point. | |
| 28 refs.refs = make(map[interface{}]int32) | |
| 29 refs.objs = make(map[int32]interface{}) | |
| 30 refs.Unlock() | |
| 31 } | |
| 32 | |
| 33 // A Ref represents a Java or Go object passed across the language | |
| 34 // boundary. | |
| 35 type Ref struct { | |
| 36 Num int32 | |
| 37 } | |
| 38 | |
| 39 // Get returns the underlying object. | |
| 40 func (r *Ref) Get() interface{} { | |
| 41 refs.Lock() | |
| 42 obj, ok := refs.objs[r.Num] | |
| 43 refs.Unlock() | |
| 44 if !ok { | |
| 45 panic(fmt.Sprintf("unknown ref %d", r.Num)) | |
| 46 } | |
| 47 return obj | |
| 48 } | |
| 49 | |
| 50 // Delete remove the reference to the underlying object. | |
| 51 func Delete(num int32) { | |
| 52 refs.Lock() | |
| 53 obj, ok := refs.objs[num] | |
| 54 if !ok { | |
| 55 panic(fmt.Sprintf("seq.Delete unknown refnum: %d", num)) | |
| 56 } | |
| 57 delete(refs.objs, num) | |
| 58 delete(refs.refs, obj) | |
| 59 refs.Unlock() | |
| 60 } | |
| OLD | NEW |