This question is related to finding the addresses of methods in objects, but let me first describe what I’m trying to do to give you context. I’m writing a data logging Lazarus application where there are a number of sensors (in this case, thermocouples) for which data should be logged. However, the exact number of them is unknown until run time. The provider of the sensor hardware (Phidgets) usefully provides a library to which you can interface to discover how many sensors there are and connect to them. Outside of making the initial connections to them, the primary means of communications with them is through events (presumably initiated by hardware) they create. This necessitates a callback for each event from each sensor. In this case there are 4 events per sensor (temperature, attached, detached, error), each of which requires its own separate procedure.
I’ve built and currently have running an application that accomplishes this, but it is an excellent example of spaghetti code. Since I’m most comfortable with the procedural way of thinking about coding software (I reckon from the early Turbo days), outside of using components on forms, I’ve always used Free Pascal in this way. However, it means that I have a whole slew of callback procedures, each slightly different, that litter my code. That they are each slightly different (at least in an array index) means that it’s easy to create errors.
I’ve built the necessary data structures as an array of thermocouple records, so it seems obvious that the next thing that I should do is to create an array of thermocouple objects within which are the callbacks for its particular sensor. The callbacks only need to access variables within the thermocouple object, so it’s a nice way of segmenting and simplifying the application. It also lends itself to an implementation where the number of sensors can be discovered at runtime since thermocouple objects can be created and destroyed as needed.
However, the callbacks need to be registered with the library that calls them, i.e., I need to pass the addresses of the procedures that respond to the callbacks to the Phidgets library. Here is where I have a problem that I haven’t been able to overcome, that is, taking the address of object methods. I always, in one way or another, reach the same point: an error message that says that it’s illegal to recast an untyped number as a pointer to a procedure.
I’m not sure why I’m receiving this message because I’m reusing code that I used in my original implementation. I’ve tried other suggested implementations based on other similar questions on this forum, but they all lead to the same results. I suspect it has to do with the fact that my callback procedures in the original code are static, so their addresses are “known” at compile time, whereas with objects, the addresses of the methods that implement the callbacks may not be known until run time. It strikes me that it isn’t impossible to find the addresses of the methods at run time, but it may be that such a capability isn’t currently implemented in Lazarus or Free Pascal.
So, my question has a few parts.
1. Is my understanding of the problem correct?
2. If not, how should I implement getting addresses of object methods? If so, is there a known work-around?
3. Any recommendations for a different approach that allows me to define the callback procedures only once, although generating the required code for a number of sensors, that number being something that isn’t known at compile time?
Many thanks for your help.