FTXUI  0.8.1
C++ functional terminal UI.
autoreset.hpp
Go to the documentation of this file.
1 #ifndef FTXUI_UTIL_AUTORESET_HPP
2 #define FTXUI_UTIL_AUTORESET_HPP
3 
4 #include <utility>
5 
6 namespace ftxui {
7 
8 /// Assign a value to a variable, reset its old value when going out of scope.
9 template <typename T>
10 class AutoReset {
11  public:
12  AutoReset(T* variable, T new_value)
13  : variable_(variable), previous_value_(std::move(*variable)) {
14  *variable_ = std::move(new_value);
15  }
16  ~AutoReset() { *variable_ = std::move(previous_value_); }
17 
18  private:
19  T* variable_;
20  T previous_value_;
21 };
22 
23 } // namespace ftxui
24 
25 #endif /* end of include guard: FTXUI_UTIL_AUTORESET_HPP */
26 
27 // Copyright 2020 Arthur Sonzogni. All rights reserved.
28 // Use of this source code is governed by the MIT license that can be found in
29 // the LICENSE file.
ftxui
Definition: captured_mouse.hpp:6
ftxui::AutoReset::~AutoReset
~AutoReset()
Definition: autoreset.hpp:16
ftxui::AutoReset::AutoReset
AutoReset(T *variable, T new_value)
Definition: autoreset.hpp:12
ftxui::AutoReset
Assign a value to a variable, reset its old value when going out of scope.
Definition: autoreset.hpp:10