D
Daniel Mitchell
I'm interested in computational physics and have defined an abstract base
class to represent a simulation. Now I want to develop a GUI for setting
the member data of classes derived from my base class.
I've approached this problem by creating a class named Interface with an
overloaded member function named addParameter():
class Interface {
public:
void addParameter( int& param );
void addParameter( double& param );
// etc.
};
Simulations call addParameter() on their data and the GUI works by keeping
pointers to the data. These pointers (along with other data to be displayed
by the GUI such as the name of the parameter, etc.) are stored in a
template struct named Parameter derived from a base class called
ParameterBase:
class ParameterBase {
virtual ~ParameterBase() {};
};
template<typename T>
struct Parameter : public ParameterBase {
T* data;
};
The GUI keeps track of these Parameters by managing a vector of
ParameterBase pointers, but this requires rtti. Can anyone suggest an
approach that doesn't?
I've also tried writing the Interface this way
class Interface {
public:
template<typename T>
void addParameter( T& param );
private:
template<typename T>
vector<T*>& parameters();
};
template<typename T>
vector<T*>& Interface:arameters()
{
static vector<T*> vec;
return vec;
}
template<typename T>
void Interface::addParameter( T& x )
{
parameters<T>().push_back( &x );
}
This avoids the problems associated with storing base class pointers, but
requires a way to keep track of what types addParameter() has been called
on. Furthermore I would still have to write code like
for( all the elements of parameters<int>() )
do_something
for( all the elements of parameters<double>() )
do_something
All comments are welcome.
Daniel
class to represent a simulation. Now I want to develop a GUI for setting
the member data of classes derived from my base class.
I've approached this problem by creating a class named Interface with an
overloaded member function named addParameter():
class Interface {
public:
void addParameter( int& param );
void addParameter( double& param );
// etc.
};
Simulations call addParameter() on their data and the GUI works by keeping
pointers to the data. These pointers (along with other data to be displayed
by the GUI such as the name of the parameter, etc.) are stored in a
template struct named Parameter derived from a base class called
ParameterBase:
class ParameterBase {
virtual ~ParameterBase() {};
};
template<typename T>
struct Parameter : public ParameterBase {
T* data;
};
The GUI keeps track of these Parameters by managing a vector of
ParameterBase pointers, but this requires rtti. Can anyone suggest an
approach that doesn't?
I've also tried writing the Interface this way
class Interface {
public:
template<typename T>
void addParameter( T& param );
private:
template<typename T>
vector<T*>& parameters();
};
template<typename T>
vector<T*>& Interface:arameters()
{
static vector<T*> vec;
return vec;
}
template<typename T>
void Interface::addParameter( T& x )
{
parameters<T>().push_back( &x );
}
This avoids the problems associated with storing base class pointers, but
requires a way to keep track of what types addParameter() has been called
on. Furthermore I would still have to write code like
for( all the elements of parameters<int>() )
do_something
for( all the elements of parameters<double>() )
do_something
All comments are welcome.
Daniel