Implement serialization for Locations.
authorRalf Wildenhues <Ralf.Wildenhues@gmx.de>
Sun, 26 Oct 2008 19:38:06 +0000 (20:38 +0100)
committerRalf Wildenhues <Ralf.Wildenhues@gmx.de>
Sun, 26 Oct 2008 19:38:06 +0000 (20:38 +0100)
* 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.

Signed-off-by: Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
ChangeLog
lib/Automake/Location.pm

index de78a03..10b0b5f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2008-10-26  Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>
 
+       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.
+
        Parallel automake: ordered output messages.
        * lib/Automake/Channels.pm (%_default_options): New options
        `ordered' default enabled, `queue', default zero (no queue),
index 33f526a..90534f1 100644 (file)
@@ -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>