Hi,
I am looking for a way to achieve the following. I've tried a couple
of things, but they all ended up being too complicated:
I have a templated class A. I want another class B to be able to call
a method defined in A's base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A's template parameters.
I'm imagining something like this - I know that it can't work like
this, but I would like to achieve a similarly simple syntax:
class Base
{
public:
virtual void call(...) = 0;
}
template <class T1,class T2>
class A : public Base
{
public:
void call(...)
{
if (typeid(T1) == ...)
...
else if (...)
...
else if (...)
...
}
}
class B
{
public:
void x()
{
Base *pSomePtr = ...;
pSomePtr->call(this,&B:: y);
};
template <class T1,class T2>
void y()
{
// do stuff
};
};
Any help on how I could realize this would be greatly appreciated .
Thanks,
Stefan
Feb 21 '07
12 1878
If this does not help lead you to a solution, I guess it is beyond
my knowledge to solve this.
--------------------------------------------------------------
#include <vector>
#include <boost/any.hpp>
class ProcBase
{
public:
virtual ~ProcBase() {}
virtual boost::any operator()( boost::any val ) =0;
};
template<typena me T1>
class Proc1 : public ProcBase
{
public:
virtual ~Proc1() {}
virtual boost::any operator()( boost::any val )
{
return boost::any( process(boost:: any_cast<T1>( val )) );
}
private:
T1 process( T1 v )
{
return T1();
}
};
template<typena me T1>
class Proc2 : public ProcBase
{
public:
virtual ~Proc2() {}
virtual boost::any operator()( boost::any val )
{
return boost::any( process(boost:: any_cast<T1>( val )) );
}
private:
T1 process( T1 v )
{
return T1();
}
};
enum ProcType
{
kProc1, kProc2
};
template<typena me T>
ProcBase *ProcFactory( ProcType t )
{
switch( t )
{
case kProc1:
return new Proc1<T>();
case kProc2:
return new Proc2<T>();
}
return NULL;
}
class Resource
{
public:
virtual ~Resource() {}
virtual void applyProc( ProcType type1, ProcType type2 ) = 0;
};
template<typena me T1>
class Array : public Resource
{
public:
virtual ~Array() {}
virtual void applyProc( ProcType t, ProcType )
{
ProcBase * processor = ProcFactory<T1> ( t );
for( unsigned int i=0; i < m_internal.size (); ++i )
{
m_internal[i] =
boost::any_cast <T1>((*processo r)( m_internal[i] ));
}
}
private:
std::vector<T1m _internal;
};
template<typena me T1, typename T2>
class Image : public Resource
{
public:
virtual ~Image() {}
virtual void applyProc( ProcType t1, ProcType t2 )
{
ProcBase * processor1 = ProcFactory<T1> (t1);
ProcBase * processor2 = ProcFactory<T2> (t2);
for( unsigned int i=0; i < m_first.size(); ++i )
{
m_first[i] =
boost::any_cast <T1>( (*processor1)( m_first[i] ));
}
for( unsigned int i=0; i < m_second.size() ; ++i )
{
m_second[i] =
boost::any_cast <T2>( (*processor2)( m_second[i] ));
}
}
private:
std::vector<T1m _first;
std::vector<T2m _second;
};
int
main()
{
Image<float,flo ati;
i.applyProc( kProc1, kProc2 );
}
On Feb 21, 6:56 pm, Piyo <cybermax...@ya hoo.comwrote:
stefan.bruck... @gmail.com wrote:
On Feb 21, 10:28 am, Piyo <cybermax...@ya hoo.comwrote:
stefan.bruck... @gmail.com wrote: On Feb 21, 8:50 am, John Harrison <john_androni.. .@hotmail.comwr ote: Piyo wrote: Darn, you beat me to it!! :) Interestingly enough, we came up with the same solution. BTW, I forgot (so I went and included it in my solution) do you need to do ->template in the call to member function y()? (See below). I can't remember the rules for ->template. I know it's purpose is to disambiguat e the syntax for the compiler, so I add it if the compiler is complaining . If the compiler isn't complaining I figure there is nothing to disambiguate. john Thanks for your replies. Maybe I should explain the scenario in a bit more detail: Class Base is actually something called a Resource, which can be an Array, Image, Volume, etc. Class A is one type of Resource, e.g a Volume. The voxels of the volume can have different types (char,int,float ,...) and number of components (1,2,...). Class B wants to perform some processing on a resource, for any type and any number of components. The algorithm will, in general, be independent of type and components (but there also might be the need for partial specialization for some types/number of components). All class B gets is a pointer to a resource and now it wants to call some member of the resource class which determines the actual type of resource and its voxel type and components and calls and appropriate templated member which I want to specify. The actual implementation of the algorithm is performance critical, so everything should be statically typed after this point. --Stefan
So here is my new suggestion. Based on this description, you seem to
be pushing the responsibility of knowing how to apply the algorithm
properly onto the algorithm. This is not the best way to do it. Since
your algorithm is independent of type and components, you can
encapsulate it as a templated functor. Then inside each concrete
Resource, you instantiate as many as Processors as you need for
each component you need to process and do the processing.
I sure hope this helps :)
-------------------------------------------------------------------
#include <boost/function.hpp>
template<typena me T1>
class Proc
{
public:
T1 operator()( const T1 &val )
{
// process
}
};
class Resource
{
virtual void applyProc() = 0;
};
template<typena me T1>
class Array
{
virtual void applyProc()
{
Proc<T1processo r;
for( unsigned int i=0; i < m_internal.size (); ++i )
{
m_internal[i] = processor( m_internal[i] );
}
}
private:
std::vector<T1m _internal;
};
template<typena me T1, typename T2>
class Image
{
virtual void applyProc()
{
Proc<T1processo r1;
Proc<T2processo r2;
for( unsigned int i=0; i < m_internal.size (); ++i )
{
m_first[i] = processor1( m_first[i] );
m_second[i] = processor2( m_second[i] );
}
}
private:
std::vector<T1m _first;
std::vector<T2m _second;
};
template<typena me T1, typename T2, typename T3>
class Volume
{
// .. you get the picture
};- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Well ... hmmmm ... the problem is that different algorithms are
performed on resources from basically all over the place. A plugin to
the application might implement a specific algorithms, for example. In
your solution, the basic problem remains:
Lets say, I now have Proc1, Proc2 and Proc3, all do different things.
At runtime, any one of them could be applied to a resource.
So I would need to pass an instance of Proc1, Proc2, or, Proc3 to
applyProc -- but that doesn't work because I don't know which
instance, since I don't know the template parameters of the resource.
The problem is you cannot pass Proc1, Proc2 or Proc3 since they are
Template-ids and not real functions. Another problem you are
encountering is the ability to determine template parameters from the
NON-template base class. There is a lot of mixing of compile-time and
run-time polymorphism here which spells trouble.
Exactly right, this mix is the problem but unfortunately unavoidable
(at least, I don't see a way) in the context of the whole application:
the flexibility of run-time polymorphismus is needed but at the level
of an actual algorithm it has to be broken due to performance reasons.
I think your last approach seems viable and I'll probably go for
something similar.
Thanks for your help,
Stefan st************* @gmail.com wrote:
>
I am looking for a way to achieve the following. I've tried a couple
of things, but they all ended up being too complicated:
I have a templated class A. I want another class B to be able to call
a method defined in A's base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A's template parameters.
I'm imagining something like this - I know that it can't work like
this, but I would like to achieve a similarly simple syntax:
class Base
{
public:
virtual void call(...) = 0;
}
virtual ~Base(){}
};
template <class T1,class T2>
class A : public Base
{
public:
void call(...)
{
if (typeid(T1) == ...)
...
else if (...)
...
else if (...)
...
Instead of "if (typeid(T1) == ...)" try
template <class T1>
class Derived: public Base
{
public:
void call(...);
};
template <class T1,class T2>
class A : public Derived<T1>
{
};
--
Maksim A. Polyanin http://grizlyk1.narod.ru/cpp_new
"In thi world of fairy tales rolls are liked olso"
/Gnume/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gianni Mariani |
last post by:
I was a little surprised by this:
It seems like the code below should not compile but the Comeau 4.3.3
compiler accepts it and the gcc 3.4(prerel) compiler rejects it and
MSVC++7.1 ICE's.
14.6.1 in the standard seems to imply that template parameters are
hidden by class members.
struct X
|
by: wkaras |
last post by:
I would like to propose the following changes to the C++ Standard,
the goal of which are to provide an improved ability to specify
the constraints on type parameters to templates. Let me say from
the start that my knowledge of compiler implementation is very
limited. Therefore, my suggestions may have to be rejected
because they are difficult or impossible to implement.
The proposal is based on the concept of "type similarity". Type...
|
by: Erik Wikström |
last post by:
I've been trying for a while now to understand how template template
parameters work. But I just can't wrap my head around it and was hoping
that someone might help me. As best I can figure the code should look
something like this:
template<template<typename S> typename T>
struct element_traits {
typedef S type;
};
|
by: rincewind |
last post by:
Hi,
can anybody summarise all options for partial template specialization,
for all kind of parameters (type, nontype, template)?
I *think* I understand options for partial specialization on type
parameters - in place of a template argument one can construct arbitrary
valid C++ type declaration, more or less like in "typedef" statement.
What about nontype parameters? Am I right that you cannot partially
|
by: Dan Krantz |
last post by:
I have the following template to ensure that a given number (val) falls into
a range (between vmin & vmax):
template<typename T> T ForceNumericRange( const T& val, const T& vmin, const
T& vmax)
{
T retVal = val;
if ( retVal < vmin )
retVal = vmin;
| |
by: Paul Roberts |
last post by:
Hi,
I'm hoping somebody here can help me with a simple problem of template
syntax.
Here's an example:
template<typename T, int iclass A
{
static int a;
|
by: desktop |
last post by:
I have this code from the book C++ Templates: The Complete Guide:
#ifndef ACCUM8_HPP_
#define ACCUM8_HPP_
#include "accumtraits4.hpp"
#include "sumpolicy2.hpp"
|
by: Jess |
last post by:
Hi,
I have a template function that triggered some compiler error. The
abridged version of the class and function is:
#include<memory>
using namespace std;
template <class T>
|
by: flopbucket |
last post by:
Hi,
I want to provide a specialization of a class for any type T that is a
std::map.
template<typename T>
class Foo
{
// ...
};
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |