Permalink
Browse files

fix some calls to g_free, fix lint doc issues

  • Loading branch information...
jmoiron committed Oct 2, 2016
1 parent 33d56ae commit 4776f2bbe5454c8097f4fda707adab79b5ed0de4
Showing with 36 additions and 15 deletions.
  1. +30 −14 glib/gvariant.go
  2. +6 −1 glib/gvarianttype.go
View
@@ -20,24 +20,26 @@ import (
// IVariant is an interface type implemented by Variant and all types which embed
// an Variant. It is meant to be used as a type for function arguments which
// require GVariants or any subclasses thereof.
type IVariant interface {
ToGVariant() *C.GVariant
ToVariant() *Variant
}
// Variant is a representation of GLib's GVariant.
// A Variant is a representation of GLib's GVariant.
type Variant struct {
GVariant *C.GVariant
}
// ToGVariant exposes the underlying *C.GVariant type for this Variant,
// necessary to implement IVariant.
func (v *Variant) ToGVariant() *C.GVariant {
if v == nil {
return nil
}
return v.native()
}
// ToVariant returns this Variant, necessary to implement IVariant.
func (v *Variant) ToVariant() *Variant {
return v
}
@@ -47,55 +49,64 @@ func newVariant(p *C.GVariant) *Variant {
return &Variant{GVariant: p}
}
func VariantFromUnsafePointer(p unsafe.Pointer) *Variant {
return &Variant{C.toGVariant(p)}
}
// VariantFromUnsafePointer returns a Variant from an unsafe pointer.
// XXX: unnecessary footgun?
//func VariantFromUnsafePointer(p unsafe.Pointer) *Variant {
// return &Variant{C.toGVariant(p)}
//}
// native returns a pointer to the underlying GVariant.
func (v *Variant) native() *C.GVariant {
if v == nil || v.GVariant == nil {
return nil
}
return v.GVariant
p := unsafe.Pointer(v.GVariant)
return C.toGVariant(p)
}
// Native returns a pointer to the underlying GVariant.
func (v *Variant) Native() uintptr {
return uintptr(unsafe.Pointer(v.native()))
}
// TypeString returns the g variant type string for this variant.
func (v *Variant) TypeString() string {
// the string returned from this belongs to GVariant and must not be freed.
return C.GoString((*C.char)(C.g_variant_get_type_string(v.native())))
}
// IsContainer returns true if the variant is a container and false otherwise.
func (v *Variant) IsContainer() bool {
return gobool(C.g_variant_is_container(v.native()))
}
func (v *Variant) IsFloating() bool {
return gobool(C.g_variant_is_floating(v.native()))
}
// IsFloating returns true if the variant has a floating reference count.
// XXX: this isn't useful without ref_sink/take_ref, which are themselves
// perhaps not useful for most Go code that may use variants.
//func (v *Variant) IsFloating() bool {
// return gobool(C.g_variant_is_floating(v.native()))
//}
// GetBoolean returns the bool value of this variant.
func (v *Variant) GetBoolean() bool {
return gobool(C.g_variant_get_boolean(v.native()))
}
// GetString returns the string value of the variant.
func (v *Variant) GetString() string {
var len C.gsize
gc := C.g_variant_get_string(v.native(), &len)
defer C.g_free(gc)
defer C.g_free(C.gpointer(gc))
return C.GoStringN((*C.char)(gc), (C.int)(len))
}
// GetStrv returns a slice of strings from this variant. It wraps
// g_variant_get_strv, but returns copies of the strings instead.
func (v *Variant) GetStrv() []string {
gstrv := C.g_variant_get_strv(v.native(), nil)
// we do not own the memory for these strings, so we must not use strfreev
// but we must free the actual pointer we receive.
c := gstrv
defer C.g_free(gstrv)
defer C.g_free(C.gpointer(gstrv))
var strs []string
for *c != nil {
@@ -105,6 +116,9 @@ func (v *Variant) GetStrv() []string {
return strs
}
// GetInt returns the int64 value of the variant if it is an integer type, and
// an error otherwise. It wraps variouns `g_variant_get_*` functions dealing
// with integers of different sizes.
func (v *Variant) GetInt() (int64, error) {
t := v.Type().String()
var i int64
@@ -129,10 +143,12 @@ func (v *Variant) GetInt() (int64, error) {
return i, nil
}
// Type returns the VariantType for this variant.
func (v *Variant) Type() *VariantType {
return newVariantType(C.g_variant_get_type(v.native()))
}
// IsType returns true if the variant's type matches t.
func (v *Variant) IsType(t *VariantType) bool {
return gobool(C.g_variant_is_of_type(v.native(), t.native()))
}
@@ -141,15 +157,15 @@ func (v *Variant) IsType(t *VariantType) bool {
// by g_variant_parse().
func (v *Variant) String() string {
gc := C.g_variant_print(v.native(), gbool(false))
defer C.g_free(gc)
defer C.g_free(C.gpointer(gc))
return C.GoString((*C.char)(gc))
}
// AnnotatedString wraps g_variant_print(), but returns a type-annotated
// string.
func (v *Variant) AnnotatedString() string {
gc := C.g_variant_print(v.native(), gbool(true))
defer C.g_free(gc)
defer C.g_free(C.gpointer(gc))
return C.GoString((*C.char)(gc))
}
View
@@ -10,6 +10,8 @@ package glib
// #include "gvarianttype.go.h"
import "C"
// A VariantType is a wrapper for the GVariantType, which encodes type
// information for GVariants.
type VariantType struct {
GVariantType *C.GVariantType
}
@@ -18,16 +20,19 @@ func (v *VariantType) native() *C.GVariantType {
return v.GVariantType
}
// String returns a copy of this VariantType's type string.
func (v *VariantType) String() string {
ch := C.g_variant_type_dup_string(v.native())
defer C.g_free(ch)
defer C.g_free(C.gpointer(ch))
return C.GoString((*C.char)(ch))
}
func newVariantType(v *C.GVariantType) *VariantType {
return &VariantType{v}
}
// Variant types for comparing between them. Cannot be const because
// they are pointers.
var (
VARIANT_TYPE_BOOLEAN = newVariantType(C._G_VARIANT_TYPE_BOOLEAN)
VARIANT_TYPE_BYTE = newVariantType(C._G_VARIANT_TYPE_BYTE)

0 comments on commit 4776f2b

Please sign in to comment.