| 1 | package Lab::Bus; | ||
| 2 | $Lab::Bus::VERSION = '3.899'; | ||
| 3 | #ABSTRACT: Bus base class | ||
| 4 | |||
| 5 | use v5.20; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | |||
| 9 | use Lab::Generic; | ||
| 10 | use Lab::Exception; | ||
| 11 | use Time::HiRes qw (usleep sleep); | ||
| 12 | |||
| 13 | #use POSIX; # added for int() function | ||
| 14 | use Scalar::Util qw(weaken); | ||
| 15 | use Carp qw(croak cluck); | ||
| 16 | use Data::Dumper; | ||
| 17 | our $AUTOLOAD; | ||
| 18 | |||
| 19 | our @ISA = ('Lab::Generic'); | ||
| 20 | |||
| 21 | # this holds a list of references to all the bus objects that are floating around in memory, | ||
| 22 | # to enable transparent bus reuse, so the user doesn't have to handle (or even know about, | ||
| 23 | # to that end) bus objects. weaken() is used so the reference in this list does not prevent destruction | ||
| 24 | # of the object when the last "real" reference is gone. | ||
| 25 | our %BusList = ( | ||
| 26 | |||
| 27 | # BusType => $BusReference, | ||
| 28 | ); | ||
| 29 | |||
| 30 | our %fields = ( | ||
| 31 | config => undef, | ||
| 32 | type => undef, # e.g. 'GPIB' | ||
| 33 | ignore_twins => 0, # | ||
| 34 | ins_debug => 0, # do we need additional output? | ||
| 35 | ); | ||
| 36 | |||
| 37 | sub new { | ||
| 38 | my $proto = shift; | ||
| 39 | my $class = ref($proto) || $proto; | ||
| 40 | my $config = undef; | ||
| 41 | if ( ref $_[0] eq 'HASH' ) { | ||
| 42 | $config = shift; | ||
| 43 | } # try to be flexible about options as hash/hashref | ||
| 44 | else { $config = {@_} } | ||
| 45 | my $self = $class->SUPER::new(@_); | ||
| 46 | bless( $self, $class ); | ||
| 47 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 48 | |||
| 49 | $self->config($config); | ||
| 50 | |||
| 51 | # Object data setup | ||
| 52 | $self->ignore_twins( $self->config('ignore_twins') ); | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | # | ||
| 58 | # Call this in inheriting class's constructors to conveniently initialize the %fields object data | ||
| 59 | # | ||
| 60 | sub _construct { # _construct(__PACKAGE__); | ||
| 61 | ( my $self, my $package ) = ( shift, shift ); | ||
| 62 | my $class = ref($self); | ||
| 63 | my $fields = undef; | ||
| 64 | { | ||
| 65 | no strict 'refs'; | ||
| 66 | $fields = *${ \( $package . '::fields' ) }{HASH}; | ||
| 67 | } | ||
| 68 | my $twin = undef; | ||
| 69 | |||
| 70 | foreach my $element ( keys %{$fields} ) { | ||
| 71 | $self->{_permitted}->{$element} = $fields->{$element}; | ||
| 72 | } | ||
| 73 | @{$self}{ keys %{$fields} } = values %{$fields}; | ||
| 74 | } | ||
| 75 | |||
| 76 | # | ||
| 77 | # these are stubs to be overwritten in child classes | ||
| 78 | # | ||
| 79 | |||
| 80 | # | ||
| 81 | # In child classes, this should search %Lab::Bus::BusList for a reusable | ||
| 82 | # instance (and be called in the constructor). | ||
| 83 | # e.g. | ||
| 84 | # return $self->_search_twin() || $self; | ||
| 85 | # | ||
| 86 | sub _search_twin { | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub connection_read { # @_ = ( $connection_handle, \%args ) | ||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub connection_write { # @_ = ( $connection_handle, \%args ) | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | # | ||
| 99 | # generates and returns a connection handle; | ||
| 100 | # | ||
| 101 | sub connection_new { | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | # | ||
| 106 | # config gets it's own accessor - convenient access like $self->config('GPIB_Paddress') instead of $self->config()->{'GPIB_Paddress'} | ||
| 107 | # with a hashref as argument, set $self->{'config'} to the given hashref. | ||
| 108 | # without an argument it returns a reference to $self->config (just like AUTOLOAD would) | ||
| 109 | # | ||
| 110 | sub config { # $value = self->config($key); | ||
| 111 | ( my $self, my $key ) = ( shift, shift ); | ||
| 112 | |||
| 113 | if ( !defined $key ) { | ||
| 114 | return $self->{'config'}; | ||
| 115 | } | ||
| 116 | elsif ( ref($key) =~ /HASH/ ) { | ||
| 117 | return $self->{'config'} = $key; | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | return $self->{'config'}->{$key}; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | sub AUTOLOAD { | ||
| 125 | |||
| 126 | my $self = shift; | ||
| 127 | my $type = ref($self) or croak "$self is not an object"; | ||
| 128 | |||
| 129 | my $name = $AUTOLOAD; | ||
| 130 | $name =~ s/.*://; # strip fully qualified portion | ||
| 131 | |||
| 132 | unless ( exists $self->{_permitted}->{$name} ) { | ||
| 133 | cluck( "AUTOLOAD in " | ||
| 134 | . __PACKAGE__ | ||
| 135 | . " couldn't access field '${name}'.\n" ); | ||
| 136 | Lab::Exception::Error->throw( error => "AUTOLOAD in " | ||
| 137 | . __PACKAGE__ | ||
| 138 | . " couldn't access field '${name}'.\n" ); | ||
| 139 | } | ||
| 140 | |||
| 141 | if (@_) { | ||
| 142 | return $self->{$name} = shift; | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | return $self->{$name}; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | # needed so AUTOLOAD doesn't try to call DESTROY on cleanup and prevent the inherited DESTROY | ||
| 150 | sub DESTROY { | ||
| 151 | my $self = shift; | ||
| 152 | $self->SUPER::DESTROY if $self->can("SUPER::DESTROY"); | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; | ||
| 156 | |||
| 157 | __END__ |