hmmmm

On 13 Mar 2002, (Jason E. Stewart) wrote:

> "John Utz" <[EMAIL PROTECTED]> writes:
> 
> > I think that i have created the correct actual_cast and reinterpret_class,
> > but i just realized that this doesnt get propogated to the methods,
> > 
> > i see there is some code that has some 'transmogrification' regexp work
> > and does this work on the lsit @dom_node_methods.
> > 
> > do i need to do something with this? do i need to create a
> > @grammar_methods? what goes into this list and what doesnt go into this
> > list, or am i barking up the wrong tree?
> 
> You're barking pretty loud. Chances are it's the wrong tree ;-)

really? i am working from the example of the DOM stuff, and the methods
that are listed in the @dom_node_methods all end up in Xerces.pm with
the following line added:

    # automatically convert to base class
    $result = $result->actual_cast();


> At about line 609 in postModule.pl, there's the begining of a HERE
> doc:
> 
>   my $extra = <<'EXTRA';
> 
> which goes until almost the end of the file at line 1234. The last few
> lines of the script print out $extra to the TEMP filehandle, and then
> the temp file gets renamed to Xerces.pm. 
> 
> The moral of this story is that anything that you insert between the
> start of the HERE doc and the end will get printed out verbatim into
> Xerces.pm.
> 
> The only thing you have to watch out for is the 'package'
> declarations, like:
> 
>    package XML::Xerces::DOM_Node;
> 
> every method defined after that line will be in the
> XML::Xerces::DOM_Node package, until a new package declaration occurs.
> 
> So if you want stuff to appear in the XML::Xerces::Grammar package,
> you'll need to stick it right before one of my package defs like so:
> 
>    package XML::Xerces::Grammar;
> 
>    # insert you methods here
> 
>    package XML::Xerces::DOM_Node;
> 
>    # define methods for DOM_Node...
> 
> Also, for actual_cast() you don't need the following:
> 
>   sub actual_cast {
>     return undef unless _isa( ref($_[0]), 'XML::Xerces::DOM_Node' );
>     return $_[0] if $_[0]->isNull;
> 
> This should work:
> 
>   sub actual_cast {
>     my ($self) = @_;
>     return undef unless $self->isa('XML::Xerces::Grammar');
> 
> The isNull() stuff is only for DOM_Node subclasses, and I have no idea
> why the _isa() method exists. As far as I can tell, it is just a very
> inefficient reimplementation of UNIVERSAL::isa(). If Harmon or
> Frederick Paul are listening maybe they can enlighten me.

well, i thing that i actually had all those parts sorted out yesterday,
before i sent the mail:

package XML::Xerces::Grammar;

sub _isa {
  return 0 unless defined $_[0];

  my %package_hash = ($_[0] => 1);
  my @package_array = ($_[0]);
  my $base = $_[1];

  while(my $class = shift @package_array) {
    return 1 if "$class" eq "$base";
    foreach my $inherit (eval "\@$class\::ISA;") {
      unless ($package_hash{$inherit}) {
        $package_hash{$inherit} = 1;
        push @package_array, $inherit;
      }
    }
  }
  return 0;
};


sub _reinterpret_cast {
  return undef unless ref $_[1];

  bless $_[1], $_[0];

  my($raw_ref, $tied) = ($_[1] =~ m[=(.*)\(]);

  if ($raw_ref eq 'SCALAR')   { $tied = tied ${$_[1]}; }
  elsif ($raw_ref eq 'ARRAY') { $tied = tied @{$_[1]}; }
  elsif ($raw_ref eq 'HASH')  { $tied = tied %{$_[1]}; }

  return $_[1] unless $tied;

  bless $tied, $_[0];
  return $_[1];
};

sub actual_cast {
  return undef unless _isa( ref($_[0]), 'XML::Xerces::Grammar' );
  return $_[0] if $_[0]->isNull;

  my $grammar_type = $_[0]->getGrammarType;
  return _reinterpret_cast('XML::Xerces::SchemaGrammar', $_[0])
      if $grammar_type == $XML::Xerces::SchemaGrammar;
  return _reinterpret_cast('XML::Xerces::Grammar', $_[0])
      if $grammar_type == $XML::Xerces::Grammar;

  return $_[0];
}


but i dont think this is sufficient, because it appears to me that the
call to actual_cast has to be inserted in the methods in the pm file.

thats what i assume this code is doing:

    #   I?DOM_Node: automatically convert to base class
    if (grep {/$CURR_CLASS/} @dom_node_methods) {
      if (my ($sub) = /^sub\s+([\w_]+)/) {
        $sub = "$ {CURR_CLASS}::$sub";
        if (grep {/$sub$/} @dom_node_methods) {
          my $fix = <<'EOT';
    # automatically convert to base class
    $result = $result->actual_cast();
EOT
          fix_method(\*FILE,
                     \*TEMP,
                     qr/return undef/,
                     $fix,
                     1);
          next;
        }
      }
    }


so, am i still wrong? i am content to be wrong. :-) 

10 days ago i knew zip about SWIG, and this was probably not the project i
would have picked for getting my tootsies wet :-)

but the help you've provided has more than made up for the steepness of
the learning curve...

> HTH,
> jas.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to