Passing Exception Types down through a menu
So I've come across this problem and I can't seem to solve it.
Basically I've got a menu of tests, it can be of arbitrary depth, it's just a way of organizing tests and at the lowest level has specific test-cases.
As it stands right now everything seems to work, however I'd like to implement a system where you can specify an exception type and act on whether or not it gets thrown. To complicate things more, due to some macros we're using the catch block takes only the type, not an instance (which I believe a normal catch can, it's been a while).
so here's some code as it is now:
Adding a test:
Menu* mainmenu = new Menu("MainMenu");
Menu* sub1 = mainmenu->add("Sub1", functionptr);
sub1->add("Test1", "Script_to_Drive_Test");
sub1->add("Test2", "Script_to_Drive_Test");
sub1->add("Test3", "Script_to_Drive_Test");
What I'd like is to be able to specify an exception like so:
....
sub1->add<SOME::EXCEPTION1>("Test1", "Script_to_Drive_Test");
sub1->add<SOME::EXCEPTION2>("Test2", "Script_to_Drive_Test");
sub1->add<SOME::EXCEPTION3>("Test3", "Script_to_Drive_Test");
The issue is that there's multiple exceptions to test for, which (I believe) creates different types. Currently everyt开发者_高级运维hing gets stored cleanly in a vector when add is called, however using templates that's no longer possible (even if I were to templatize the vector type as well, it wouldn't work after the first exception is created, since Test2 and Test3 are of a different type, right?). In reality there are a ton of different exceptions to test for.
So the core issue I'm having is being able to get those exception types down into the bowls of my menu. A complete re-write isn't out of the question (it's a relatively small system), however I'd really prefer not to.
Any insight/help or even questions would be greatly appreciated. I'd be more than happy to clarify anything.
I'm not sure if I understand your question well.
However as far as I understood your problem is that now add
creates an object depending on the specified exception type and thous you cannot store it in internal vector
. You might try to use a common base class. Make the vector
store a pointer to non-template base class while each add
will create an object of derived template class (the template depends on the exception type passed to add
).
In general all operations performed previously on the objects stored in vector
will now be virtual functions on the base class implemented in the derived template class.
精彩评论