[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Implement serialization for Locations. [3/4]
From: |
Ralf Wildenhues |
Subject: |
Implement serialization for Locations. [3/4] |
Date: |
Sun, 26 Oct 2008 20:39:58 +0100 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
This serialization business is probably cleaner done with something like
Data::Dumper. Oh well, if anyone wants to clean it up after me, be my
guest.
Cheers,
Ralf
Implement serialization for Locations.
* lib/Automake/Location.pm (serialize, deserialize): New
functions. They allows to serialize a Location in an array, and
to restore a Location from a thread queue. The API is
unsymmetric (array vs. queue) because enqueuing data needs to
happen atomically.
diff --git a/lib/Automake/Location.pm b/lib/Automake/Location.pm
index 33f526a..90534f1 100644
--- a/lib/Automake/Location.pm
+++ b/lib/Automake/Location.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+# Copyright (C) 2002, 2003, 2008 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -57,6 +57,13 @@ Automake::Location - a class for location tracking, with a
stack of contexts
# that would otherwise be modified.
my $where_copy = $where->clone;
+ # Serialize a Location object (for passing through a thread queue,
+ # for example)
+ my @array = $where->serialize ();
+
+ # De-serialize: recreate a Location object from a queue.
+ my $where = new Automake::Location::deserialize ($queue);
+
=head1 DESCRIPTION
C<Location> objects are used to keep track of locations in Automake,
@@ -145,6 +152,33 @@ sub dump ($)
return $res;
}
+sub serialize ($)
+{
+ my ($self) = @_;
+ my @serial = ();
+ push @serial, $self->get;
+ my @contexts = $self->get_contexts;
+ for my $pair (@contexts)
+ {
+ push @serial, @{$pair};
+ }
+ push @serial, undef;
+ return @serial;
+}
+
+sub deserialize ($)
+{
+ my ($queue) = @_;
+ my $position = $queue->dequeue ();
+ my $self = new Automake::Location $position;
+ while (my $position = $queue->dequeue ())
+ {
+ my $context = $queue->dequeue ();
+ push @{$self->{'contexts'}}, [$position, $context];
+ }
+ return $self;
+}
+
=head1 SEE ALSO
L<Automake::Channels>