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