1 //===-- CleanUp.h -----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_CleanUp_h_ 11 #define liblldb_CleanUp_h_ 12 13 #include "lldb/lldb-public.h" 14 #include <functional> 15 16 namespace lldb_private { 17 18 /// Run a cleanup function on scope exit unless it's explicitly disabled. 19 class CleanUp { 20 std::function<void()> Clean; 21 22 public: 23 /// Register a cleanup function which applies \p Func to a list of arguments. 24 /// Use caution with arguments which are references: they will be copied. 25 template <typename F, typename... Args> CleanUp(F && Func,Args &&...args)26 CleanUp(F &&Func, Args &&... args) 27 : Clean(std::bind(std::forward<F>(Func), std::forward<Args>(args)...)) {} 28 ~CleanUp()29 ~CleanUp() { 30 if (Clean) 31 Clean(); 32 } 33 34 /// Disable the cleanup. disable()35 void disable() { Clean = nullptr; } 36 37 // Prevent cleanups from being run more than once. 38 DISALLOW_COPY_AND_ASSIGN(CleanUp); 39 }; 40 41 } // namespace lldb_private 42 43 #endif // #ifndef liblldb_CleanUp_h_ 44