Changing name of function being called in a template

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Changing name of function being called in a template

Hughes, James
Hello all,

I have a feeling this may be off the topic of Boost, so if anyone can point
me in the direction of a better place to ask please do (a templates mailing
list?). However, I post it here because I think Boost::Bind does something
similar internally to what I want to do, so someone may be able to advise.

I have a couple of templated class which implement a simple observer pattern
(yes I know Boost has signals, but it's not thread safe).

It uses a virtual function to make a call in to the observer from the
subject. My own observer then derive from this class and implement the
notify function. However, I would like to 'template' the name of the
function, so that different instances of the template can make calls to
different named function.

Now in Boost:bind you specify the name of the function in the class to be
called, but I don't know how to implement that in my own template class.

class observer<typename T>
{
virtual void Notify(T&); // This is the function I want to rename
according to the template
}

so I might want, in a different instance, the Notify function to be called,
Shutdown(). Following code is NOT valid C++, but gives an idea of what I am
trying to implement, but don't know enough yet to do so!!

class observer<typename T, typename functionname>
{
virtual void <functionname>(T&)
}

Is there a way I can use templates to do this? Boost:bind can call a
particular member function passed in as part of the template defn, how do I
that?

I hope that is vaguely clear...

James


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Changing name of function being called in a template

Wu Yinghui, Freddie
> From:

> James Hughes <[hidden email]>
> Date:
> Tue, 11 Apr 2006 10:14:12 +0100
>
> Hello all,
>
> I have a feeling this may be off the topic of Boost, so if anyone can point
> me in the direction of a better place to ask please do (a templates mailing
> list?). However, I post it here because I think Boost::Bind does something
> similar internally to what I want to do, so someone may be able to advise.
>
> I have a couple of templated class which implement a simple observer pattern
> (yes I know Boost has signals, but it's not thread safe).
>
> It uses a virtual function to make a call in to the observer from the
> subject. My own observer then derive from this class and implement the
> notify function. However, I would like to 'template' the name of the
> function, so that different instances of the template can make calls to
> different named function.
>
> Now in Boost:bind you specify the name of the function in the class to be
> called, but I don't know how to implement that in my own template class.
>
> class observer<typename T>
> {
> virtual void Notify(T&); // This is the function I want to rename
> according to the template
> }
>
> so I might want, in a different instance, the Notify function to be called,
> Shutdown(). Following code is NOT valid C++, but gives an idea of what I am
> trying to implement, but don't know enough yet to do so!!
>
> class observer<typename T, typename functionname>
> {
> virtual void <functionname>(T&)
> }
>
> Is there a way I can use templates to do this? Boost:bind can call a
> particular member function passed in as part of the template defn, how do I
> that?
>
> I hope that is vaguely clear...
>
> James
Have you checked out Boost.Function? You can use a
boost::function<void (T &)>
instance in your class to point to the correct member function you want
to call.

Alternatively, you may try to use member function pointer:

template <typename T, void (Observer::*Notify)(T & )>
class ....
{ }

Just my 5 cents...

Cheers,

Freddie


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

yhwu.vcf (813 bytes) Download Attachment
signature.asc (195 bytes) Download Attachment