Aspect oriented programming: Difference between revisions

(Expanded AOP description; clarified task. How about it? Someone back this out if it sux, or fix it.)
Line 74:
===Using function pointers===
 
In C a class-like object can be created using a table, or more commonly, a structure containing function pointers. This is often done in kernel programming for device drivers.
Although C is not strictly a functional programming language, it is possible to call a procedure from a pointer, and by re-assigning the pointer force a different procedure to be called. I forget the code for this but it may involve (void *) and &.
 
Here is a typical layout:
 
<lang c>struct object {
struct object_operations *ops;
int member;
};
 
struct object_operations {
void (*frob_member)(struct object *obj, int how);
};</lang>
 
In this example, an object is constructed as an instance of <code>struct object</code> and the <code>ops</code> field is filled in with a pointer to an operations table of type <code>struct object_operations</code>. The object is usually dynamically allocated, but the operations table is often statically allocated, and the function pointers are statically initialized.
 
A call to the <code>frob_member</code> method, if coded by hand without the help of any macros or wrapper functions, would look like this:
 
<lang c>pobj->frob_member(pobj, 42);</lang>
 
This representation opens the door to various possibilities. To gain control over all of the calls to an object, all we have to do is replace its <code>ops</code> pointer with a pointer to another operations structure of the same type, but with different function pointers.
 
With some further refinements and hacks, we can create a way for our "aspect" can keep additional context for itself, but associated with the original object, including a pointer to the original operations which the instrumenting operations can call.
 
Of course, this is a far cry from being able to instrument multiple functions from different classes across an entire hirarchy, in a single place.
 
=={{header|JavaScript}}==
Anonymous user