-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not catch and rethrow exception from state handlers
- Loading branch information
Showing
5 changed files
with
96 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * | ||
* * | ||
* This software is distributed under the terms of the * | ||
* GNU Lesser General Public Licence (LGPL) version 3, * | ||
* copied verbatim in the file "LICENSE" * | ||
********************************************************************************/ | ||
|
||
#ifndef FAIR_MQ_TOOLS_EXCEPTIONS_H | ||
#define FAIR_MQ_TOOLS_EXCEPTIONS_H | ||
|
||
#include <functional> | ||
|
||
namespace fair::mq::tools | ||
{ | ||
|
||
/** | ||
* Executes the given callback in the destructor. | ||
* Can be used to execute something in case of an exception when catch is undesirable, e.g.: | ||
* | ||
* { | ||
* // callback will be executed only if f throws an exception | ||
* CallOnDestruction cod([](){ cout << "exception was thrown"; }, true); | ||
* f(); | ||
* cod.disable(); | ||
* } | ||
*/ | ||
|
||
class CallOnDestruction | ||
{ | ||
public: | ||
CallOnDestruction(std::function<void()> c, bool enable = true) | ||
: callback(c) | ||
, enabled(enable) | ||
{} | ||
|
||
~CallOnDestruction() | ||
{ | ||
if (enabled) { | ||
callback(); | ||
} | ||
} | ||
|
||
void enable() { enabled = true; } | ||
void disable() { enabled = false; } | ||
|
||
private: | ||
std::function<void()> callback; | ||
bool enabled; | ||
}; | ||
|
||
} // namespace fair::mq::tools | ||
|
||
#endif /* FAIR_MQ_TOOLS_EXCEPTIONS_H */ |