Imagine that I have a job to do, which can be done in three different ways: a slow and painful, but failsafe way; the way with moderate suffering, given you have Resource1
; and a quick and easy way, which requires both Resource1
and Resource2
. Now, those resources are precious so I wrap them up into RAII-implementing ResNHolder
s and write something like this:
void DoTheJob(Logger& log/*, some other params */) {
try {
Res1Holder r1(/* arguments for creating resource #1 */);
try {
Res2Holder r2(/* arguments */);
DoTheJobQuicklyAndEasily(log, r1, r2);
}
catch (Res2InitializationException& e) {
log.log("Can't obtain resource 2, that'll slowdown us a bit");
DoTheJobWithModerateSuffering(log, r1);
}
}
catch (Res1InitializationException& e) {
log.log("Can't obtain resource 1, using fallback");
DoTheJobTheSlowAndPainfulWay(log);
}
}
"DoTheJobXxx()" take references to Logger
/ResNHolder
, because they are non-copyable. Am I doing it too clumsily? Is there any other clever way to structurize the function?