1

I need to make, for my college homework, an interpreter in C++ for a language based on functions (or commands). The interpreter has got to read an input file, extract the words (strings), generate the commands and execute them. All commands are classes which inherit from a common super-class (Command, for example), which's got a virtual method called execute. For each word read from the input file, a command is created and stored in a vector<Command>.

So, I'm thinking of using a hashtable, whose keys are the names of the commands (strings) and whose values are some kind of objects which allow me to create an specific class (or give me access to the constructor of an specific class), to easily create the classes for each word instead of using a chain of if-else-if's.

By now, I'm planning to create a CommandGenerator class with a virtual method called generate which returns a new Command object. The values of my commands hash table will be objects of theCommandGenerator class. So I derive from it many other subclasses for all commands, which return specific new objects derived from Command.

But, does anything like that already exist? Or is there any more elegant way to do that? Is there any kind of object that can be extracted from a class to represent it?

LuisABOL
  • 2,951
  • 4
  • 25
  • 57

2 Answers2

1

If each command is a subclass of Command, why don't you use a std::vector<Command*> and push pointers to instances of each subclass? Then you can iterate over the vector and call your virtual execute function.

The closest thing you can get about placing classes in a vector is boost::fusion::vector. But can't be filled at runtime, no use on your specific case.


Assuming you can use C++11. If you can define commands as just a execute function, you can do something like:

map<string, function<void()>> cmds = {
    make_pair("print1", [](){
        cout << "1" << end;
    }),
    make_pair("print2", [](){
        cout << "2" << end;
    }),
    make_pair("print3", [](){
        cout << "3" << end;
    })
};

And then put the command on a vector with:

vector<function<void()>> list;
list.push_back(cmds["print1"]);
list.push_back(cmds["print1"]);
list.push_back(cmds["print2"]);

Then just execute with a loop:

for (function<void()>& cmd : list)
    cmd();

This should print 112 to screen. But if you care a lot with speed, do a lot of ifs instead.

Guilherme Bernal
  • 8,183
  • 25
  • 43
  • "If each command is a subclass of Command, why don't you use a std::vector and push pointers to instances of each subclass? Then you can iterate over the vector and call your virtual execute function." Ok. That's what I'm gonna do with the just-created objects: push them to a vector and iterate over the vector, calling `execute`. But my problem has to do with generate these objects according to the names read from the files without using if-else-if for each name. – LuisABOL Dec 15 '12 at 17:26
  • A lot better, man! Do you have a link to any `function<>` reference, because by typing "c++ function<>" on google, I can only find the concept of functions or function declarations in c++... – LuisABOL Dec 15 '12 at 17:59
1

The basic problem you have is: You have the name of the class as a string and want to create a class with that name. This translation you have to do somehow manually, like you mentioned. This has been discussed here several times, like in Instantiating classes by name with factory pattern or in Looking for a better C++ class factory. The only addition I would make here: use good old macros, because they have a stringize-operator. E.g.:

#include <stdio.h>
#define CREATEOBJ(clss,command) if (strcmp (#clss, command)==0) return new clss;
class Base {
public:
  virtual const char *name()=0;
};
class A : public Base {
public:
  const char *name() {return "I am an A";}
};
class B : public Base {
public:
  const char *name() {return "I am an B";}
};
Base *makeInstance (const char *nm) {
  CREATEOBJ(A,nm);
  CREATEOBJ(B,nm);
}
int main () {
  printf ("%s\n", makeInstance ("A")->name());
  printf ("%s\n", makeInstance ("B")->name());
}

of course you can make it nicer by using a hash-table containing the strings and some function-pointer or generator-class pointer, but the idea remains the same: to add a new class, just add one more CREATEOBJ-thingy.

Community
  • 1
  • 1
pbhd
  • 4,384
  • 1
  • 20
  • 26