1b39c5158Smillertpackage TAP::Base;
2b39c5158Smillert
3b39c5158Smillertuse strict;
46fb12b70Safresh1use warnings;
5b39c5158Smillert
66fb12b70Safresh1use base 'TAP::Object';
7b39c5158Smillert
8b39c5158Smillert=head1 NAME
9b39c5158Smillert
10b39c5158SmillertTAP::Base - Base class that provides common functionality to L<TAP::Parser>
11b39c5158Smillertand L<TAP::Harness>
12b39c5158Smillert
13b39c5158Smillert=head1 VERSION
14b39c5158Smillert
15*3d61058aSafresh1Version 3.48
16b39c5158Smillert
17b39c5158Smillert=cut
18b39c5158Smillert
19*3d61058aSafresh1our $VERSION = '3.48';
20b39c5158Smillert
21b39c5158Smillertuse constant GOT_TIME_HIRES => do {
22b39c5158Smillert    eval 'use Time::HiRes qw(time);';
23b39c5158Smillert    $@ ? 0 : 1;
24b39c5158Smillert};
25b39c5158Smillert
26b39c5158Smillert=head1 SYNOPSIS
27b39c5158Smillert
28b39c5158Smillert    package TAP::Whatever;
29b39c5158Smillert
306fb12b70Safresh1    use base 'TAP::Base';
31b39c5158Smillert
32b39c5158Smillert    # ... later ...
33b39c5158Smillert
34b39c5158Smillert    my $thing = TAP::Whatever->new();
35b39c5158Smillert
36b39c5158Smillert    $thing->callback( event => sub {
37b39c5158Smillert        # do something interesting
38b39c5158Smillert    } );
39b39c5158Smillert
40b39c5158Smillert=head1 DESCRIPTION
41b39c5158Smillert
42b39c5158SmillertC<TAP::Base> provides callback management.
43b39c5158Smillert
44b39c5158Smillert=head1 METHODS
45b39c5158Smillert
46b39c5158Smillert=head2 Class Methods
47b39c5158Smillert
48b39c5158Smillert=cut
49b39c5158Smillert
50b39c5158Smillertsub _initialize {
51b39c5158Smillert    my ( $self, $arg_for, $ok_callback ) = @_;
52b39c5158Smillert
53b39c5158Smillert    my %ok_map = map { $_ => 1 } @$ok_callback;
54b39c5158Smillert
55b39c5158Smillert    $self->{ok_callbacks} = \%ok_map;
56b39c5158Smillert
57b39c5158Smillert    if ( my $cb = delete $arg_for->{callbacks} ) {
58b39c5158Smillert        while ( my ( $event, $callback ) = each %$cb ) {
59b39c5158Smillert            $self->callback( $event, $callback );
60b39c5158Smillert        }
61b39c5158Smillert    }
62b39c5158Smillert
63b39c5158Smillert    return $self;
64b39c5158Smillert}
65b39c5158Smillert
66b39c5158Smillert=head3 C<callback>
67b39c5158Smillert
68b39c5158SmillertInstall a callback for a named event.
69b39c5158Smillert
70b39c5158Smillert=cut
71b39c5158Smillert
72b39c5158Smillertsub callback {
73b39c5158Smillert    my ( $self, $event, $callback ) = @_;
74b39c5158Smillert
75b39c5158Smillert    my %ok_map = %{ $self->{ok_callbacks} };
76b39c5158Smillert
77b39c5158Smillert    $self->_croak('No callbacks may be installed')
78b39c5158Smillert      unless %ok_map;
79b39c5158Smillert
80b39c5158Smillert    $self->_croak( "Callback $event is not supported. Valid callbacks are "
81b39c5158Smillert          . join( ', ', sort keys %ok_map ) )
82b39c5158Smillert      unless exists $ok_map{$event};
83b39c5158Smillert
84b39c5158Smillert    push @{ $self->{code_for}{$event} }, $callback;
85b39c5158Smillert
86b39c5158Smillert    return;
87b39c5158Smillert}
88b39c5158Smillert
89b39c5158Smillertsub _has_callbacks {
90b39c5158Smillert    my $self = shift;
91b39c5158Smillert    return keys %{ $self->{code_for} } != 0;
92b39c5158Smillert}
93b39c5158Smillert
94b39c5158Smillertsub _callback_for {
95b39c5158Smillert    my ( $self, $event ) = @_;
96b39c5158Smillert    return $self->{code_for}{$event};
97b39c5158Smillert}
98b39c5158Smillert
99b39c5158Smillertsub _make_callback {
100b39c5158Smillert    my $self  = shift;
101b39c5158Smillert    my $event = shift;
102b39c5158Smillert
103b39c5158Smillert    my $cb = $self->_callback_for($event);
104b39c5158Smillert    return unless defined $cb;
105b39c5158Smillert    return map { $_->(@_) } @$cb;
106b39c5158Smillert}
107b39c5158Smillert
108b39c5158Smillert=head3 C<get_time>
109b39c5158Smillert
110b39c5158SmillertReturn the current time using Time::HiRes if available.
111b39c5158Smillert
112b39c5158Smillert=cut
113b39c5158Smillert
114b39c5158Smillertsub get_time { return time() }
115b39c5158Smillert
116b39c5158Smillert=head3 C<time_is_hires>
117b39c5158Smillert
118b39c5158SmillertReturn true if the time returned by get_time is high resolution (i.e. if Time::HiRes is available).
119b39c5158Smillert
120b39c5158Smillert=cut
121b39c5158Smillert
122b39c5158Smillertsub time_is_hires { return GOT_TIME_HIRES }
123b39c5158Smillert
124b8851fccSafresh1=head3 C<get_times>
125b8851fccSafresh1
126b8851fccSafresh1Return array reference of the four-element list of CPU seconds,
127b8851fccSafresh1as with L<perlfunc/times>.
128b8851fccSafresh1
129b8851fccSafresh1=cut
130b8851fccSafresh1
131b8851fccSafresh1sub get_times { return [ times() ] }
132b8851fccSafresh1
133b39c5158Smillert1;
134