From: Nick Rosbrook <rosbro...@ainfosec.com>

Define Defbool as struct analagous to the C type, and define the type
'defboolVal' that represent true, false, and default defbool values.

Implement Set, Unset, SetIfDefault, IsDefault, Val, and String functions
on Defbool so that the type can be used in Go analagously to how its
used in C.

Finally, implement fromC and toC functions.

Signed-off-by: Nick Rosbrook <rosbro...@ainfosec.com>
Reviewed-by: George Dunlap <george.dun...@citrix.com>
---
 tools/golang/xenlight/xenlight.go | 93 +++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)

diff --git a/tools/golang/xenlight/xenlight.go 
b/tools/golang/xenlight/xenlight.go
index 89ed439fd0..640d82f35f 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -85,6 +85,99 @@ type MemKB uint64
 
 type Uuid C.libxl_uuid
 
+// defboolVal represents a defbool value.
+type defboolVal int
+
+const (
+       defboolDefault defboolVal = 0
+       defboolFalse   defboolVal = -1
+       defboolTrue    defboolVal = 1
+)
+
+// Defbool represents a libxl_defbool.
+type Defbool struct {
+       val defboolVal
+}
+
+func (d Defbool) String() string {
+       switch d.val {
+       case defboolDefault:
+               return "<default>"
+       case defboolFalse:
+               return "False"
+       case defboolTrue:
+               return "True"
+       }
+
+       return ""
+}
+
+// Set sets the value of the Defbool.
+func (d *Defbool) Set(b bool) {
+       if b {
+               d.val = defboolTrue
+               return
+       }
+       d.val = defboolFalse
+}
+
+// Unset resets the Defbool to default value.
+func (d *Defbool) Unset() {
+       d.val = defboolDefault
+}
+
+// SetIfDefault sets the value of Defbool only if
+// its current value is default.
+func (d *Defbool) SetIfDefault(b bool) {
+       if d.IsDefault() {
+               d.Set(b)
+       }
+}
+
+// IsDefault returns true if the value of Defbool
+// is default, returns false otherwise.
+func (d *Defbool) IsDefault() bool {
+       return d.val == defboolDefault
+}
+
+// Val returns the boolean value associated with the
+// Defbool value. An error is returned if the value
+// is default.
+func (d *Defbool) Val() (bool, error) {
+       if d.IsDefault() {
+               return false, fmt.Errorf("%v: cannot take value of default 
defbool", ErrorInval)
+       }
+
+       return (d.val > 0), nil
+}
+
+func (d *Defbool) fromC(c *C.libxl_defbool) error {
+       if C.libxl_defbool_is_default(*c) {
+               d.val = defboolDefault
+               return nil
+       }
+
+       if C.libxl_defbool_val(*c) {
+               d.val = defboolTrue
+               return nil
+       }
+
+       d.val = defboolFalse
+
+       return nil
+}
+
+func (d *Defbool) toC() (C.libxl_defbool, error) {
+       var c C.libxl_defbool
+
+       if !d.IsDefault() {
+               val, _ := d.Val()
+               C.libxl_defbool_set(&c, C.bool(val))
+       }
+
+       return c, nil
+}
+
 type Context struct {
        ctx    *C.libxl_ctx
        logger *C.xentoollog_logger_stdiostream
-- 
2.19.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reply via email to