| 1 | # O b j e c t : : I n t e r f a c e | ||
| 2 | # | ||
| 3 | # Perl Module List Entry: | ||
| 4 | # | ||
| 5 | # Name DSLI Description Info | ||
| 6 | # ------------- ---- -------------------------------------------- ----- | ||
| 7 | # Object:: | ||
| 8 | # ::Interface adpn Specificy pure virtual base classes ala C++ GWELCH | ||
| 9 | # | ||
| 10 | # | ||
| 11 | # Author: Gerad Welch <gwelch@computer.org> | ||
| 12 | # <welch.119@osu.edu> for development issues | ||
| 13 | # | ||
| 14 | # Contributors: | ||
| 15 | # [STH] Shay Harding <sharding@geocities.com> | ||
| 16 | # | ||
| 17 | # Copyright: | ||
| 18 | # Copyright (c) 1999-2000 Gerad Welch. All rights reserved. | ||
| 19 | # This program may be freely redistributed, though all useful | ||
| 20 | # modifications must be sent back to the author for examination and | ||
| 21 | # and possible incorporation into future versions. =) | ||
| 22 | # | ||
| 23 | # Revision Log: | ||
| 24 | # | ||
| 25 | # 2000.07.16 GMW A version that really works.... Unfortunately, this | ||
| 26 | # only runs under versions of Perl which support CHECK | ||
| 27 | # and the warning pragma (I think only 5.6 or better). | ||
| 28 | # 2000.05.11 STH Added output of all missing methods at program | ||
| 29 | # termination, as well as some other miscellaneous useful | ||
| 30 | # stuff. | ||
| 31 | # 1999.09.18 GMW Removed 'require' interface for simplicity. Cleaned up | ||
| 32 | # things a /little/ bit.... | ||
| 33 | # 1999.09.02 GMW Finished preliminary version. | ||
| 34 | # | ||
| 35 | # Note: | ||
| 36 | # Someday it might be nice to have an 'object' pragma, so one could say | ||
| 37 | # 'use object interface qw( required symbols )' instead of the current | ||
| 38 | # usage. Maybe someday.... | ||
| 39 | |||
| 40 | package Object::Interface; | ||
| 41 | |||
| 42 | use strict; | ||
| 43 | |||
| 44 | use vars qw( $VERSION $DEBUG $list ); | ||
| 45 | |||
| 46 | $VERSION = '1.1'; | ||
| 47 | $DEBUG = 0; | ||
| 48 | |||
| 49 | BEGIN | ||
| 50 | { | ||
| 51 | |||
| 52 | $list = []; | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | sub import | ||
| 57 | { | ||
| 58 | |||
| 59 | # Get rid of our package name | ||
| 60 | shift; | ||
| 61 | |||
| 62 | my @required = @_; | ||
| 63 | |||
| 64 | # Find out what module called us (the last require'd or use'd module) | ||
| 65 | my @caller; | ||
| 66 | my $i = 0; | ||
| 67 | |||
| 68 | while ( 1 ) { | ||
| 69 | @caller = caller $i++; | ||
| 70 | die "Unable to find derived class!\n" if ! @caller; | ||
| 71 | # This must be compared explicitly to 1, as sometimes caller() returns | ||
| 72 | # non-null values that aren't 1. Think that's a bug. Seems that this | ||
| 73 | # changed between Perl 5.00x and 5.6. | ||
| 74 | if ( defined $caller[7] ) { | ||
| 75 | last if "$caller[7]" eq "1"; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | print STDERR <<"EOS" if $DEBUG; | ||
| 80 | |||
| 81 | use Object::Interface called from $caller[0] | ||
| 82 | ( @caller ) | ||
| 83 | Interface methods: @required | ||
| 84 | |||
| 85 | EOS | ||
| 86 | |||
| 87 | # Save the frame information for later checking | ||
| 88 | push @$list, [ \@caller, \@required ]; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | CHECK | ||
| 93 | { | ||
| 94 | |||
| 95 | my @caller; | ||
| 96 | my @required; | ||
| 97 | |||
| 98 | my @syms; | ||
| 99 | |||
| 100 | my @present; | ||
| 101 | my $is_sub; | ||
| 102 | |||
| 103 | my ( $sym, $flag ); | ||
| 104 | my @errors; | ||
| 105 | |||
| 106 | my $error = 0; | ||
| 107 | |||
| 108 | foreach my $frame ( @$list ) { | ||
| 109 | |||
| 110 | @caller = @{$frame->[0]}; | ||
| 111 | @required = @{$frame->[1]}; | ||
| 112 | |||
| 113 | # Extract all sub names from the calling module | ||
| 114 | eval '@syms = keys %' . $caller[0] . '::'; | ||
| 115 | |||
| 116 | do { | ||
| 117 | map { print STDERR "$_ " } @syms; | ||
| 118 | print "\n"; | ||
| 119 | } if $DEBUG; | ||
| 120 | |||
| 121 | # Check to see if the symbol's defined in the package's symbol table | ||
| 122 | |||
| 123 | no warnings; | ||
| 124 | |||
| 125 | @present = map { | ||
| 126 | eval "\$is_sub = defined \$${caller[0]}::{$_}"; | ||
| 127 | $is_sub ? ( $_ ) : ( ); | ||
| 128 | } @syms; | ||
| 129 | |||
| 130 | use warnings; | ||
| 131 | |||
| 132 | print STDERR "Derived class's methods: @present\n\n" if $DEBUG; | ||
| 133 | |||
| 134 | # Check to see what's there and what's not | ||
| 135 | |||
| 136 | @errors = (); | ||
| 137 | foreach my $sym ( @required ) { | ||
| 138 | @present = map { ( $sym eq $_ ) ? ( $flag = $sym, () )[1] : ( $_ ) } @present; | ||
| 139 | push @errors, $sym if ! defined $flag; | ||
| 140 | undef $flag; | ||
| 141 | } | ||
| 142 | |||
| 143 | if ( @errors ) { | ||
| 144 | print STDERR sprintf "Pure virtual function%s < " . join( ', ', @errors ) . " > not defined in $caller[1].\n", | ||
| 145 | ( @errors == 1 ? '' : 's' ); | ||
| 146 | $error = 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | die "Execution aborted.\n" if $error; | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | # Codesong: Plastic Tree, "Trance Orange" | ||
| 156 | # Yo to Yohei, Tomi, and Yasu -- party animals of my own nature. | ||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 | __END__ |