question/clarification: pointer to function as passed parameter

M

ma740988

Consider the 'C' source.

void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal)
{
doorBellDetected = doorBellVal;
}

void slRcv()
{
starLinkOpenStruct myOpenStruct;
// later
myOpenStruct.starLinkId = FFT_NODE;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = PSEUDO_ROOT_NODE;
};

When view from C++ perspective, I'd like pass in myOpenStruct to the
constructor of a class called recv. So now:

int main()
{
// have user setup and pass in the open struct
starLinkOpenStruct myOpenStruct;
myOpenStruct.starLinkId = FFT_NODE;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = PSEUDO_ROOT_NODE;

recv* ptr = new recv (myOpenStruct);
}

// recv looks like
class recv
{
public:
recv (starLinkOpenStuct& open_struct)
{
// stuff
}
// more stuff
};


Trouble is doorBellCallBack poses a potential problem, hence I'm trying
to figure out an ideal approach when one of the passed parmams is a
pointer to a function?

Of course another potential issue is the fact that this approach
requires the user to know what the name of the function (in this case
myDoorBellISR) is.

///////
My initial thought

static void myDoorBellISR(starLinkDevice *slDevice,
U32 doorBellVal)
{
doorBellDetected = doorBellVal;
}

class recv
{
public:
recv (starLinkOpenStuct& open_struct)
{
// stuff
}
// more stuff
};


Here again this forces the user to know the name of the
doorBellCallback function. Not good. Help!!


Thanks in advance.
 
V

Victor Bazarov

Consider the 'C' source.

void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal)
{

slDevice doesn't seem to be used here.
doorBellDetected = doorBellVal;
}

void slRcv()
{
starLinkOpenStruct myOpenStruct;
// later
myOpenStruct.starLinkId = FFT_NODE;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = PSEUDO_ROOT_NODE;
};

Considering this 'C' source requires a bunch of assumptions. For example,
'starLinkDevice' is not defined. 'starLinkOpeStruct' is not defined. U32
is not defined. Plenty of other things are undefined as well.
When view from C++ perspective, I'd like pass in myOpenStruct to the
constructor of a class called recv. So now:

int main()
{
// have user setup and pass in the open struct
starLinkOpenStruct myOpenStruct;
myOpenStruct.starLinkId = FFT_NODE;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = PSEUDO_ROOT_NODE;

recv* ptr = new recv (myOpenStruct);
}

// recv looks like
class recv
{
public:
recv (starLinkOpenStuct& open_struct)
{
// stuff
}
// more stuff
};

Given the same assumption as with the "'C' source", looks OK.
Trouble is doorBellCallBack poses a potential problem,

Really? What problem is that?
hence I'm trying
to figure out an ideal approach when one of the passed parmams is a
pointer to a function?

None of the passed parmams is a pointer to function in your code. You
have one argument -- a reference to 'starLinkOpenStruct'.
Of course another potential issue is the fact that this approach
requires the user to know what the name of the function (in this case
myDoorBellISR) is.
Huh?

///////
My initial thought

static void myDoorBellISR(starLinkDevice *slDevice,
U32 doorBellVal)
{
doorBellDetected = doorBellVal;
}

class recv
{
public:
recv (starLinkOpenStuct& open_struct)
{
// stuff
}
// more stuff
};

Your initial thought is fine. Nothing here involves any pointers to
function AFAICS.
Here again this forces the user to know the name of the
doorBellCallback function.

WHERE? I don't see any 'doorBellCallback' in that "my initial thought"
piece of code.
Not good. Help!!

Help you do what? Not good what?

V
 
M

ma740988

Your initial thought is fine. Nothing here involves any poi­nters to
function AFAICS

Thank you sir.

Victor, I'm batting 1/3 with you. :) I envisioned it would probably
take me two to three times to get it right since I might not have
understood something about your reponse or poor post on my part to
begin with :)

For clarification, here's the starLinkOpenStruct definition. So now.

// stalink.h
typedef unsigned int U32

// later
typedef struct _starLinkOpenStruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallback)(struct _starLinkDevice *slDev, U32
doorBellVal);
} starLinkOpenStruct;

starLinkDevice is additonal struct which includes another struct and
that struct includes another struct, so for the purposes of discussion
and simplicity I'll modify it to look like.

// slink_mod.h
#ifndef SLINK_MOD_H
#define SLINK_MOD_H

typedef unsigned int U32;
// later
typedef struct _starLinkOpenStruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallback)(U32 doorBellVal);
} starLinkOpenStruct;
#endif


// receiver.h
#ifndef RECV_H
#define RECV_H

# include "slink_mod.h"
static void myDoorBellISR(U32 doorBellVal)
{
doorBellVal = 5;
}

class receiver
{
public:
receiver (starLinkOpenStruct& slink_open_struct)
{
std::cout << " receiver called " << std::endl;
}
~receiver() {}
};
#endif

// test.cpp
# include <iostream>
# include "receiver.h"
# include "slink_mod.h"

int main()
{
starLinkOpenStruct myOpenStruct;
myOpenStruct.starLinkId = 100;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = 10;
receiver* recv = new receiver(myOpenStruct);
delete recv;
}

So I wrestled with the line.
myOpenStruct.doorBellCallback = myDoorBellISR;

For some strange reason I thought that having the user specify the
doorBellCallback member function is inane.
 
V

Victor Bazarov

function AFAICS

Thank you sir.

Victor, I'm batting 1/3 with you. :) I envisioned it would probably
take me two to three times to get it right since I might not have
understood something about your reponse or poor post on my part to
begin with :)

For clarification, here's the starLinkOpenStruct definition. So now.

// stalink.h
typedef unsigned int U32

// later
typedef struct _starLinkOpenStruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallback)(struct _starLinkDevice *slDev, U32
doorBellVal);
} starLinkOpenStruct;

starLinkDevice is additonal struct which includes another struct and
that struct includes another struct, so for the purposes of discussion
and simplicity I'll modify it to look like.

// slink_mod.h
#ifndef SLINK_MOD_H
#define SLINK_MOD_H

typedef unsigned int U32;
// later
typedef struct _starLinkOpenStruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallback)(U32 doorBellVal);
} starLinkOpenStruct;
#endif


// receiver.h
#ifndef RECV_H
#define RECV_H

# include "slink_mod.h"
static void myDoorBellISR(U32 doorBellVal)
{
doorBellVal = 5;
}

class receiver
{
public:
receiver (starLinkOpenStruct& slink_open_struct)
{
std::cout << " receiver called " << std::endl;
}
~receiver() {}
};
#endif

// test.cpp
# include <iostream>
# include "receiver.h"
# include "slink_mod.h"

int main()
{
starLinkOpenStruct myOpenStruct;
myOpenStruct.starLinkId = 100;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = 10;

I would probably have written

starLinkOpenStruct myOpenStruct = { 0, 100, 0, 10, myDoorBellISR };

OTOH, your way helps to understand what members the numbers correspond
to. BTW, the 'idx' member is left uninitialised.
receiver* recv = new receiver(myOpenStruct);
delete recv;
}

So I wrestled with the line.
myOpenStruct.doorBellCallback = myDoorBellISR;

"Wrestled"? In what way? Did it compile? Did it do what you expected
it to do?
For some strange reason I thought that having the user specify the
doorBellCallback member function is inane.

It's not a member function. It's a member [of 'starLinkOpenStruct'] that
just happens to be a pointer to a function. And why is it inane? The
user has to initialise (or assign in your case) all the members so that
the struct is useful. 'doorBellCallback' is just another member to be
initialised (assigned). Otherwise it contains garbage and cannot be used.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top