R
rwf_20
I just wanted to throw this up here in case anyone smarter than me has
a suggestion/workaround:
Problem:
I have a classic producer/consumer system which accepts 'commands' from
a socket and 'executes' them. Obviously, each different command (there
are ~20 currently) has its own needed functionality. The dream goal
here would be to remove all knowledge of the nature of the command at
runtime. That is, I don't want ANY switch/cases or if/elses to
determine what command has been received, etc:
1. Accept command from socket.
2. Create generic 'command' object with command data.
3. Call 'execute' on command object.
4. Command-specific code runs.
So, there's two obvious ways that are _close_:
Using static polylmorphism:
template <unsigned long commandCode>
class base_command {
void execute();
.....
};
// command 1
void base_command<1>::execute() {
....command 1 specific code...
}
void base_command<2>::execute() {
....command 2 specific code...
}
int main() {
unsigned long commandCode = //read command in somehow
base_command<commandCode>().execute(); //obviously doesn't work
because the compiler doesn't know 'commandCode' at compile-time
}
Using dynamic polymorphism:
class base_command {
virtual void execute() = 0;
};
class command_1 : public base_command {
virtual void execute() {
...command 1 specific code...
}
};
class command_2 : public base_command {
.... etc...
};
int main() {
unsigned long commandCode = // read command in somehow
base_command* c = // some logic to determine subclass from (huge
switch/case)
}
So, I'd love to use static polymorphism, but I'm fairly certain this
can't and never will happen, due to no compile-time knowledge of the
command code. I'm currently implementing the dynamic method, but I
HATE the huge switch/case needed to determine command type. The
executable size should be the same in all cases, but what I'm really
looking for is a) cleanliness and b) efficiency. I'm on an embedded
system, so using polymorphism makes me sick to my stomach. But, I
guess it's either that or one class with an enormous if/else.
I've got some other crazy half-ideas like a map of constructor function
objects indexed by command code (boost probably has something nutty
that might half-support this). But for the most part I think I'm
stuck.
Unless anyone has any thoughts...
Thanks,
Ryan
a suggestion/workaround:
Problem:
I have a classic producer/consumer system which accepts 'commands' from
a socket and 'executes' them. Obviously, each different command (there
are ~20 currently) has its own needed functionality. The dream goal
here would be to remove all knowledge of the nature of the command at
runtime. That is, I don't want ANY switch/cases or if/elses to
determine what command has been received, etc:
1. Accept command from socket.
2. Create generic 'command' object with command data.
3. Call 'execute' on command object.
4. Command-specific code runs.
So, there's two obvious ways that are _close_:
Using static polylmorphism:
template <unsigned long commandCode>
class base_command {
void execute();
.....
};
// command 1
void base_command<1>::execute() {
....command 1 specific code...
}
void base_command<2>::execute() {
....command 2 specific code...
}
int main() {
unsigned long commandCode = //read command in somehow
base_command<commandCode>().execute(); //obviously doesn't work
because the compiler doesn't know 'commandCode' at compile-time
}
Using dynamic polymorphism:
class base_command {
virtual void execute() = 0;
};
class command_1 : public base_command {
virtual void execute() {
...command 1 specific code...
}
};
class command_2 : public base_command {
.... etc...
};
int main() {
unsigned long commandCode = // read command in somehow
base_command* c = // some logic to determine subclass from (huge
switch/case)
}
So, I'd love to use static polymorphism, but I'm fairly certain this
can't and never will happen, due to no compile-time knowledge of the
command code. I'm currently implementing the dynamic method, but I
HATE the huge switch/case needed to determine command type. The
executable size should be the same in all cases, but what I'm really
looking for is a) cleanliness and b) efficiency. I'm on an embedded
system, so using polymorphism makes me sick to my stomach. But, I
guess it's either that or one class with an enormous if/else.
I've got some other crazy half-ideas like a map of constructor function
objects indexed by command code (boost probably has something nutty
that might half-support this). But for the most part I think I'm
stuck.
Unless anyone has any thoughts...
Thanks,
Ryan