473,769 Members | 3,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding rtti

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<typena me 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<typena me T>
void addParameter( T& param );
private:
template<typena me T>
vector<T*>& parameters();
};

template<typena me T>
vector<T*>& Interface::para meters()
{
static vector<T*> vec;
return vec;
}

template<typena me T>
void Interface::addP arameter( 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<doub le>() )
do_something

All comments are welcome.

Daniel
Jul 22 '05 #1
4 1736
Daniel Mitchell <da*********@ma il.utexas.edu> wrote in
news:cc******** **@geraldo.cc.u texas.edu:
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?


The visitor pattern.

#include <iostream>
#include <vector>

// --- Visitor ABS.

template <typename T> class Parameter;

struct Visitor {
virtual ~Visitor() { }
virtual void visit(Parameter <float>&) = 0;
virtual void visit(Parameter <double>&) = 0;
// ...
};

// --- Parameter base

struct ParameterBase {
virtual ~ParameterBase( ) {};
virtual void applyVisitor(Vi sitor&) = 0;
};

// --- Parameter implementation

template<typena me T>
class Parameter : public ParameterBase {
T data;
public:
Parameter(T const& value) : data(value) { }
virtual void applyVisitor(Vi sitor& v) { v.visit(*this); }
};

// --- Visitor implementation

template <typename OStream>
class Printer : public Visitor {
OStream& out;
public:
Printer(OStream & out) : out(out) { }
virtual void visit(Parameter <float>& p) {
out << 'f' << ':' << p.data;
}
virtual void visit(Parameter <double>& p) {
out << 'd' << ':' << p.data;
}
// ...
};

// ---

int main() {
using namespace std;

vector<Paramete rBase*> v;
v.push_back(new Parameter<float >(1.2f));
v.push_back(new Parameter<doubl e>(0.1));

Printer<std::os tream> p(std::cout);
std::vector<Par ameterBase*>::i terator it = v.begin();
for (; it != v.end(); ++it) {
(*it)->applyVisitor(p );
std::cout << ' ';
}
std::cout << '\n';

return 0;
}

Jul 22 '05 #2
On Fri, 2 Jul 2004 11:03:06 +0000 (UTC), bartek
<sp************ ******@o2.pl> wrote:

This won't compile:
#include <iostream>
#include <vector>

// --- Visitor ABS.

template <typename T> class Parameter;

struct Visitor {
virtual ~Visitor() { }
virtual void visit(Parameter <float>&) = 0;
virtual void visit(Parameter <double>&) = 0;
// ...
};

// --- Parameter base

struct ParameterBase {
virtual ~ParameterBase( ) {};
virtual void applyVisitor(Vi sitor&) = 0;
};

// --- Parameter implementation

template<typen ame T>
class Parameter : public ParameterBase {
T data;
public:
Parameter(T const& value) : data(value) { }
virtual void applyVisitor(Vi sitor& v) { v.visit(*this); }
};

// --- Visitor implementation

template <typename OStream>
class Printer : public Visitor {
OStream& out;
public:
Printer(OStream & out) : out(out) { }
virtual void visit(Parameter <float>& p) {
out << 'f' << ':' << p.data; ^^^^^^
p.data is not accessible to Visitor here because it is a private
member of Parameter.

Either make "data" public, or use a getter function, or make Visitor a
friend of Parameter, and it should compile.
}
virtual void visit(Parameter <double>& p) {
out << 'd' << ':' << p.data;
}
// ...
};

// ---

int main() {
using namespace std;

vector<Paramete rBase*> v;
v.push_back(new Parameter<float >(1.2f));
v.push_back(new Parameter<doubl e>(0.1));

Printer<std::os tream> p(std::cout);
std::vector<Par ameterBase*>::i terator it = v.begin();
for (; it != v.end(); ++it) {
(*it)->applyVisitor(p );
std::cout << ' ';
}
std::cout << '\n';

return 0;
}

Who gets to delete the Parameter* that were created with "new"?

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #3
Bob Hairgrove <wo************ **@to.know> wrote in
news:hg******** *************** *********@4ax.c om:
On Fri, 2 Jul 2004 11:03:06 +0000 (UTC), bartek
<sp************ ******@o2.pl> wrote:

This won't compile:
#include <iostream>
#include <vector>

// --- Visitor ABS.

template <typename T> class Parameter;

struct Visitor {
virtual ~Visitor() { }
virtual void visit(Parameter <float>&) = 0;
virtual void visit(Parameter <double>&) = 0;
// ...
};

// --- Parameter base

struct ParameterBase {
virtual ~ParameterBase( ) {};
virtual void applyVisitor(Vi sitor&) = 0;
};

// --- Parameter implementation

template<type name T>
class Parameter : public ParameterBase {
T data;
public:
Parameter(T const& value) : data(value) { }
virtual void applyVisitor(Vi sitor& v) { v.visit(*this); }
};

// --- Visitor implementation

template <typename OStream>
class Printer : public Visitor {
OStream& out;
public:
Printer(OStream & out) : out(out) { }
virtual void visit(Parameter <float>& p) {
out << 'f' << ':' << p.data; ^^^^^^
p.data is not accessible to Visitor here because it is a private
member of Parameter.

Either make "data" public, or use a getter function, or make Visitor a
friend of Parameter, and it should compile.


You're absolutely right. Thanks for pointing it out.
}
virtual void visit(Parameter <double>& p) {
out << 'd' << ':' << p.data;
}
// ...
};

// ---

int main() {
using namespace std;

vector<Paramete rBase*> v;
v.push_back(new Parameter<float >(1.2f));
v.push_back(new Parameter<doubl e>(0.1));

Printer<std::os tream> p(std::cout);
std::vector<Par ameterBase*>::i terator it = v.begin();
for (; it != v.end(); ++it) {
(*it)->applyVisitor(p );
std::cout << ' ';
}
std::cout << '\n';

return 0;
}

Who gets to delete the Parameter* that were created with "new"?


Whoever writes real code will know what to do, I guess.

Perhaps I should have noted, that this code is just an approximate sample
of how a basic visitor is usually implemented. Not how to write proper
C++ code. I just hope that it wouldn't be really difficult for a person
who knows C++ to find it out.

Cheers.
Jul 22 '05 #4
> 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<doub le>() )
do_something

All comments are welcome.

Daniel

Why you didn't overload the member "do_somethi ng"? In this case you
would have:

for (forall elm in container)
{
elm.do_somethin g (...);
}

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2048
by: Rick | last post by:
Hi, I wrote a few classes recently and am writing a small GC implementation for C++. I will be implementing my own gc_ptr type maintain a list where I'll store pointers to allocated memory. I was wondering if it is possible to identify objects during runtime via RTTI because I don't want to be doing: gc_ptr<int>::doGC(); gc_ptr<double>::doGC();
2
5980
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
1949
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return #name; } #define DEF_RTTI(name) inline const char *Name(){ return #name; } And(for example) class CProva
9
4629
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This doesn't sound too bad. What I'm worried about is whether there is a general performance overhead caused by enabling RTTI, such as enabling exceptions makes a C++ program slower even when there are no exceptions actually used. Are there similar...
2
3780
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram needs of my dll? Does it bloat the actual download size - would my dll be smaller without it? thanks - I'm using rtti in some instances, but I jsut want to know if it's costing me hundreds of K in extra download. -denny-
3
1621
by: Steven T. Hatton | last post by:
I'm trying to work out a design for dynamically determining file types, and for generating new files of a given type. I'm curious to know what others think of my current strategy. Is it "so einfach wie möglich machen, aber nicht einfacher" or "Rube Goldberg"? The first part of this has to do with a technique used in the C++ Standard Library, so I suspect the purist will not have any objection. It's the approach used to build the...
5
3025
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined data types, so I have to implement the RTTI by myself. I need a simple and effective example to help me decide how to design. Can someon help me?
2
2248
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks integers, then this procedure will be more fast, so I create something like this: --------------------------------------------- class A { public:
3
2153
by: n.torrey.pines | last post by:
Hi I work with a nested tree-like data structure template (that happens to be a tuple of vectors of tuples of trees of something, with different tuple elements having different types) I need to define a variety of ELEMENTWISE operations, like a. increment all leaves (unary) b. match two data structures (binary)
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
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...
0
9865
tracyyun
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...
0
8873
agi2029
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...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
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...
1
3965
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.