473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

"Reverse" polymorphism

Hi there,

I am having an interesting C++ problem which I currently am working
around but I don't like this so perhaps somebody has a better idea.

Basically I am parsing (rather: scanning) a specification (Word
document, ugh) and want to generate code from the information I gather,
mostly argument passing stuff.

The types I have to (un)marshal are currently represented by the
following (simplified) class hierarchy:

class Type { ... };
class ArrayType { ... };
class PointerType { ... };

When generating code I run into the problem to generate different code
depending on the class of the associated type object. Basically the
emitter would have to check the type of the parameter like this:

PointerType *ptr = dynamic_cast<ArrayType*>(arg->type());
if (ptr != 0) { ... }

Obviously I don't like this. OTOH the Type classes don't know how to
generate code (I have to generate Java and C code, and maybe a
simplified C code for an embedded system).

For different outputs I have instances of the class CodeGenerator with
a generate method for each object I might have to generate code for:

class CodeGenerator {
public:
void generate(const ArrayType& t);
void generate(const PointerType& t);
...
};

Of course when calling the generate method passing some object
reference with the static type "Type" (which is an abstract class) I
get an error. I could work around this by adding a generate method for
a Type reference which could multiplex into the specialized methods.
But I don't like using explicit RTTI.

So what I did is to add a method with the following signature to the
Type class:

virtual void generate(CodeGenerator &c) = 0;

which is implemented in each subclass like this:

void generate(CodeGenerator &c) { c.generate(*this); }
So to generate code I have to write something like this:

CodeGenerator &cg = ...;
Type &t = ...; // some PointerType
t.generate(cg);

This works but it's a bit too much stuff for this small problem if you
ask me. Also adding the implementation of the generate method for each
type is boring and could lead to errors. This is no big deal in my
small project but I wonder how this problem could be solved more
elegantly.
Thanks for any hints, thoughts or comments

Torsten

Oct 1 '05 #1
2 2805
* Torsten Landschoff:
what I did is to add a method with the following signature to the
Type class:

virtual void generate(CodeGenerator &c) = 0;

which is implemented in each subclass like this:

void generate(CodeGenerator &c) { c.generate(*this); }
This is sound; it's the basic visitor pattern.

So to generate code I have to write something like this:

CodeGenerator &cg = ...;
Type &t = ...; // some PointerType
t.generate(cg);


Add a Type member function

void doAllTheGenerateStuff()
{
CodeGenerator& cg = ...;
generate( cg );
}

If that isn't enough consider the template pattern to insert snippets of
derived class specific actions inside the above code; essentially that just
amounts to using virtual member functions for those actions. And/or pass as
argument an object that implements usage specific actions. There's almost no
problem in computer science that can't be solved by a bit of indirection.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 1 '05 #2

Torsten Landschoff wrote:
Hi there,

I am having an interesting C++ problem which I currently am working
around but I don't like this so perhaps somebody has a better idea.

Basically I am parsing (rather: scanning) a specification (Word
document, ugh) and want to generate code from the information I gather,
mostly argument passing stuff.

The types I have to (un)marshal are currently represented by the
following (simplified) class hierarchy:

class Type { ... };
class ArrayType { ... };
class PointerType { ... };

When generating code I run into the problem to generate different code
depending on the class of the associated type object. Basically the
emitter would have to check the type of the parameter like this:

PointerType *ptr = dynamic_cast<ArrayType*>(arg->type());
if (ptr != 0) { ... }

Obviously I don't like this. OTOH the Type classes don't know how to
generate code (I have to generate Java and C code, and maybe a
simplified C code for an embedded system).

For different outputs I have instances of the class CodeGenerator with
a generate method for each object I might have to generate code for:

class CodeGenerator {
public:
void generate(const ArrayType& t);
void generate(const PointerType& t);
...
};

Of course when calling the generate method passing some object
reference with the static type "Type" (which is an abstract class) I
get an error. I could work around this by adding a generate method for
a Type reference which could multiplex into the specialized methods.
But I don't like using explicit RTTI.

So what I did is to add a method with the following signature to the
Type class:

virtual void generate(CodeGenerator &c) = 0;

which is implemented in each subclass like this:

void generate(CodeGenerator &c) { c.generate(*this); }
So to generate code I have to write something like this:

CodeGenerator &cg = ...;
Type &t = ...; // some PointerType
t.generate(cg);

This works but it's a bit too much stuff for this small problem if you
ask me. Also adding the implementation of the generate method for each
type is boring and could lead to errors. This is no big deal in my
small project but I wonder how this problem could be solved more
elegantly.
Thanks for any hints, thoughts or comments

Torsten


If I'm not mistaken, it sounds like you're trying to clone your derived
types.
If so, consider using a clone smart pointer, which can do this for you
automatically.
Check out the code in the following links:
http://code.axter.com/copy_ptr.h

http://code.axter.com/cow_ptr.h

For usage info, check out the following link:
http://www.codeguru.com/Cpp/Cpp/algo...le.php/c10407/

Oct 2 '05 #3

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

Similar topics

5
by: Amir S. | last post by:
Hi, I'm a newbie to C++ (2 weeks into the course). We were given this assignment to write some code that reads a set of integers (grades) from a file (filename passed by console), outputs them...
14
by: TTroy | last post by:
Hello, can anyone explain why the following function will not work for INT_MIN: /* itoa: convert n to characters in s */ void itoa(int n, char s) { int i, sign; if((sign = n) < 0) /*...
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
4
by: Ollie | last post by:
I have wrriten a reverse proxy for a client of mine, this reverse proxy takes the url takes it attempt to access a *.XXXX and returns the the contents of an aspx web page from another internal...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
3
by: metaperl | last post by:
For this program: def reverse(data): for index in range(len(data)-1, -1, -1): yield data r = reverse("golf") for char in r: print char
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
1
by: Rudolf Bargholz | last post by:
Hi, We have created triggers to log modifications to tables in our application. The triggers work fine, just on one of the tables in our database the triggers fail with the error message...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.