10-05-2011 12:35 PM
Hello,
I am reviewing some labwindows codes. But I am getting some problems understanding this very well.
Could some one give some explanation here with this struct? What the Performing_Tests() function is exactly doing here?
To start, I have this function called
Result = Performing_Tests (0xffffffff);
and this function is this:
int Performing_Tests(long Which_Tests)
{
int x=0,y=1;
int Result = 1;
while (All_Tests[x].FUNC != NULL){
if (y & Which_Tests && Result){
Result&=All_Tests[x].FUNC();
//
}
y<<=1;
x++;
}
return Result;
}
below is the structure used:
struct Functions{ int (*FUNC) (void); };
typedef struct Functions Function;
Function All_Tests[] = {
Apollo_boot,
NULL, //DO NOT REMOVE THIS LINE, It is used as the End of Tests (EOT) identifier.
};
10-06-2011 07:15 PM
Agyna,
There is quite a bit going on with this code, so its a bit difficult to really read into functionality without context to the larger program.
What I can take away is that its a sort of function selector with error checking, which you can add your array of functions to in the function typedef in the bottom (below apollo_test). Functions in this array must return an int and have no parameters.
When the code actually executes,
Performing_tests is called, and starts at the first function in the array (index zero), sets Y to 1, and sets the result to one. It then checks to see if y and which_tests bitwise have anything in common. If that is true, AND no previous result has failed, it keeps the result as a true value (ie, not in error), bit-shifts y over one, then does the next test in the list.
In this exact case, it will run like so:
It will check to see if we have reached the last test in the series (index 1, NULL in our case), which it has not. Then it checks y (=1) bitwise with 0xffffffff, which is true, and the current Result (also 1). Because this is true, it will return apollo_boot's integer value, and bitwise and it with result (which is currently 1). Basically it will "error" if apollo_boot does not share any bits with return and not run the test in the array all_tests.
This ultimately doesnt matter in our case though, because it bitshifts Y, then increments x to execute the next function, which is NULL, so we exit our while loop, and return the current value of Return, which will be a bitwise and of Apollo_boot and 1, so either a 1 or a 0.
I apologize if this is a convoluted explanation, but it is pretty compex code, and was clearly designed to do something very efficiently. I hope this brought some clarity to the issue.
Regards,
Kyle Mozdzyn
Applications Engineering
National Instruments
10-07-2011 02:04 PM
Thanks Kyle, it is clear enough.