Function name as text
Bill Baxter
dnewsgroup at billbaxter.com
Thu Dec 6 00:59:30 PST 2007
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
More information about the Digitalmars-d
mailing list