ran "beautify-file" make target over perl5.cxx patch hunks and rewrote callback and extend examples in the style of existing examples

This commit is contained in:
Robert Stone 2013-11-14 09:22:23 -08:00
commit 43aefba9ee
3 changed files with 277 additions and 260 deletions

View file

@ -1,40 +1,48 @@
#!/usr/bin/perl
use strict;
use warnings;
# file: runme.pl
# This file illustrates the cross language polymorphism using directors.
use example;
{
package PerlCallback;
package PlCallback;
use base 'example::Callback';
sub run {
print "PerlCallback.run()\n";
print "PlCallback->run()\n";
}
}
# Create an Caller instance
$caller = example::Caller->new();
# Add a simple C++ callback (caller owns the callback, so
# we disown it first by clearing the .thisown flag).
print "Adding and calling a normal C++ callback\n";
print "----------------------------------------\n";
my $caller = example::Caller->new();
my $callback = example::Callback->new();
$callback = example::Callback->new();
$callback->DISOWN();
$caller->setCallback($callback);
$caller->call();
$caller->delCallback();
$callback = PerlCallback->new();
print "\n";
print
print "Adding and calling a Perl callback\n";
print "------------------------------------\n";
print "----------------------------------\n";
# Add a Perl callback (caller owns the callback, so we
# disown it first by calling DISOWN).
$callback = PlCallback->new();
$callback->DISOWN();
$caller->setCallback($callback);
$caller->call();
$caller->delCallback();
# Note that letting go of $callback will not attempt to destroy the
# object, ownership passed to $caller in the ->setCallback() call, and
# $callback was already destroyed in ->delCallback().
undef $callback;
# All done.
print "\n";
print "perl exit\n";

View file

@ -1,48 +1,56 @@
#!/usr/bin/perl
use strict;
use warnings;
use example;
# file: runme.pl
# This file illustrates the cross language polymorphism using directors.
use example;
# CEO class, which overrides Employee::getPosition().
{
# CEO class, which overrides Employee::getPosition().
package CEO;
use base 'example::Manager';
sub getPosition {
return 'CEO';
return "CEO";
}
}
# Create an instance of CEO, a class derived from the Java proxy of the
# underlying C++ class. The calls to getName() and getPosition() are standard,
# the call to getTitle() uses the director wrappers to call CEO.getPosition().
my $e = CEO->new('Alice');
print "${\ $e->getName } is a ${\ $e->getPosition() }\n";
print "Just call her \"${\ $e->getTitle() }\"\n";
# Create an instance of our employee extension class, CEO. The calls to
# getName() and getPosition() are standard, the call to getTitle() uses
# the director wrappers to call CEO->getPosition. $e = CEO->new("Alice")
$e = CEO->new("Alice");
print $e->getName(), " is a ", $e->getPosition(), "\n";
printf "Just call her \"%s\"\n", $e->getTitle();
print "----------------------\n";
# Create a new EmployeeList instance. This class does not have a C++
# director wrapper, but can be used freely with other classes that do.
my $list = example::EmployeeList->new();
$list = example::EmployeeList->new();
# EmployeeList owns its items, so we must surrender ownership of objects
# we add. This involves calling the DISOWN method to tell the
# C++ director to start reference counting.
$e->DISOWN();
$list->addEmployee($e);
print "----------------------\n";
# Now we access the first four items in list (three are C++ objects that
# EmployeeList's constructor adds, the last is our CEO). The virtual
# methods of all these instances are treated the same. For items 0, 1, and
# 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
# 2, both all methods resolve in C++. For item 3, our CEO, getTitle calls
# getPosition which resolves in Perl. The call to getPosition is
# slightly different, however, because of the overidden getPosition() call, since
# slightly different, however, from the $e->getPosition() call above, since
# now the object reference has been "laundered" by passing through
# EmployeeList as an Employee*. Previously, Perl resolved the call
# immediately in CEO, but now Perl thinks the object is an instance of
# class Employee. So the call passes through the
# class Employee (actually EmployeePtr). So the call passes through the
# Employee proxy class and on to the C wrappers and C++ director,
# eventually ending up back at the Perl CEO implementation of getPosition().
# eventually ending up back at the CEO implementation of getPosition().
# The call to getTitle() for item 3 runs the C++ Employee::getTitle()
# method, which in turn calls getPosition(). This virtual method call
# passes down through the C++ director class to the Perl implementation
@ -50,16 +58,22 @@ print "----------------------\n";
print "(position, title) for items 0-3:\n";
print " ${\ $list->get_item(0)->getPosition() }, \"${\ $list->get_item(0)->getTitle() }\"\n";
print " ${\ $list->get_item(1)->getPosition() }, \"${\ $list->get_item(1)->getTitle() }\"\n";
print " ${\ $list->get_item(2)->getPosition() }, \"${\ $list->get_item(2)->getTitle() }\"\n";
print " ${\ $list->get_item(3)->getPosition() }, \"${\ $list->get_item(3)->getTitle() }\"\n";
printf " %s, \"%s\"\n", $list->get_item(0)->getPosition(), $list->get_item(0)->getTitle();
printf " %s, \"%s\"\n", $list->get_item(1)->getPosition(), $list->get_item(1)->getTitle();
printf " %s, \"%s\"\n", $list->get_item(2)->getPosition(), $list->get_item(2)->getTitle();
printf " %s, \"%s\"\n", $list->get_item(3)->getPosition(), $list->get_item(3)->getTitle();
print "----------------------\n";
# Time to delete the EmployeeList, which will delete all the Employee*
# items it contains. The last item is our CEO, which gets destroyed as well.
# items it contains. The last item is our CEO, which gets destroyed as its
# reference count goes to zero. The Perl destructor runs, and is still
# able to call self.getName() since the underlying C++ object still
# exists. After this destructor runs the remaining C++ destructors run as
# usual to destroy the object.
undef $list;
print "----------------------\n";
# All done.
print "perl exit\n";