Virtual Testbed
Ship dynamics simulator for extreme conditions
ios_guard.hh
1 #ifndef VTESTBED_BASE_IOS_GUARD_HH
2 #define VTESTBED_BASE_IOS_GUARD_HH
3 
4 #include <ios>
5 
6 namespace vtb {
7 
8  namespace base {
9 
10  class ios_guard {
11 
12  private:
13  std::ios& _ios;
14  std::ios::iostate _oldstate;
15 
16  public:
17 
18  inline explicit
19  ios_guard(std::ios& ios): _ios(ios) { this->save(); }
20  ~ios_guard() noexcept { this->restore(); }
21 
22  inline void
23  save() noexcept {
24  this->_oldstate = this->_ios.exceptions();
25  }
26 
27  inline void
28  restore() noexcept {
29  try { this->_ios.exceptions(this->_oldstate); } catch (...) { }
30  }
31 
32  ios_guard(const ios_guard&) = delete;
33  ios_guard& operator=(const ios_guard&) = delete;
34  ios_guard(ios_guard&&) = delete;
35  ios_guard& operator=(ios_guard&&) = delete;
36 
37  };
38 
39  }
40 
41 }
42 
43 #endif // vim:filetype=cpp
Main namespace.
Definition: convert.hh:9