NSCL DDAS 12.2-009
Support for XIA DDAS at FRIB
CPixieShimGuard.h
Go to the documentation of this file.
1
8#ifndef CPIXIESHIMGUARD_H
9#define CPIXIESHIMGUARD_H
10
11#include <iostream>
12
19constexpr int SHIM_UNEXPECTED_ERROR = -1999;
20
30template <typename T, typename R, typename Fn>
31R shimGuard(T *utils, const char *where, R fallback, Fn fn) {
32 try {
33 utils->SetLastErrorMessage(""); // Clear any previous error message
34 return fn();
35 } catch (const std::exception &e) {
36 utils->SetLastErrorMessage(e.what());
37 std::cerr << where << " unexpected std::exception: " << e.what()
38 << std::endl;
39 return fallback;
40 } catch (...) {
41 utils->SetLastErrorMessage("unknown exception");
42 std::cerr << where << " unknown exception" << std::endl;
43 return fallback;
44 }
45}
46
53template <typename T, typename Fn>
54void shimGuardVoid(T *utils, const char *where, Fn fn) {
55 try {
56 utils->SetLastErrorMessage(""); // Clear any previous error message
57 fn();
58 } catch (const std::exception &e) {
59 utils->SetLastErrorMessage(e.what());
60 std::cerr << where << " unexpected std::exception: " << e.what()
61 << std::endl;
62 } catch (...) {
63 utils->SetLastErrorMessage("unknown exception");
64 std::cerr << where << " unknown exception" << std::endl;
65 }
66}
67
75template <typename Fn>
76auto shimGuardNew(const char *where, Fn fn) -> decltype(fn()) {
77 try {
78 return fn();
79 } catch (const std::exception &e) {
80 std::cerr << where
81 << " construction failed: unhandled std::exception: " << e.what()
82 << std::endl;
83 return nullptr;
84 } catch (...) {
85 std::cerr << where << " construction failed: unknown exception"
86 << std::endl;
87 return nullptr;
88 }
89}
90
93#endif // CPIXIESHIMGUARD_H
R shimGuard(T *utils, const char *where, R fallback, Fn fn)
Run fn() inside the boundary net; return fallback on any exception. T must provide SetLastErrorMessag...
Definition: CPixieShimGuard.h:31
void shimGuardVoid(T *utils, const char *where, Fn fn)
Same net for void shims.
Definition: CPixieShimGuard.h:54
constexpr int SHIM_UNEXPECTED_ERROR
Definition: CPixieShimGuard.h:19
auto shimGuardNew(const char *where, Fn fn) -> decltype(fn())
Net for the constructors: no object exists yet to store a message in, so log and return nullptr (the ...
Definition: CPixieShimGuard.h:76