mardi 14 juin 2016

design pattern for nondeterministic operation (nondet. factory?)

Suppose I have this recurring block of code:

Class1 c1;
try {
    c1.parse(x);
} catch(Exception& e) {
    Class2 c2;
    try {
        c2.parse(x);
    } catch(Exception& e) {
        Class3 c3;
        try {
            // ...
        }
        // ...
    }
}

Obviously, what I am interested in, is the first Class# (#=1,2,3,...) instance for which parse does not throw an exception.

Optionally, Class1, ..., ClassN have a common superclass, and I can use that as a return type.

What is a concise way to abstract this operation, considering it will recurr many times in the code?

Ok to use STL and Boost, not ok to use C++11, because of project requirements :( Macros are a gray zone.

Aucun commentaire:

Enregistrer un commentaire