Thread::Queue::Queueable

# Thread::Queue::Duplex

Description | Thread-safe request/response queue with identifiable elements

Thread::Queue::Duplex > Package Manuals > Thread-Queue-Queueable
Classdocs

### Index

- NAME
- SYNOPSIS
- DESCRIPTION
- METHODS
- SEE ALSO
- AUTHOR, COPYRIGHT, & LICENSE

# NAME

Thread::Queue::Queueable - abstract class for marshalling elements for a Thread::Queue::Duplex queue

# SYNOPSIS

    	use Thread::Queue::Queueable;
    	use base qw(Thread::Queue::Queueable);
    	#
    	#	implement onEnqueue method
    	#	(default implementation shown)
    	#
    	sub onEnqueue {
    		my $obj = shift;
    	#
    	#	capture class name, and create shared
    	#	version of object
    	#
    		return $obj->isa('ARRAY') ?
    			(ref $obj, share([ @$obj ])) :
    			(ref $obj, share({ %$obj }));
    	}
    	#
    	#	implement onDequeue method
    	#	(default implementation shown)
    	#
    	sub onDequeue {
    		my ($class, $obj) = @_;
    	#
    	#	reconstruct as non-shared
    	#
    		$obj = (ref $obj eq 'ARRAY') ? [ @$obj ] : { %$obj };
    		bless $obj, $class;
    		return $obj;
    	}
    	#
    	#	permit the object to be reconstructed on dequeueing
    	#
    	sub onCancel {
    		my $obj = shift;
    		return 1;
    	}
    	#
    	#	curse (ie, unbless) the object into a shared structure
    	#
    	sub curse {
    		my $obj = shift;

    		if ($obj->isa('HASH')) {
    			my %cursed : shared = ();
    			$cursed{$_} = $obj->{$_}
    				foreach (keys %$obj);
    			return \%cursed;
    		}

    		my @cursed : shared = ();
    		$cursed[$_] = $obj->[$_]
    			foreach (0..$#$obj);
    		return \@cursed;
    	}
    	#
    	#	redeem (ie, rebless) the object into
    	#	the class
    	#
    	sub redeem {
    		my ($class, $obj) = @_;

    		if (ref $obj eq 'HASH') {
    			my $redeemed = {};
    			$redeemed->{$_} = $obj->{$_}
    				foreach (keys %$obj);
    			return bless $redeemed, $class;
    		}

    		my $redeemed = [];
    		$redeemed->[$_] = $obj->[$_]
    			foreach (0..$#$obj);
    		return bless $redeemed, $class;
    	}

# DESCRIPTION

Thread::Queue::Queueable (aka TQQ) provides abstract methods to be invoked whenever an object is enqueued or dequeued, in either the request or response direction, on a Thread::Queue::Duplex (TQD) queue.

The primary purpose is to simplify application logic so that marshalling/unmarhsalling of objects between threads is performed automatically. In addition, when subclassed, the application class can modify or add logic (e.g., notifying a server thread object to update its reference count when a wrapped object is passed between threads - see DBIx::Threaded for an example).

# METHODS

Refer to the included classdocs for summary and detailed method descriptions.

# SEE ALSO

Thread::Queue::Duplex, threads, threads::shared, Thread::Queue

# AUTHOR, COPYRIGHT, & LICENSE

Dean Arnold, Presicient Corp. darnold@presicient.com

Copyright(C) 2005, Presicient Corp., USA

Permission is granted to use this software under the same terms as Perl itself. Refer to the Perl Artistic License for details.