"Reverse" polymorphism

  • Thread starter Torsten Landschoff
  • Start date
T

Torsten Landschoff

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
 
A

Alf P. Steinbach

* 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

Axter

Torsten said:
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/algorithms/general/article.php/c10407/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top