File: State.pm

package info (click to toggle)
libpgplot-perl 1%3A2.35-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,336 kB
  • sloc: perl: 3,880; ansic: 453; makefile: 5
file content (140 lines) | stat: -rw-r--r-- 3,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
=head1 NAME

State - A package to keep track of plotting commands

=head1 SYNOPSIS

  use PDL::Graphics::State;

=head1 DESCRIPTION

This is a very simple, at present almost trivial, package to keep track
of the current set of plotting commands.

=head1 USAGE

You create a new object by calling the C<new> operator

  $state = PDL::Graphics::State->new();

Then for each new command you call C<add> on this object so that for a
call to C<line> of the form

  line $x, $y, $opt;

the call to C<add> would be like

  $state->add(\&line, 'line', [$x, $y], $opt);

which is stored internally as:

  [\&line, 'line', [$x, $y], $opt]

The state can later be extracted using C<get> which returns the state
object which is an array of anonymous arrays like the one above where
the first object is a reference to the function, the second an anomymous
array of arguments to the function and finally an anonymous hash with
options to the command.

If you know the order in which you inserted commands they can be removed
by calling C<remove> with the number in the stack. No further interaction
is implemented except C<clear> which clears the stack and C<copy> which
returns a "deep" copy of the state.

=head1 AUTHOR

Jarle Brinchmann (jarle@astro.ox.ac.uk) after some prodding by
Karl Glazebrook.

All rights reserved. There is no warranty. You are allowed
to redistribute this software / documentation under certain
conditions. For details, see the file COPYING in the PDL
distribution. If this file is separated from the PDL distribution,
the copyright notice should be included in the file.

=cut

package PDL::Graphics::State;
use strict;
use warnings;

#
# This is a very simple package to deal with the graphics state.
#

sub new {
  my $type = shift;

  my $self = {
	      'Commands' => [],
	     };

  bless $self, ref($type) || $type;
  return $self;
}


sub DESTROY {
  my $self = shift;
  $self->clear();
}

sub add {
  my $self = shift;
  # The command is a reference to the subroutine, the data is an
  # anonymous array containing the data passed to the routine and
  # opt is the options hash PASSED TO THE ROUTINE..
  my ($command, $command_name, $data, $opt) = @_;

  # Compact and not user-friendly storage.
  push @{$self->{Commands}}, [$command, $command_name, $data, $opt];

#  return $#{$self->{Commands}}+1;
}


sub remove {
  my $self = shift;
  my $num = shift;

  # Remove entry #1
  splice @{$self->{Commands}}, $num, 1;
}

sub get {
  my $self = shift;
  return @{$self->{Commands}};
}

sub info {
  my $self = shift;
  print "The state has ".($#{$self->{Commands}}+1)." commands in the stack\n";
}

sub show {
  my $self = shift;
  my $count=0;
  foreach my $arg (@{$self->{Commands}}) {
    print "$count - Func=$$arg[1]\n";
    $count++;
  }

}

sub clear {
  my $self = shift;
  # Do I need to do more?
  $self->{Commands}=[];
}


sub copy {
  my $self = shift;
  my $new = PDL::Graphics::State->new();
  foreach my $arg (@{$self->{Commands}}) {
    $new->add(@$arg);
  }
  return $new;
}

1;