package VCP::Branches ; =head1 NAME VCP::Branches - A collection of VCP::Rev objects. =head1 SYNOPSIS =head1 DESCRIPTION Right now, all branches are kept in memory, but we will enable storing them to disk and recovering them at some point so that we don't gobble huge tracts of RAM. =head1 METHODS =over =cut $VERSION = 1 ; use strict ; use Carp ; use VCP::Debug ":debug" ; use VCP::Rev ; use fields ( 'BRANCHES', ## The branches, sorted or not 'SEEN', ## Oh, the branches we've seen ) ; =item new =cut sub new { my $class = CORE::shift ; $class = ref $class || $class ; my $self ; { no strict 'refs' ; $self = bless [ \%{"$class\::FIELDS"} ], $class ; } $self->{BRANCHES} = [] ; $self->{SEEN} = {} ; return $self ; } =item add $branches->add( $branch ) ; $branches->add( $branch1, $branch2, ... ) ; Adds a branch or branches to the collection, unless they are already present. =cut sub add { my VCP::Branches $self = CORE::shift ; for my $b ( @_ ) { my $key = $b->branch_id; next if $self->{SEEN}->{$key} ; debug( "vcp: queuing ", $b->as_string ) if debugging $self || debugging scalar caller; $self->{SEEN}->{$key} = 1 ; push @{$self->{BRANCHES}}, $b ; } } =item set $branches->set( $branch ) ; $branches->set( $branch1, $branch2, ... ) ; Sets the list of branches. =cut sub set { my VCP::Branches $self = CORE::shift ; Carp::confess "undef passed" if grep !defined, @_; if ( debugging $self || debugging scalar caller ) { require UNIVERSAL; Carp::confess "unblessed ref passed" if grep !UNIVERSAL::can( $_, "as_string" ), @_; debug( "vcp: queuing ", $_->as_string ) for @_ ; } $self->add( $_ ) for @_; } #=item seen # # $branches->seen( $branch ); # #=cut # #sub seen { # my $self = shift; # my ( $b ) = @_; # # return $self->{SEEN}->{$b->branch_id}; #} # # =item get @branches = $branches->get ; Gets the list of branches. =cut sub get { my VCP::Branches $self = CORE::shift ; return @{$self->{BRANCHES}} ; } =item sort # Using a custom sort function: $branches->sort( sub { ... } ) ; Note: Don't use $a and $b in your sort function. They're package globals and that's not your package. See L for more details. =cut sub sort { my VCP::Branches $self = CORE::shift ; my ( $sort_func ) = @_ ; @{$self->{BRANCHES}} = sort $sort_func, @{$self->{BRANCHES}} ; } =item shift while ( $b = $branches->shift ) { ... } Call L before calling this :-). =cut sub shift { my VCP::Branches $self = CORE::shift ; return shift @{$self->{BRANCHES}} ; } =item as_array_ref Returns an ARRAY ref of all branches. =cut sub as_array_ref { my VCP::Branches $self = CORE::shift ; return $self->{BRANCHES} ; } =head1 SUBCLASSING This class uses the fields pragma, so you'll need to use base and possibly fields in any subclasses. =head1 COPYRIGHT Copyright 2000, Perforce Software, Inc. All Rights Reserved. This module and the VCP package are licensed according to the terms given in the file LICENSE accompanying this distribution, a copy of which is included in L. =head1 AUTHOR Barrie Slaymaker =cut 1