Skip to content

Commit

Permalink
Merge e8fc8a6 into 98a9d72
Browse files Browse the repository at this point in the history
  • Loading branch information
Arimeka committed Nov 21, 2019
2 parents 98a9d72 + e8fc8a6 commit b2a7249
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Argument contains key-gValue for set it to *C.VipsOperation
type Argument struct {
cName *C.char
gValue *GValue
gValue Value

mu sync.RWMutex
}
Expand All @@ -25,7 +25,7 @@ func (a *Argument) name() *C.char {
return a.cName
}

func (a *Argument) value() *GValue {
func (a *Argument) value() Value {
a.mu.RLock()
defer a.mu.RUnlock()

Expand Down
9 changes: 9 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package imgvips
#include "vips/vips.h"
*/
import "C"
import (
"unsafe"
)

// Image wrapper around *C.VipsImage
type Image struct {
Expand All @@ -13,6 +16,12 @@ type Image struct {
val *GValue
}

// Ptr return unsafe pointer to *C.VipsImage
// Return nil if image was freed
func (i *Image) Ptr() unsafe.Pointer {
return unsafe.Pointer(i.image)
}

// Width return image width
// Return 0 if image was freed
func (i *Image) Width() int {
Expand Down
10 changes: 5 additions & 5 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Operation struct {

// AddInput adds argument for set to operation.
// After call *Operation.Exec(), all values from input arguments will be freed.
func (op *Operation) AddInput(name string, value *GValue) {
func (op *Operation) AddInput(name string, value Value) {
op.mu.Lock()
defer op.mu.Unlock()

Expand All @@ -56,7 +56,7 @@ func (op *Operation) AddInput(name string, value *GValue) {
// AddOutput adds argument for get from operation.
// After call Exec(), all values from output arguments will be updated from operation result.
// This arguments will be freed after call *Operation.Free()
func (op *Operation) AddOutput(name string, value *GValue) {
func (op *Operation) AddOutput(name string, value Value) {
op.mu.Lock()
defer op.mu.Unlock()

Expand All @@ -81,18 +81,18 @@ func (op *Operation) Exec() error {
}

for _, arg := range op.inputs {
C.g_object_set_property((*C.GObject)(unsafe.Pointer(op.operation)), arg.name(), arg.value().value())
C.g_object_set_property((*C.GObject)(unsafe.Pointer(op.operation)), arg.name(), (*C.GValue)(arg.value().Ptr()))
}
for _, arg := range op.outputs {
C.g_object_set_property((*C.GObject)(unsafe.Pointer(op.operation)), arg.name(), arg.value().value())
C.g_object_set_property((*C.GObject)(unsafe.Pointer(op.operation)), arg.name(), (*C.GValue)(arg.value().Ptr()))
}

if success := C.vips_cache_operation_buildp(&op.operation); success != 0 {
return vipsError()
}

for _, arg := range op.outputs {
C.g_object_get_property((*C.GObject)(unsafe.Pointer(op.operation)), arg.name(), arg.value().value())
C.g_object_get_property((*C.GObject)(unsafe.Pointer(op.operation)), arg.name(), (*C.GValue)(arg.value().Ptr()))
}

return nil
Expand Down
14 changes: 11 additions & 3 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ var (
ErrCopyForbidden = errors.New("copy forbidden for this type")
)

// Value is interface for create own value for operation argument
type Value interface {
// Free freed data in C
Free()
// Ptr return unsafe pointer to *C.GValue
Ptr() unsafe.Pointer
}

// GValue contains glib gValue and its type
type GValue struct {
gType C.GType
Expand All @@ -28,12 +36,12 @@ type GValue struct {
mu sync.RWMutex
}

// gValue return gValue *C.gValue
func (v *GValue) value() *C.GValue {
// Ptr return unsafe pointer to *C.GValue
func (v *GValue) Ptr() unsafe.Pointer {
v.mu.RLock()
defer v.mu.RUnlock()

return v.gValue
return unsafe.Pointer(v.gValue)
}

// Copy create new instance of *GValue with new *C.gValue and run copy() func
Expand Down
4 changes: 2 additions & 2 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ func compareImageValsFull(t *testing.T, val1, val2 *imgvips.GValue) {
if result2 == nil {
t.Error("Expected val1 contain image")
}
if result2 == result1 {
t.Errorf("Expected val2 contain %p different from val1 %p", result2, result1)
if result2.Ptr() == result1.Ptr() {
t.Errorf("Expected val2 contain %p different from val1 %p", result2.Ptr(), result1.Ptr())
}
}

Expand Down

0 comments on commit b2a7249

Please sign in to comment.