Interception of method call

Started by
3 comments, last by VoidSpirit 7 years, 7 months ago

Hello!

I have new question.

Is there an ability to intercept an object's arbitrary method call or access to object's properties?

In details:

I have a class 'selector' incapsulating mechanics of enumeration and selection of objects of different classes. I'm interesting in ability to

call same-named methods of group of objects by calling an fictitious method of selector. Selector must translate it to calls of methods of enumerated objects with actual arguments list.

Set of methods unknown at compile time and it's impossible to make needed selector's methods.

For example:

// selector

selector s=s();

// initialize 's' selector, bind it to some objects

...

// call all objects methods

s.foo("test",12); // foo methods of all enumerated object same signatures will be called

If there is a combination of Angel Script mechanics, overloaded operators etc, it would be very usefull for me. Best way is to intercept method's call with information of method's name, types and values of arguments and returning value.

Advertisement

For this you'll need to implement some kind of reflection, i.e. provide a way for the script to find functions at run-time and then call them. This is perfectly possible to do, but I don't have anything already implemented for this.

Here's a couple of forum threads that have some implementations that you may use as a starting point:

http://www.gamedev.net/topic/629338-template-methods/

http://www.gamedev.net/topic/662663-templated-type-of-object-add-on/

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you for answer!

But, as I understand, reflection does not solve this problem.

In example code above 'selector' is an application class that has neither method 'foo' no other methods linking it to other classes. Selector does not know about other classes. I want it can dynamically intersept any-named method call and translate it to call of methods of enumerated objects. Any linking must be realized at run-time.

Desired mechanics:

I write at script:

selector s=selector();

s.bind(object_1);

...

s.bind(object_n);

s.move(0,0); // example method call. selector has no method 'move', and not all objects have. But an application can get this call as information about the name of called method and actual parameters, take each binded object, find same method 'move' in it and try to call it.

At current time code 's.move(0,0)' of course cases a compile-time error.

s.move(0,0) cannot be done as-is, since the script will only be able to call methods on the registered type that has been registered. However, you could provide something like this:

s.invoke('move', 0, 0);

Where the invoke method is implemented as a C++ method and takes the name of the method that should be called on all the bound objects and the arguments that should be used. You can make the invoke method support variable arguments through the use of the variable parameter type, combined with multiple overloads to support a variable number of arguments. Or you could use the dictionary type to pass the arguments to the invoke method, and the implementation extracts the arguments from the dictionary to pass them on to the bound object methods.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you! It was a reserve variant. I will most likely use it

This topic is closed to new replies.

Advertisement