Simplify block reference declaration using typedefs - objective-c

I'm working through Advanced Mac OS X Programming book and one exercise has me stumbled.
Use typedefs to simplify the following block reference declaration:
int (^(*(^get_block_factory_funcptr)(void))(int))(void);
Here's how I understand the declaration:
Function not taking arguments, returns a block which returns a void pointer, which is a pointer to a function that takes int for an argument and returns a block which has no arguments, returns an int.
Now given that, I have these 2 typedefs
typedef void *(^get_block_factory_funcptr)(void);
typedef int (^myBlock(int))(void);
However I have no idea how to combine them to a single declaration, any help appreciated

If I'm reading it correctly, it's a block that returns a function pointer that returns a block. The name is a hint.
One way to figure it out is to start with the end result and work backwards. Or, start from the inside and work outwards.
What is get_block_factory_funcptr? It's a block:
MyBlock get_block_factory_funcptr;
What does it do? It takes a void and it returns a "Block Factory" function pointer. Let's declare MyBlock:
typedef FactoryFuncPtr (^MyBlock)(void);
What is FactoryFuncPtr? It takes an int and returns another block:
typedef OuterBlock (*FactoryFuncPtr)(int);
What is OuterBlock? It takes a void and returns an int:
typedef int (^OuterBlock)(void);
Those are the declarations in reverse order.
Edit: A working example.
#import <Foundation/Foundation.h>
typedef int (^OuterBlock)(void);
typedef OuterBlock (*FactoryFuncPtr)(int);
typedef FactoryFuncPtr (^MyBlock)(void);
OuterBlock factory(int foo) {
return ^ {
printf("A block with %d\n", foo);
return 123;
};
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
int (^(*(^get_block_factory_funcptr)(void))(int))(void) = ^ {
return &factory;
};
MyBlock myBlock = get_block_factory_funcptr;
FactoryFuncPtr foo = myBlock();
OuterBlock bar = foo(999);
int baz = bar();
printf("Final %d\n", baz);
}
return 0;
}

Related

Pass Objective-C block or method as C-function pointer

I'm using https://github.com/nodejs/http-parser, the callbacks it uses are like this
struct http_parser_settings {
http_cb on_message_begin;
http_data_cb on_url;
http_data_cb on_status;
http_data_cb on_header_field;
http_data_cb on_header_value;
http_cb on_headers_complete;
http_data_cb on_body;
http_cb on_message_complete;
/* When on_chunk_header is called, the current chunk length is stored
* in parser->content_length.
*/
http_cb on_chunk_header;
http_cb on_chunk_complete;
};
The main callback type is defined here
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
I'm trying to find a way to pass either an Objective-C block or method as the function pointer in the parser_settings. However it lets me use only a C-function, which doesn't suit me because I also need to access the state of an Objective-C object in the callback
At the moment my solution is as follows:
int onHeaderField(http_parser* _, const char* at, size_t length) {
// Need to access state here, so doesn't work for me as a c function
char header[length];
strncpy(header, at, length);
NSLog(#"Header %s", header);
return 0;
}
...
- (void)method {
http_parser_settings settings;
settings.on_header_field = onHeaderField; // rather than func would like to set a block/method to capture and access self
size_t nparsed = http_parser_execute(self.parser, &parserSettings, charData, messageLength)
}
How would I go about accessing self from the callback passed to http_parser_execute?
Technically you can "extract" an Objective-C method implementation in form of a C-pointer with use of class_getMethodImplementation, however these implementations have objc_msgSend-like signature and always require the receiver as an argument, thus not really usable outside of Objective-C world:
NSString *str = #"Hollow World";
SEL sel = #selector(isEqualToString:);
Method meth = class_getInstanceMethod([str class], sel);
typedef BOOL(*IsEqualToStringPtr)(NSString *, SEL, NSString *);
IsEqualToStringPtr impl = (IsEqualToStringPtr)method_getImplementation(meth);
NSLog(#"Is equal? %#", impl(str, sel, #"Hello, World!") ? #"YES" : #"NO"); // prints "NO"
NSLog(#"Is equal? %#", impl(str, sel, #"Hollow World") ? #"YES" : #"NO"); // prints "YES"
Having that said, neither blocks nor Objective-C methods are directly convertible to a C function pointer (they are pointers to structures under the hood), especially when you want to complement it with any kind of context/state.
The simplest thing you can do is to use a global/statically allocated block variable which can be accessed from a C function without altering it's signature:
static int(^StaticBlock)(http_parser *parser, const char *at, size_t length);
static int my_callback(http_parser *parser, const char *at, size_t length) {
return StaticBlock(parser, at, length);
}
...
- (void)someObjectiveCMethod {
__weak typeof(self) weakSelf = self;
StaticBlock = ^(http_parser *parser, const char *at, size_t length) {
if (!weakSelf) {
return -1;
}
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.mprpty += length;
NSLog(#"Hello from Objective-C");
return 8;
};
http_parser_settings settings;
settings.on_header_field = my_callback;
}
The only viable alternative I can think of is using C++ lambdas. However it's still a big challenge when you need to access current state/context, let alone it will require you to switch to Objective-C++. If you are ok with it, first you need to rename your Objective-C file from SomeClass.m into SomeClass.mm. This way you tell Clang that the source code is Objective-C++ now and the compiler should accept a C++ code. Next, if your C library doesn't have C++ guards, you may want to wrap the C includes with extern "C" expression (otherwise linker would not be able to locate C symbols, because C++ mangles them):
extern "C" {
#include <c_header.h>
}
Now the tricky part: lambda expressions return special objects, closures, which can be seamlessly converted to C function pointers only if they don't capture anything from surrounding context. In our scenario it's not the case and it will require extra steps to convert it to a C pointer. Add this code somewhere in your *.mm file:
template<typename L>
struct c_functor_factory : c_functor_factory<decltype(&L::operator())> {};
template<typename R, typename F, typename ...Args>
struct c_functor_factory<R(F::*)(Args...) const> {
using pointer = typename std::add_pointer<R(Args...)>::type;
static pointer make_cptr(F&& func) {
static F instance = std::forward<F>(func);
return [](Args... args) {
return instance(std::forward<Args>(args)...);
};
}
};
template<typename L>
inline static typename c_functor_factory<L>::pointer make_cptr(L&& lambda) {
return c_functor_factory<L>::make_cptr(std::forward<L>(lambda));
}
In fact this solution is not much far from the global C function solution I suggested above. When a closure is passed as an argument here, this template function just perfect-forwards it to a statically allocated variable. As a result the static closure can be called from a capture-less lambda, which in turn is converted to a C function pointer.
Finally, you can make use of C++ lambda expressions and pass them as C function pointers anywhere in your Objective-C code:
- (void)someObjectiveCMethod {
__weak typeof(self) weakSelf = self;
const auto cptr = make_cptr([weakSelf](http_parser *parser, const char *at, size_t length) {
if (!weakSelf) {
return -1;
}
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.num += val;
NSLog(#"Hello from Objective-C++, %s!", at);
return 32;
});
http_parser_settings settings;
settings.on_header_field = my_callback;
}
Unlike the previous one, C++ solution is much more reliable, because each time your code hits the lambda expression, it emits a new closure object. In both cases, however, the function objects have static storage duration, thus make sure you don't pass any strong pointer in the body of it (otherwise it will never be released).

Running a C callback function inside an Objective-C completion block

I've been working on making an app with kivy/python and I needed to call some ios frameworks with obj-c. So I've made a couple layers of wrappers with python->cython->c->obj-c->framework. So far I've got everything to work up until the call back funtion that goes all the way back through to python. Right now the call back is failing somewhere between the cython->C->obj-c layer (never hits my print in cython). I feel like its because I'm trying to call user_func as a C function and not like an obj-c function. How should I go about running my C callback func inside of obj-c? I've peppered the code with prints (can't step through the way my setup is) and it prints the token that's generated and then crashes right on the user_func. It also never reaches the callback function in my cython file. So somewhere between the two is the source of the crash.
- (void) retrieveTokenObjC:(char*)myKey andcardNumber:(char*)cardNumber andexpMonth:(int)expMonth andexpYear:(int)expYear andcvc:(char*)cvc anduser_func:(tokenfunc)user_func anduser_data:(void*)user_data {
NSString* NScardNumber = [NSString stringWithUTF8String:cardNumber];
NSString* NScvc = [NSString stringWithUTF8String:cvc];
STPCardParams *cardParams = [[STPCardParams alloc] init];
cardParams.number = NScardNumber;
cardParams.expMonth = expMonth;
cardParams.expYear = expYear;
cardParams.cvc = NScvc;
NSString *myPublishableKey = [NSString stringWithUTF8String:myKey];
STPAPIClient *apiClient = [[STPAPIClient alloc] initWithPublishableKey:myPublishableKey];
[apiClient createTokenWithCard:cardParams completion:^(STPToken *token,NSError *error) {
if (token == nil || error != nil) {
const char* errorChar = [error.localizedDescription UTF8String];
user_func(errorChar,user_data);
} else {
const char* tokenChar = [token.tokenId UTF8String];
user_func(tokenChar,user_data);
}
}];
}
After this it goes the obj-c header
#import <Foundation/Foundation.h>
typedef void (*tokenfunc) (const char *name, void *user_data);
#interface retToken : NSObject
- (void) retrieveTokenObjC:(char*)myKey andcardNumber:(char*)cardNumber andexpMonth:(int)expMonth andexpYear:(int)expYear andcvc:(char*)cvc anduser_func:(tokenfunc)user_func anduser_data:(void*)user_data;
#end
Then it goes into a c wrapper for cython.
#include "stripe_ios_c.h"
#include "stripe_ios_imp.h"
void retrieveToken(char* myKey, char* cardNumber, int expMonth, int expYear, char* cvc,tokenfunc user_func, void *user_data){
retToken* retrieveToken = [[retToken alloc] init];
[retrieveToken retrieveTokenObjC:myKey andcardNumber:cardNumber andexpMonth:expMonth andexpYear:expYear andcvc:cvc anduser_func:user_func anduser_data:user_data];
}
Then the header file for the c wrapper
typedef void (*tokenfunc)(const char *name, void *user_data);
void retrieveToken(char* myKey, char* cardNumber, int expMonth, int expYear, char* cvc,tokenfunc user_func, void *user_data);
And finally to the cython code
__all__ = ['StripeWrapper']
cdef extern from "stripe_ios_c.h":
ctypedef void (*tokenfunc)(const char *name, void *user_data)
void retrieveToken(char* myKey, char* cardNumber, int expMonth, int expYear, char* cvc,tokenfunc user_func, void *user_data)
class StripeWrapper():
def __init__(self,**kwargs):
foo = 'bar'
pass
def getToken(self,tokenCallback,myKey,cardNumber,expMonth,expYear,cvc):
cdef bytes myKey_bytes = myKey.encode('utf-8')
cdef char* myKey_string = myKey_bytes
cdef bytes cardNumber_bytes = cardNumber.encode('utf-8')
cdef char* cardNumber_string = cardNumber_bytes
cdef bytes cvc_bytes = cvc.encode('utf-8')
cdef char* cvc_string = cvc_bytes
print myKey_bytes
print cardNumber_bytes
print cvc_bytes
print myKey_string
print cardNumber_string
print cvc_string
retrieveToken(myKey_bytes, cardNumber_bytes, expMonth, expYear, cvc_bytes, callback, <void*>tokenCallback)
print 'Debug 1'
cdef void callback(const char *name, void *tokenCallback):
print 'callback debug'
(<object>tokenCallback)(name.decode('utf-8'))
Update: I've tracked the issue down and my call back function executes the problem is the python callback is being deallocated somewhere along the way.
I solved the issue. The example here https://github.com/cython/cython/blob/master/Demos/callback/run_cheese.py for a cython callback won't work if you leave the main/current file. This is because the moment you leave that file the memory is deallocated. After pushing a python object and using
cdef void callback(const char *name, void *tokenCallback):
(<object> tokenCallback).token = (name.decode('utf-8'))
I feel like the cython is example is kind of a bad example and should have used an object to send the callback with which could have prevented lots of frustration but it finally works!

How does typedef-ing a block works

In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear.
Doing typedef for a block -typedef void (^MyBlock) (int a);
Now, we can use MyBlock.
Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock; similar to #define?
How the syntax works?
See Declaring a Block Reference in "Blocks Programming Topics":
Block variables hold references to blocks. You declare them using
syntax similar to that you use to declare a pointer to a function,
except that you use ^ instead of *.
So
typedef void (^myBlock) (int a);
defines a the type of a block using the same syntax as
typedef void (*myFunc) (int a);
declares a function pointer.
See e.g. Understanding typedefs for function pointers in C for more information about function pointers.
Also from "Blocks Programming Topics", creating a type for blocks should be like this:
typedef returnType (^blockName)(argument1, argument2, ...)
Below is a very simple practical example:
typedef float (^MyBlockType)(float, float);
MyBlockType AddTwoFloat = ^(float a, float b) {return a + b;};
MyBlockType MultiplyTwoFloat = ^(float a, float b) {return a * b;};
float c = AddTwoFloat(1, 2); //c = 3
float d = MultiplyTwoFloat(1, 2); //d = 2

from within a static function how to place info into iVars?

And note that I can not pass in a ViewController pointer due to this function being passed into another function.
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
NSString *str = #"";
int i;
for(i=0; i<argc; i++)
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
str = [NSString stringWithFormat:#"%#\n%s = %s\n", str, azColName[i], argv[i] ? argv[i] : "NULL"];
}
printf("\n");
//tvDisplay is a UITextView
[tvDisplay setText:str]; // <---- ??? how to get to an iVar
return 0;
}
the call:
rc = sqlite3_exec(db, pSQL[i], callback, 0, &zErrMsg);
Callback functions typically have an argument that allows you to pass along arbitrary data (it's usually a void * called context or something similar). You can pass in the object that you need to access when you set up the callback function, and then retrieve it within the callback function:
static void myCallback(int someResult, void *context) {
SomeClass *someObject = (SomeClass *)context;
[someObject doStuff];
}
In your particular case, the place for the "arbitrary data that you want to access in the callback function" is the void * argument right after the callback function itself that you have presently set to 0:
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
Keep in mind that you're responsible for ensuring that any data you stick in there remains valid while the callback has not yet returned, and, if necessary, free it in the callback.

Can an Obj-C Block execute itself?

This is an extension of this queston:
Is it possible to create a category of the "Block" object in Objective-C.
Basically while it seems possible to create a category on blocks, either through NSObject or NSBlock, I'm having trouble understanding how the block would be able to evaluate itself. The example given in the answer to the last question:
- (void) doFoo {
//do something awesome with self, a block
//however, you can't do "self()".
//You'll have to cast it to a block-type variable and use that
}
Implies that it is possible to somehow cast self to a block variable, but how would one execute the block itself? For example, say I did a category on NSBlock and in a method did:
NSBlock* selfAsBlock = (NSBlock*)self;
Is there any message I can send to selfAsBlock to have the block evaluate?
Implies that it is possible to somehow cast self to a block variable
Like this:
- (void)doFoo {
// Assume the block receives an int, returns an int,
// and cast self to the corresponding block type
int (^selfBlock)(int) = (int (^)(int))self;
// Call itself and print the return value
printf("in doFoo: %d\n", selfBlock(42));
}
Note that (in most cases) you need to fix the block signature so that the compiler is able to set up the call site according to the target platform ABI. In the example above, the signature is return type int, single parameter of type int.
A full example is:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#interface Foo : NSObject
- (void)doFoo;
#end
#implementation Foo
- (void)doFoo {
// Assume the block receives an int, returns an int,
// and cast self to the corresponding block type
int (^selfBlock)(int) = (int (^)(int))self;
// Call itself and print the return value
printf("in doFoo: %d\n", selfBlock(42));
}
#end
int main(void) {
[NSAutoreleasePool new];
// From Dave's answer
Method m = class_getInstanceMethod([Foo class], #selector(doFoo));
IMP doFoo = method_getImplementation(m);
const char *type = method_getTypeEncoding(m);
Class nsblock = NSClassFromString(#"NSBlock");
class_addMethod(nsblock, #selector(doFoo), doFoo, type);
// A block that receives an int, returns an int
int (^doubler)(int) = ^int(int someNumber){ return someNumber + someNumber; };
// Call the category method which in turn calls itself (the block)
[doubler doFoo];
return 0;
}
NSBlock has an invoke method that can be used to call the block.
NSBlock* b = ^() { /* do stuff */ };
[b invoke];
Note that this is a private, undocumented method.