[libnet-dbus-perl] 22/335: Support for strong typing, arrays & dictionaries

Intrigeri intrigeri at moszumanska.debian.org
Sat Mar 21 01:07:13 UTC 2015


This is an automated email from the git hooks/post-receive script.

intrigeri pushed a commit to branch experimental
in repository libnet-dbus-perl.

commit 111d961f88e5160950ef3ac72e1866b421750004
Author: Daniel P. Berrange <dan at berrange.com>
Date:   Sun Oct 31 14:39:50 2004 +0000

    Support for strong typing, arrays & dictionaries
---
 DBus.xs                          | 256 +++++++++++++++++++++++++++++++++++++--
 lib/Net/DBus.pm                  |  63 ++++++++++
 lib/Net/DBus/Binding/Iterator.pm |  97 ++++++++++++---
 lib/Net/DBus/Binding/Value.pm    |  78 ++++++++++++
 4 files changed, 465 insertions(+), 29 deletions(-)

diff --git a/DBus.xs b/DBus.xs
index eb0bbbe..f6e5e3f 100644
--- a/DBus.xs
+++ b/DBus.xs
@@ -1061,63 +1061,301 @@ void
 append_nil(iter)
         DBusMessageIter *iter;
     CODE:
-        dbus_message_iter_append_nil(iter);
+        if (!dbus_message_iter_append_nil(iter)) {
+          croak("cannot append nill");
+        }
 
 void
 append_boolean(iter, val)
         DBusMessageIter *iter;
         dbus_bool_t val;
     CODE:
-	dbus_message_iter_append_boolean(iter, val);
+	if (!dbus_message_iter_append_boolean(iter, val)) {
+          croak("cannot append boolean");
+        }
 
 void
 append_byte(iter, val)
         DBusMessageIter *iter;
         unsigned char val;
     CODE:
-	dbus_message_iter_append_byte(iter, val);
+	if (!dbus_message_iter_append_byte(iter, val)) {
+          croak("cannot append byte");
+        }
 
 void
 append_int32(iter, val)
         DBusMessageIter *iter;
         dbus_int32_t val;
     CODE:
-	dbus_message_iter_append_int32(iter, val);
+	if (!dbus_message_iter_append_int32(iter, val)) {
+          croak("cannot append int32");
+        }
 
 void
 append_uint32(iter, val)
         DBusMessageIter *iter;
         dbus_uint32_t val;
     CODE:
-        dbus_message_iter_append_uint32(iter, val);
+        if (!dbus_message_iter_append_uint32(iter, val)) {
+          croak("cannot append uint32");
+        }
 
 void
 _append_int64(iter, val)
         DBusMessageIter *iter;
         dbus_int64_t val;
     CODE:
-        dbus_message_iter_append_int64(iter, val);
+        if (!dbus_message_iter_append_int64(iter, val)) {
+          croak("cannot append int64");
+        }
 
 void
 _append_uint64(iter, val)
         DBusMessageIter *iter;
         dbus_uint64_t val;
     CODE:
-        dbus_message_iter_append_uint64(iter, val);
+        if (!dbus_message_iter_append_uint64(iter, val)) {
+          croak("cannot append uint64");
+        }
 
 void
 append_double(iter, val)
         DBusMessageIter *iter;
         double val;
     CODE:
-        dbus_message_iter_append_double(iter, val);
+        if (!dbus_message_iter_append_double(iter, val)) {
+          croak("cannot append double");
+        }
 
 void
 append_string(iter, val)
         DBusMessageIter *iter;
         char *val;
     CODE:
-        dbus_message_iter_append_string(iter, val);
+        if (!dbus_message_iter_append_string(iter, val)) {
+          croak("cannot append string");
+        }
+
+
+void
+append_boolean_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        char *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, char);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          char *str = el ? SvPVbyte_nolen(*el) : NULL;
+          vals[i] = str && str[0] ? 0x1: 0x0;
+        }
+        res = dbus_message_iter_append_boolean_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_byte_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        char *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, char);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          char *str = el ? SvPVbyte_nolen(*el) : NULL;
+          vals[i] = str ? str[0] : '0';
+        }
+        res = dbus_message_iter_append_byte_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_string_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        const char **vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, const char*);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          vals[i] = el ? SvPV_nolen(*el) : NULL;
+        }
+        res = dbus_message_iter_append_string_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_int32_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        dbus_int32_t *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, dbus_int32_t);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          vals[i] = el ? SvIV(*el) : 0;
+        }
+        res = dbus_message_iter_append_int32_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_uint32_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        dbus_uint32_t *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, dbus_uint32_t);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          vals[i] = el ? SvIV(*el) : 0;
+        }
+        res = dbus_message_iter_append_uint32_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_int64_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        dbus_int64_t *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, dbus_int64_t);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          vals[i] = el ? SvIV(*el) : 0;
+        }
+        res = dbus_message_iter_append_int64_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_uint64_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        dbus_uint64_t *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, dbus_uint64_t);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          vals[i] = el ? SvIV(*el) : 0;
+        }
+        res = dbus_message_iter_append_uint64_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_double_array(iter, ref)
+        DBusMessageIter *iter;
+        SV *ref;
+    PREINIT:
+        AV *val;
+        I32 len;
+        I32 i;
+        double *vals;
+        dbus_bool_t res;
+    CODE:
+        val = (AV*)SvRV(ref);
+        len = val ? av_len(val)+1 : 0;
+        New(1, vals, len, double);
+        for (i = 0 ; i < len ; i++) {
+          SV **el = av_fetch(val,i,0);
+          vals[i] = el ? SvNV(*el) : 0.0;
+        }
+        res = dbus_message_iter_append_double_array(iter, vals, len);
+        Safefree(vals);
+        if (!res) {
+          croak("Cannot append array");
+        }
+
+void
+append_dict_key(iter, val)
+        DBusMessageIter *iter;
+        char *val;
+    CODE:
+        if (!dbus_message_iter_append_dict_key(iter, val)) {
+          croak("Cannot append dict key");
+        }
+
+DBusMessageIter *
+append_dict(iter)
+        DBusMessageIter *iter;
+    PREINIT:
+        DBusMessageIter *diter;
+    CODE:
+        diter = dbus_new(DBusMessageIter, 1);
+
+        if (!dbus_message_iter_append_dict(iter, diter)) {
+          dbus_free(diter);
+          croak("Cannot append dict");
+        }
+        RETVAL = iter;
+    OUTPUT:
+        RETVAL
 
 void
 DESTROY(iter)
diff --git a/lib/Net/DBus.pm b/lib/Net/DBus.pm
index ffcd50b..a14a357 100644
--- a/lib/Net/DBus.pm
+++ b/lib/Net/DBus.pm
@@ -6,10 +6,21 @@ use warnings;
 use Carp;
 
 use Net::DBus::Binding::Bus;
+use Net::DBus::Binding::Message;
+use Net::DBus::Binding::Value;
 use Net::DBus::RemoteService;
 
 our $VERSION = '0.0.1';
 
+use Exporter;
+
+use base qw(Exporter);
+
+use vars qw(@EXPORT);
+
+ at EXPORT = qw(dboolean dbyte dstring dint32
+             duint32 dint64 duint64 ddouble);
+
 require XSLoader;
 XSLoader::load('Net::DBus', $VERSION);
 
@@ -112,6 +123,58 @@ sub _signal_func {
     return $handled;
 }
 
+
+sub dboolean {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_BOOLEAN,
+					  $value);
+}
+
+sub dbyte {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_BYTE,
+					  $value);
+}
+
+sub dstring {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_STRING,
+					  $value);
+}
+
+sub dint32 {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_INT32,
+					  $value);
+}
+
+sub duint32 {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32,
+					  $value);
+}
+
+sub dint64 {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_INT64,
+					  $value);
+}
+
+sub duint64 {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT64,
+					  $value);
+}
+
+sub ddouble {
+    my $value = shift;
+    return Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_DOUBLE,
+					  $value);
+}
+
+
+
+
 1;
 __END__
 
diff --git a/lib/Net/DBus/Binding/Iterator.pm b/lib/Net/DBus/Binding/Iterator.pm
index e185960..fe8ff16 100644
--- a/lib/Net/DBus/Binding/Iterator.pm
+++ b/lib/Net/DBus/Binding/Iterator.pm
@@ -172,6 +172,10 @@ sub get {
     
     if ($type == &Net::DBus::Binding::Message::TYPE_STRING) {
 	return $self->get_string;
+    } elsif ($type == &Net::DBus::Binding::Message::TYPE_BOOLEAN) {
+	return $self->get_boolean;
+    } elsif ($type == &Net::DBus::Binding::Message::TYPE_BYTE) {
+	return $self->get_byte;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_INT32) {
 	return $self->get_int32;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_UINT32) {
@@ -180,27 +184,28 @@ sub get {
 	return $self->get_int64;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_UINT64) {
 	return $self->get_uint64;
+    } elsif ($type == &Net::DBus::Binding::Message::TYPE_DOUBLE) {
+	return $self->get_double;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_ARRAY) {
 	my $iter = $self->get_array_iter();
 	my @value;
 	do {
 	    push @value, $iter->get();
 	} while ($iter->next());
-	return @value;
-    } elsif ($type == &Net::DBus::Binding::Message::TYPE_BOOLEAN) {
-	return $self->get_boolean;
-    } elsif ($type == &Net::DBus::Binding::Message::TYPE_BYTE) {
-	return $self->get_byte;
+	return \@value;
+    } elsif ($type == &Net::DBus::Binding::Message::TYPE_DICT) {
+	my $iter = $self->get_dict_iter();
+	my %value;
+	do {
+	    $value{$iter->get_dict_key} = $iter->get;
+	} while ($iter->next());
+	return \%value;
+    } elsif ($type == &Net::DBus::Binding::Message::TYPE_NIL) {
+	return undef;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_CUSTOM) {
 	confess "cannot handle Net::DBus::Binding::Message::TYPE_CUSTOM";
-    } elsif ($type == &Net::DBus::Binding::Message::TYPE_DICT) {
-	confess "cannot handle Net::DBus::Binding::Message::TYPE_DICT";
-    } elsif ($type == &Net::DBus::Binding::Message::TYPE_DOUBLE) {
-	return $self->get_double;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_INVALID) {
 	confess "cannot handle Net::DBus::Binding::Message::TYPE_INVALID";
-    } elsif ($type == &Net::DBus::Binding::Message::TYPE_NIL) {
-	return undef;
     } elsif ($type == &Net::DBus::Binding::Message::TYPE_OBJECT_PATH) {
 	confess "cannot handle Net::DBus::Binding::Message::TYPE_OBJECT_PATH";
     }
@@ -211,19 +216,71 @@ sub append {
     my $value = shift;
     
     if (ref($value)) {
-	if (ref($value) eq "ARRAY") {
+	if (ref($value) eq "Net::DBus::Binding::Value") {
+	    my $raw = $value->value;
+
+	    if ($value->type == &Net::DBus::Binding::Message::TYPE_BOOLEAN) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_boolean_array($raw);
+		} else {
+		    $self->append_boolean($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_BYTE) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_byte_array($raw);
+		} else {
+		    $self->append_byte($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_STRING) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_string_array($raw);
+		} else {
+		    $self->append_string($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_INT32) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_int32_array($raw);
+		} else {
+		    $self->append_int32($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_UINT32) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_uint32_array($raw);
+		} else {
+		    $self->append_uint32($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_INT64) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_int64_array($raw);
+		} else {
+		    $self->append_int64($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_UINT64) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_uint64_array($raw);
+		} else {
+		    $self->append_uint64($raw);
+		}
+	    } elsif ($value->type == &Net::DBus::Binding::Message::TYPE_DOUBLE) {
+		if (ref($raw) && ref($raw) eq "ARRAY") {
+		    $self->append_double_array($raw);
+		} else {
+		    $self->append_double($raw);
+		}
+	    }
 	} elsif (ref($value) eq "HASH") {
+	    my $iter = $self->append_dict();
+	    foreach my $key (keys %{$value}) {
+		$iter->append_dict_key($key);
+		$iter->append($value->{$key});
+	    }
+	} elsif (ref($value) eq "ARRAY") {
+	    $self->append_string_array($value);
 	} else {
-	    confess "Unknown reference type ", ref($value);
+	    confess "Unsupported reference type ", ref($value);
 	}
     } else {
-	if ($value =~ /^[+-]?\d+$/) {
-	    $self->append_int32($value);
-	} elsif ($value =~ /^([+-]?)(?=\d���\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) {
-	    $self->append_double($value);
-	} else {
-	    $self->append_string($value);
-	}
+	$self->append_string($value);
     }
 }
 
diff --git a/lib/Net/DBus/Binding/Value.pm b/lib/Net/DBus/Binding/Value.pm
new file mode 100644
index 0000000..e585309
--- /dev/null
+++ b/lib/Net/DBus/Binding/Value.pm
@@ -0,0 +1,78 @@
+=pod
+
+=head1 NAME
+
+Net::DBus::Binding::Value - A strongly typed value
+
+=head1 SYNOPSIS
+
+
+=head1 DESCRIPTION
+
+
+
+=head1 METHODS
+
+=over 4
+
+=cut
+
+package Net::DBus::Binding::Value;
+
+
+use 5.006;
+use strict;
+use warnings;
+use Carp qw(confess);
+
+use Net::DBus;
+
+our $VERSION = '0.0.1';
+
+
+sub new {
+    my $proto = shift;
+    my $class = ref($proto) || $proto;
+    my $self = [];
+    
+    $self->[0] = shift;
+    $self->[1] = shift;
+    
+    bless $self, $class;
+
+    return $self;
+}
+
+
+sub type {
+    my $self = shift;
+    return $self->[0];
+}
+
+sub value {
+    my $self = shift;
+    return $self->[1];
+}
+
+1;
+
+=pod
+
+=back
+
+=head1 SEE ALSO
+
+L<Net::DBus::Binding::Message>
+
+=head1 AUTHOR
+
+Daniel Berrange E<lt>dan at berrange.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2004 by Daniel Berrange
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself. 
+
+=cut

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libnet-dbus-perl.git



More information about the Pkg-perl-cvs-commits mailing list