Function name as text
Craig Black
cblack at ara.com
Thu Dec 6 07:50:43 PST 2007
"Don Clugston" <dac at nospam.com.au> wrote in message
news:fj8j1p$2per$1 at digitalmars.com...
> Bill Baxter wrote:
>> Don Clugston wrote:
>>> Craig Black wrote:
>>>> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message
>>>> news:fj769h$7ou$1 at digitalmars.com...
>>>>> Craig Black wrote:
>>>>>> I have been considering porting some C++ code to D. One of the
>>>>>> classes I would have to port is an event queue class where each event
>>>>>> on the queue has a delegate and a text string that points to the
>>>>>> function name that the delegate refers to. The function name is used
>>>>>> to visualize the event queue for run-time debugging purposes. It is
>>>>>> important to capture both the class name and the function name as
>>>>>> text.
>>>>>>
>>>>>> In C++ I had a macro called DISPATCH that used the stringize operator
>>>>>> # to capture the name of the function. The good (and bad) thing
>>>>>> about C++ in this case is that when specifying a pointer to a member,
>>>>>> you must fully qualify the function name, so you would have something
>>>>>> like this.
>>>>>>
>>>>>> class Foo {
>>>>>> public:
>>>>>> void bar() {}
>>>>>> };
>>>>>>
>>>>>> Foo *foo = new Foo;
>>>>>> Event event = DISPATCH(foo, &Foo::bar);
>>>>>>
>>>>>> Using the stringize operator, the DISPATCH macro could capture the
>>>>>> text string "Foo::bar" as well as the member function pointer. Here
>>>>>> is the equivalent code in D..
>>>>>>
>>>>>> Foo foo = new Foo;
>>>>>> Event event = dispatch(&foo.bar);
>>>>>>
>>>>>> Which is much more elegant, except that I can't figure out a way to
>>>>>> capture the name of the function and it's class. I tried fiddling
>>>>>> with the stringof operator but that doesn't seem to work.
>>>>>>
>>>>>> Any ideas?
>>>>> There probably isn't a way to do it right now without using a string
>>>>> mixin, which uglies things up on the calling side:
>>>>>
>>>>> Event event = mixin(dispatch("&foo.bar"));
>>>>>
>>>>> Macros are supposed to give us a way to clean that up. But for now
>>>>> you're probably better off just passing the name separately like
>>>>> dispatch(&foo.bar, "foo");
>>>>>
>>>>> --bb
>>>>
>>>> I guess that's not so bad. It would be.
>>>>
>>>> Foo *foo = new Foo;
>>>> Event event = dispatch(&foo.bar, "Foo.bar");
>>>>
>>>> That is probably easier on the eyes than the mixin syntax. It stilll
>>>> would be cool if the compiler could somehow build the name
>>>> automatically.
>>>
>>> Or you could use an alias template parameter, to give the syntax:
>>>
>>> Event event = dispatch!(foo.bar);
>>>
>>
>> Really? Can you take the stringof an alias parameter and get back
>> "foo.bar" ? If so then nifty!
>>
>> --bb
>
> Unfortunately it's not quite so simply, since there are so many bugs in
> .stringof. I'm unable to get the 'foo'.
> ----
> class Foo
> {
> void bar(int d) {}
> }
>
> void GetName(alias F)()
> {
> pragma(msg, (&F).stringof); // prints "& bar"
> }
>
> void main()
> {
> GetName!(Foo.bar);
> }
> ----
> I have however done this previously using .mangleof to retrieve the fully
> qualified name. The behaviour with alias template parameters was one of
> the parts of my NameOf module which didn't get into .stringof. Sure would
> be nice if
> F.stringof worked.
I ran into this same thing with stringof. Are you sure that this is a bug?
Has it been officially reported?
More information about the Digitalmars-d
mailing list