Thread: Function Import
View Single Post
  #1  
Old 06-23-2007, 08:54 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Function Import

I had requested this feature along time ago and the new function objects have made it possible now.

This will create function pointers in an object. Variable list:
fromobj - Object, The object you want to import functions from.
toobj - Object, The object you want to import functions to.
fromprefix - String, Only import functions starting with this text. If null, all functions imported.
toprefix - String, Imported functions will be prefixed with this.
events - Boolean, Should functions starting with "on" be included in the operations?
PHP Code:
public function import(fromobj, toobj, fromprefix, toprefix, events) {
  
temp.vars = 0;
  
temp.i = 0;
  
  if (
fromobj.type() != 2 || toobj.type() != 2) {
    echo(
"System: Import Error");
    return;
  }
  
vars = fromobj.getFunctions();
  if (
fromprefix != null) {
    for (
i: vars) {
      if (
i.starts("on") && events == false)
        continue;
      if (
fromobj.(@ i).starts(fromprefix)) {
        if (
toprefix == null) {
          
toobj.(@ i) = fromobj.(@ i);
          
toobj.importedfunctions.add(i);
        }
        else {
          
toobj.(@ toprefix @ i) = fromobj.(@ i);
          
toobj.importedfunctions.add(toprefix @ i);
        }
      }
    }
  }
  else {
    for (
i: vars) {
      if (
i.starts("on") && events == false)
        continue;
      if (
toprefix == null) {
        
toobj.(@ i) = fromobj.(@ i);
        
toobj.importedfunctions.add(i);
      }
      else {
        
toobj.(@ toprefix @ i) = fromobj.(@ i);
        
toobj.importedfunctions.add(toprefix @ i);
      }
    }
  }
}
public function 
deport(obj) {
  
temp.vars = 0;
  
temp.i = 0;
  
  
vars = obj.importedfunctions;
  for (
i: vars) {
    
obj.(@ i) = null;
  }
} 
Example:
PHP Code:
// WEAPON: InverTest
function testf(text) {
  echo(
"TESTF: " @ text);
}
function 
test2(text) {
  echo(
"Test2: " @ text);
} 
PHP Code:
// WEAPON: InverTest2
function onCreated() {
  
System.import(InverTest, this, "test", null, false); // importing functions that start with 'test'
  
testf("ZOMG");
  
test2("RAWR");
  
System.deport(this); // removes imported functions
} 
PHP Code:
// Example script
function onCreated() {
  
System.import(MudControl, this, null, "mud_", false);
  
// Importing all functions from MudControl and prefixing with mud_ to remove conflict.
} 
I've also noted that using this method can be used even if the imported functions are not public.
__________________

Last edited by Inverness; 06-23-2007 at 09:06 AM..
Reply With Quote