Declaring callback function inside FSEventStreamCreate

十年热恋 提交于 2020-01-04 17:34:01

问题


There are a couple examples of using an FSEvent to listen for changes in the file system.

How to listen for file system changes MAC - kFSEventStreamCreateFlagWatchRoot

and

FSEvents weirdness on OS X Leopard

When creating the event with FSEventStreamCreate they all seem to pass the callback item just fine. No parameters or anything, just &feCallback. Basically it seems as if they're passing a variable rather than a function, if that makes sense.

But I'm getting a Use of Undeclared identifier error when I try to do it. What gives?

 FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                  &feCallback, // what does '&' mean? Why are no parameters passed?
                                                  &cntxt, 
                                                  pathsToWatch, 
                                                  kFSEventStreamEventIdSinceNow, 
                                                  1,
                                                  kFSEventStreamCreateFlagWatchRoot );

and then later have the callback function:

static void feCallback(ConstFSEventStreamRef streamRef,
                       void* pClientCallBackInfo,
                       size_t numEvents,
                       void* pEventPaths,
                       const FSEventStreamEventFlags eventFlags[],
                       const FSEventStreamEventId eventIds[]) 
{
        NSLog(@"The file changed!"); 
}

I'd love some sample code to get the open-source helper object here working: https://bitbucket.org/boredzo/fs-notifier/overview

But same thing. It has the method:

- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath;

and I can't pass it a newCallback because of the error described above.


回答1:


When creating the event with FSEventStreamCreate they all seem to pass the callback item just fine. No parameters or anything, just &feCallback.

FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                 &feCallback, // what does '&' mean? Why are no parameters passed?

The & is the “address of” operator, and evaluates to a pointer to its operand. It's not really necessary here; a function name not in a function call always evaluates to the pointer to the function, with or without &.

So this passes the pointer to the feCallback function, which is how the FSEventStream object will call it back.

Basically it seems as if they're passing a variable rather than a function, if that makes sense.

That does make sense, but no, that's not correct. They are passing the function itself.

It is possible to declare a variable that holds a pointer to a function. If feCallback were such a variable, then feCallback and &feCallback would mean different things: feCallback would be the pointer to the function (inside the feCallback variable), whereas &feCallback would be the pointer to the variable.

In your case, though, with feCallback being a function, the two expressions are equivalent; either way passes the function pointer.

But I'm getting a Use of Undeclared identifier error when I try to do it. What gives?

You haven't declared that identifier (in this case, that function).

and then later have the callback function:

That's the problem. You have the callback function defined after your use of its name, but you did not declare the function before using its name. You must declare the function before you can use it (or pass it anywhere).

The reason is because the compiler doesn't know what “feCallback” is until you tell it, which is what the declaration does. When you try to refer to “feCallback” before declaring or defining it, the compiler doesn't know what you're talking about.

The other way would be to move the function's definition up. A definition counts as a declaration for everything that follows it. I'd leave the definition where it is, though, and just add a declaration near the top of the file.

Either way, then the compiler will know what feCallback is when you pass it to FSEventStreamCreate.



来源:https://stackoverflow.com/questions/7377392/declaring-callback-function-inside-fseventstreamcreate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!