Re: [ANN] Smalltalk style Blocks and iterators
Re: [ANN] Smalltalk style Blocks and iterators
- Subject: Re: [ANN] Smalltalk style Blocks and iterators
- From: Scott Ribe <email@hidden>
- Date: Mon, 04 Oct 2004 08:45:44 -0600
>
Not knowing too much about templates myself, would it work to do that
>
in a way so it can be called like an objc-object or would I have to go
>
all the way to c++ for that?
That's the opposite of what I did, so I can't answer straight off... It
certainly seems to me that you should be able to have an Objective-C object
that wraps this functionality, so that Objective-C callers would be isolated
from the underlying C++ implementation.
What I have done is create a functor type that must be created in an
Objective-C++ context, IOW a .mm file that includes Cocoa and some C++
library headers. But the functor can then be invoked from a pure C++
context. I'm starting with a Carbon app, and adding Cocoa, so I don't want
to turn all my pure C++ library .cp files into Objective-C++ files in order
to be able to invoked user interface callbacks. But I don't mind making all
my newly-added Cocoa stuff Objective-C++.
>
After having a deeper look at this, I now know that I need to ask some
>
questions (also because I couldn't find literature about this with my
>
keywords - if you know a really good url, please let me know).
My work is based on the generalized functors from:
<
http://www.amazon.com/exec/obidos/ASIN/0201704315/qid=1096899942/>
This is not a discussion that will fit info a few emails. Actually, I found
it took some work and several tries to fit the whole thing into my head ;-)
>
- Is it possible to specify a variable number of template arguments?
>
From my initial investigation it seems this is not possible, but
No, but it is possible to specify overloads--templates with the same name
and different numbers of arguments up to some pre-determined limit. This is
an ugly implementation hack, but it works.
>
- it is possible to specify a complete function as a template type
>
though it seems not to be possible to use this template type then as
>
the type of a function, or is it?
Not without dangerous casting. What Alexandrescu does is define a base type
that gets passed around, which then contains a pointer to an abstract
implementation type. The constructors of the base type determine which
concrete implementation type to instantiate. So for example, if we want a
callback interface that takes an id and a double and returns a long:
typedef Functor< long, TYPELIST_2( id, double ) > FunT;
Then to wrap an ordinary function myfun:
FunT fun1( myfun );
But to wrap a member function mymemfun of some object:
FunT fun2( this, mymemfun );
So fun1 and fun2 are the same type, are both invoked like:
long val = fun1( someobjcobj, 2.0 );
And can both be put into standard library containers and so on. So what I
did was add an ObjCFun implementation so that:
ObjCFun fun3( self, mymethod );
Is type-compatible with fun1 & fun2, so that from an Objective-C method I
can create fun3 and pass it to a function that registers it by putting it
into a map< some base functor type > along with other functors that call C++
member functions.
BTW, the TYPELIST family of macros is another chapter of the
above-referenced book, useful for, uhhh, lots of complicated things...
I was planning to write up what I've done at some point and make it
available. There is some ugliness with NSInvocation performing bitwise
copying of C++ types, not calling copy constructors, which for now means I
must only pass pointers to C++ objects. I'm thinking about that one. I've
only implemented and tested this on methods that return void, because that's
all I've needed so far. The extension to deal with return values should be
pretty straightforward, but I haven't done it yet.
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 665-7007 voice
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden