C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

virtual function template - design probelm

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Sebastian Faust
Guest





PostPosted: Wed Oct 29, 2003 1:40 am    Post subject: virtual function template - design probelm Reply with quote



Hi,

I have a design problem about which I am thinking now for a while and still
couldnt find any help in deja. What I need is something like a virtual
function template. I know that this is not possible, so I need "something
like" a virtual function template. I read in some threads that there is a
workaround using the visitor pattern. I read therefore in the book "Modern
c++ Design" about this pattern, but couldnt find any help to my approach. So
now to my problem:

class Instrument
{
public:
Instrument(Instrument* ptrSuccessor) : m_ptrSuccessor(ptrSuccessor)
{ }
template<typename Command> // I know this is not
possible, but that's what I wanna do
void virtual handle_command(Command& op) = 0;
protected:
Instrument* m_ptrSuccessor;
};

class ConcreteInstrument : public Instrument
{
public:
template<typename Command> // I know this is not
possible, but that's what I wanna do
void virtual handle_command(Command& op)
{
if(op->is_instrument())
{
_state = op->get_state();
}
else
m_ptrSuccessor->handle_command(op);
}
protected:
int _state; // this could be any other type, maybe the type
will later be defined in form of a template parameter of the class
// ConcreteInstrument, so then
ConcreteInstument will be a class template.
};

template<typename Type, Type TYPE>
class Command
{
public:
Command(Type& state) : _state(state) { };
public:
virtual bool is_instrument()
{
if(_state == TYPE)
return true;
else
return false;
}
virtual Type get_state() { return _state; }
protected:
Type _state;
};

Usually in Command._state there is some kind of raw data which includes
type-info, and the real state. In the ConcreteInstrument we then only need
the real state-data, so usually the get_state and is_instrument function
will perform some extracting processing to extract the needed data out of
the raw-data.

For any help on my problem thanks in advance,
Sebastian




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Mike
Guest





PostPosted: Wed Oct 29, 2003 4:46 pm    Post subject: Re: virtual function template - design probelm Reply with quote




"Sebastian Faust" <sfaust_deleteme (AT) logic-software (DOT) de> wrote
Quote:
Hi,

stuff


Im confused. Maybe you could simplify your example to clarify what you want.
There's a bunch of mistakes in your code too (apart from what you are
talking about). Since it's not clear, I'm not sure if this is what you want,
but here it goes. Ill sum it up at the bottom:
<CODE>

#include <iostream>
using namespace std;

template<typename T>
class Command{
public:
T t; //T must be convertible to int
};

class Base{
public:
virtual void HandleStuff(int stuff) = 0;
};

class Derived : public Base{
public:
virtual void HandleStuff(int stuff){
cout << "Handling stuff: " << stuff << endl;
}
};

template void HandleStuff(Base &b, command_t command)
{
b.HandleStuff((int)command.t);
};

</CODE>

The way i think of things, a global function template is to static
inheritance what a virtual function is to dynamic inheritance.

The other thing is, if you want to combine different types of functions
(template, virtual, static) just call one from the other. Like virtual
static: virtual function calls static function. template static: template
function calls static function. etc.

Michael Lin
Undergrad student
University of British Columbia



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Wed Oct 29, 2003 4:51 pm    Post subject: Re: virtual function template - design probelm Reply with quote



"Sebastian Faust" <sfaust_deleteme (AT) logic-software (DOT) de> wrote


Quote:
I have a design problem about which I am thinking now for a while and
still
couldnt find any help in deja. What I need is something like a virtual
function template. I know that this is not possible, so I need "something
like" a virtual function template. I read in some threads that there is a
workaround using the visitor pattern. I read therefore in the book "Modern
c++ Design" about this pattern, but couldnt find any help to my approach.
So
now to my problem:

I don't know a general solution.

You can convert template types to std::string using typeid, call a
non-template virtual function whose function argument is std::string, and in
the virtual override lookup the correct template function to call.

If you need to support only a finite number of types, can you use the double
dispatch idea to handle the registry? Double dispatch is when a function is
virtual in two arguments. I posted an implementation on this newsgroup with
subject "double dispatch: typeid, reinterpret_cast, new function".
Basically, you create your to be virtual function f(Derived1&, Derived2&).
Then you add this function to your registry using a template function. This
function concatenates typeid(Derived1).name() and typeid(Derived2).name(),
and calls a non-template function to add the function into a map. The map
is a map<string of the concatenated typeid, function pointer>.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sebastian Faust
Guest





PostPosted: Thu Oct 30, 2003 5:39 am    Post subject: Re: virtual function template - design probelm Reply with quote

Hi,

Quote:
Im confused. Maybe you could simplify your example to clarify what you
want.
There's a bunch of mistakes in your code too (apart from what you are
talking about). Since it's not clear, I'm not sure if this is what you
want,

So my basic problem is that I wanna implement the chain of responsibility
pattern. In my case the Instruments are the elments of the chain. Now I have
something lika a processor which creates Command-Objects. These Command
objects should be handled by the Instruments handle_command function. If one
Instrument "think" it is responsible for that command it will process it
further. Otherwise it will call the handle_command function of the
successor. The Command should be a class template cause the _state could
have a different types ( e.g. string, int, unsigned char ). And that's why I
think I need something like virtual function templates.

Quote:

#include using namespace std;

template class Command{
public:
T t; //T must be convertible to int
};

class Base{
public:
virtual void HandleStuff(int stuff) = 0;
};

class Derived : public Base{
public:
virtual void HandleStuff(int stuff){
cout << "Handling stuff: " << stuff << endl;
}
};

template void HandleStuff(Base &b, command_t command)
{
b.HandleStuff((int)command.t);
};
That wont work in my case, cause I don't know the type. So it can always be

another type than int, too.

Thanks,
Sebastian



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Christophe Lephay
Guest





PostPosted: Mon Nov 03, 2003 8:00 pm    Post subject: Re: virtual function template - design probelm Reply with quote

Sebastian Faust wrote:
Quote:
class Instrument
{
public:
Instrument(Instrument* ptrSuccessor) :
m_ptrSuccessor(ptrSuccessor) { }
template<typename Command> // I know this is
not possible, but that's what I wanna do
void virtual handle_command(Command& op) = 0;
protected:
Instrument* m_ptrSuccessor;
};

class ConcreteInstrument : public Instrument
{
public:
template<typename Command> // I know this is
not possible, but that's what I wanna do
void virtual handle_command(Command& op)
{
if(op->is_instrument())
{
_state = op->get_state();
}
else
m_ptrSuccessor->handle_command(op);
}
protected:
int _state; // this could be any other type, maybe the
type will later be defined in form of a template parameter of the
class // ConcreteInstrument, so then
ConcreteInstument will be a class template.
};

template class Command
{
public:
Command(Type& state) : _state(state) { };
public:
virtual bool is_instrument()
{
if(_state == TYPE)
return true;
else
return false;
}
virtual Type get_state() { return _state; }
protected:
Type _state;
};

Usually in Command._state there is some kind of raw data which
includes type-info, and the real state. In the ConcreteInstrument we
then only need the real state-data, so usually the get_state and
is_instrument function will perform some extracting processing to
extract the needed data out of the raw-data.

If you want only one command handler in your instrument, i don't see the
need here for a template function. Why cannot you derive your templated
class Command from another class CommandBase, which would be the only one
referred in your class Instrument ?

Chris



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.