Camargue
err_util.hpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10 #ifndef CMR_ERRUTIL_HPP
11 #define CMR_ERRUTIL_HPP
12 
13 #include <stdexcept>
14 #include <string>
15 #include <iostream>
16 
25 #define CMR_CATCH_PRINT_THROW( msg, new_ex ) catch(const std::exception &e) \
26  { std::cerr << e.what() << " " << msg << ".\n"; throw new_ex; }
27 
28 
29 namespace CMR {
30 namespace util {
31 
37 template <typename act_type>
38 class ScopeGuard {
39 public:
40 
42  ScopeGuard(act_type action) :
43  final_action(std::move(action)), active(true) {}
44 
45 
47  { if(active) final_action(); }
48 
49  void dismiss()
50  { active = false; }
51 
52  ScopeGuard() = delete;
53  ScopeGuard(const ScopeGuard&) = delete;
54  ScopeGuard& operator=(const ScopeGuard&) = delete;
55 
58  final_action(std::move(rhs.final_action)),
59  active(rhs.active) {rhs.dismiss(); }
60 
61 private:
62  act_type final_action;
63  bool active;
64 };
65 
70 template <typename act_type>
72 {
73  return ScopeGuard<act_type>(std::move(f));
74 }
75 
76 
81 struct retcode_error : public std::runtime_error {
87  retcode_error(const int rval, const std::string &func_name) :
88  std::runtime_error(func_name + " failed with rval " +
89  std::to_string(rval)) {}
90 };
91 
92 }
93 }
94 
95 
96 #endif
retcode_error(const int rval, const std::string &func_name)
Construct a retcode_error with retcode and function name.
Definition: err_util.hpp:87
ScopeGuard(act_type action)
Construct a ScopeGuard to performs action upon going out of scope.
Definition: err_util.hpp:42
ScopeGuard< act_type > make_guard(act_type f)
Type deduction function for ScopeGuard, also from Andrei Alexandrescu.
Definition: err_util.hpp:71
Code from Andrei Alexandrescu&#39;s ScopeGuard11 slides.
Definition: err_util.hpp:38
bool active
True iff final_action should be performed on destruction.
Definition: err_util.hpp:63
ScopeGuard()=delete
No default constructor.
Definition: cliq.hpp:118
Structure for converting retcodes to exceptions.
Definition: err_util.hpp:81
ScopeGuard & operator=(const ScopeGuard &)=delete
No copy assign.
~ScopeGuard()
Perform the final_action.
Definition: err_util.hpp:46
ScopeGuard(ScopeGuard &&rhs)
Move constructor. Moved from ScopeGuard rhs is dismissed.
Definition: err_util.hpp:57
void dismiss()
Indicate that final_action should not happen.
Definition: err_util.hpp:49
The namespace for this project.
Definition: abc_nodesel.hpp:20
act_type final_action
A function to be called on destruction.
Definition: err_util.hpp:62