-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Achille Roussel
committed
May 20, 2016
1 parent
2857ef7
commit c368e43
Showing
8 changed files
with
211 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// +build darwin | ||
|
||
package cocoa | ||
|
||
import ( | ||
"image" | ||
"runtime" | ||
) | ||
|
||
// The Image type wraps and manages a CGImageRef value. | ||
// | ||
// The intent for Image values is to provide automatic memory management for | ||
// native values from the Cocoa framework by leveraging the Go GC, basically | ||
// the lifetime of the managed CGImageRef is bound to the lifetime of the | ||
// Image instance. | ||
// | ||
// The Image type also offers an API that betters integrates with Go code by | ||
// implementing standard interfaces and exposing methods. | ||
type Image struct { | ||
ref CGImageRef | ||
} | ||
|
||
// NewImage creates a new Image value with a content equivalent to the Go | ||
// image passed as argument. | ||
func NewImage(img image.Image) *Image { | ||
return NewImageWrap(CGImageCreate(img)) | ||
} | ||
|
||
// NewImageWarp creates a new Image value that wraps the CGImageRef value | ||
// passed as argument. | ||
// | ||
// The returned Image value becomes the owner of the CGImageRef, it will | ||
// automatically release it when it gets garbage collected. | ||
func NewImageWrap(ref CGImageRef) *Image { | ||
img := &Image{ref} | ||
runtime.SetFinalizer(img, (*Image).release) | ||
return img | ||
} | ||
|
||
// Ref returns the CGImageRef wrapped by the Image value it is called on. | ||
func (img *Image) Ref() CGImageRef { | ||
return img.ref | ||
} | ||
|
||
func (img *Image) release() { | ||
if ref := img.ref; ref != nil { | ||
img.ref = nil | ||
CFRelease(CFTypeRef(ref)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// +build darwin | ||
|
||
package cocoa | ||
|
||
import ( | ||
"bufio" | ||
"image" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var gopher image.Image | ||
|
||
func init() { | ||
f, _ := os.Open("fixtures/gopher.png") | ||
gopher, _, _ = image.Decode(bufio.NewReader(f)) | ||
f.Close() | ||
} | ||
|
||
func TestNewImage(t *testing.T) { | ||
img := NewImage(gopher) | ||
|
||
if img.Ref() == nil { | ||
t.Error("invalid nil image reference") | ||
} | ||
} | ||
|
||
func TestNewImageWrap(t *testing.T) { | ||
img1 := CGImageCreate(gopher) | ||
img2 := NewImageWrap(img1) | ||
|
||
if ref := img2.Ref(); ref != img1 { | ||
t.Error("invalid image reference:", ref) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// +build darwin | ||
|
||
package cocoa | ||
|
||
import "runtime" | ||
|
||
// The String type wraps and manages a CFStringRef value. | ||
// | ||
// The intent for String values is to provide automatic memory management for | ||
// native values from the Cocoa framework by leveraging the Go GC, basically | ||
// the lifetime of the managed CFStringRef is bound to the lifetime of the | ||
// String instance. | ||
// | ||
// The String type also offers an API that betters integrates with Go code by | ||
// implementing standard interfaces and exposing methods. | ||
type String struct { | ||
ref CFStringRef | ||
} | ||
|
||
// NewString creates a new String value with a content equivalent to the Go | ||
// string passed as argument. | ||
func NewString(s string) *String { | ||
return NewStringWrap(CFStringCreate(s)) | ||
} | ||
|
||
// NewStringWarp creates a new String value that wraps the CFStringRef value | ||
// passed as argument. | ||
// | ||
// The returned String value becomes the owner of the CFStringRef, it will | ||
// automatically release it when it gets garbage collected. | ||
func NewStringWrap(ref CFStringRef) *String { | ||
s := &String{ref} | ||
runtime.SetFinalizer(s, (*String).release) | ||
return s | ||
} | ||
|
||
// String returns the content of the String object as a Go string value. | ||
func (s *String) String() string { | ||
if s.ref == nil { | ||
return "" | ||
} | ||
return GoString(s.ref) | ||
} | ||
|
||
// Ref returns the CFStringRef wrapped by the String value it is called on. | ||
func (s *String) Ref() CFStringRef { | ||
return s.ref | ||
} | ||
|
||
func (s *String) release() { | ||
if ref := s.ref; ref != nil { | ||
s.ref = nil | ||
CFRelease(CFTypeRef(ref)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// +build darwin | ||
|
||
package cocoa | ||
|
||
import "testing" | ||
|
||
func TestNewString(t *testing.T) { | ||
tests := []string{ | ||
"", | ||
"0123456789", | ||
"Hello World!", | ||
"你好", | ||
} | ||
|
||
for _, test := range tests { | ||
s1 := NewString(test) | ||
s2 := s1.String() | ||
|
||
if s2 != test { | ||
t.Error("invalid string:", s2, "!=", test) | ||
} | ||
} | ||
} | ||
|
||
func TestNewStringWrap(t *testing.T) { | ||
tests := []string{ | ||
"", | ||
"0123456789", | ||
"Hello World!", | ||
"你好", | ||
} | ||
|
||
for _, test := range tests { | ||
s1 := CFStringCreate(test) | ||
s2 := NewStringWrap(s1) | ||
|
||
if s2.Ref() != s1 { | ||
t.Error("invalid string ref:", s1, "!=", s2) | ||
} | ||
} | ||
} | ||
|
||
func TestStringZeroValue(t *testing.T) { | ||
s1 := &String{} | ||
s2 := s1.String() | ||
|
||
if s2 != "" { | ||
t.Error("string zero-value produced an invalid Go string:", s2) | ||
} | ||
} | ||
|
||
func TestStringRelease(t *testing.T) { | ||
s := NewString("") | ||
s.release() | ||
|
||
if s.ref != nil { | ||
t.Error("releasing didn't set the internal string reference to nil") | ||
} | ||
} |