Permalink
Browse files

implement more of GVariant

  • Loading branch information...
jmoiron committed Jun 25, 2016
1 parent 4bb8ce8 commit 33d56ae1ff0784cde5a7f7873c5045e8d4d5a06a
Showing with 65 additions and 16 deletions.
  1. +65 −16 glib/gvariant.go
View
@@ -8,7 +8,10 @@ package glib
// #include "glib.go.h"
import "C"
import "unsafe"
import (
"fmt"
"unsafe"
)
/*
* GVariant
@@ -63,6 +66,30 @@ func (v *Variant) Native() uintptr {
return uintptr(unsafe.Pointer(v.native()))
}
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())))
}
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()))
}
func (v *Variant) GetBoolean() bool {
return gobool(C.g_variant_get_boolean(v.native()))
}
func (v *Variant) GetString() string {
var len C.gsize
gc := C.g_variant_get_string(v.native(), &len)
defer C.g_free(gc)
return C.GoStringN((*C.char)(gc), (C.int)(len))
}
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
@@ -78,36 +105,58 @@ func (v *Variant) GetStrv() []string {
return strs
}
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())))
func (v *Variant) GetInt() (int64, error) {
t := v.Type().String()
var i int64
switch t {
case "y":
i = int64(C.g_variant_get_byte(v.native()))
case "n":
i = int64(C.g_variant_get_int16(v.native()))
case "q":
i = int64(C.g_variant_get_uint16(v.native()))
case "i":
i = int64(C.g_variant_get_int32(v.native()))
case "u":
i = int64(C.g_variant_get_uint32(v.native()))
case "x":
i = int64(C.g_variant_get_int64(v.native()))
case "t":
i = int64(C.g_variant_get_uint64(v.native()))
default:
return 0, fmt.Errorf("variant type %s not an integer type", t)
}
return i, nil
}
func (v *Variant) IsContainer() bool {
return gobool(C.g_variant_is_container(v.native()))
func (v *Variant) Type() *VariantType {
return newVariantType(C.g_variant_get_type(v.native()))
}
func (v *Variant) IsFloating() bool {
return gobool(C.g_variant_is_floating(v.native()))
func (v *Variant) IsType(t *VariantType) bool {
return gobool(C.g_variant_is_of_type(v.native(), t.native()))
}
func (v *Variant) GetBoolean() bool {
return gobool(C.g_variant_get_boolean(v.native()))
// String wraps g_variant_print(). It returns a string understood
// by g_variant_parse().
func (v *Variant) String() string {
gc := C.g_variant_print(v.native(), gbool(false))
defer C.g_free(gc)
return C.GoString((*C.char)(gc))
}
func (v *Variant) GetString() string {
var len C.gsize
gc := C.g_variant_get_string(v.native(), &len)
// 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)
return C.GoStringN((*C.char)(gc), (C.int)(len))
return C.GoString((*C.char)(gc))
}
//void g_variant_unref ()
//GVariant * g_variant_ref ()
//GVariant * g_variant_ref_sink ()
//GVariant * g_variant_take_ref ()
//const GVariantType * g_variant_get_type ()
//gboolean g_variant_is_of_type ()
//gint g_variant_compare ()
//GVariantClass g_variant_classify ()
//gboolean g_variant_check_format_string ()

3 comments on commit 33d56ae

@tonyhb

This comment has been minimized.

Show comment
Hide comment
@tonyhb

tonyhb Jun 26, 2016

it really does things by strings, eh?

tonyhb replied Jun 26, 2016

it really does things by strings, eh?

@jmoiron

This comment has been minimized.

Show comment
Hide comment
@jmoiron

jmoiron Jun 29, 2016

Owner

it seems to, i am not an expert on glib stuff ^_^;

Owner

jmoiron replied Jun 29, 2016

it seems to, i am not an expert on glib stuff ^_^;

@tonyhb

This comment has been minimized.

Show comment
Hide comment
@tonyhb

tonyhb Jun 29, 2016

haha me neither! just browsing through the timeline and saw this. it looks cool!

tonyhb replied Jun 29, 2016

haha me neither! just browsing through the timeline and saw this. it looks cool!

Please sign in to comment.