vadi said:
I need to design a class for fixing different kind of errors. The
errrors fall into number of categories. For each category the
methodology of fixing the error
is different. I do not want to use a switch...case statement for
handling different category of errors. Can any one suggest me the
method?
Assuming that you assign a status to each error, I think that the
fastestt lookup time for an error is through something like a hashed map.
I don't remember the syntax of the STL like hash_map, but in general it
should be something in the line of: hash_map<type K, type V>. The
constructor probably asks you to provide a hashing function (or it will
give you a default one. Having said this, in order to so this approach,
the only way to execut code after you do a lookup is by having a
function pointer returned, therefore this is complex, troublesome and a
nightmare to maintain.
However, I do beleive that well-implemented, this would yield a superior
performance to switch.
To summarize, you would probably end up writing something that *MIGHT*
end up looking like this:
// I'm sure someone in this newsgroup knows of a good struct to use tat
// encapsulates functors(probably something like
// boost::function::<something>
// This handles errors of one type
struct handle1 : $(some_functor) {
void operator(){
// handle error of sometype
};
};
hash_map<int, $(some_functor)> hashed_map;
// Initialize somewhere
hashed_map.insert(std:
air<int, $(some_functor)>(error1, handle1));
.....
// Then to run
hashed_map[error1]();
I know this will be far from compiling. This is just an idea...
You might also find something with template metaprogramming, but I don't
know much about this.
Regards,
JLR