// file : bpkg/utility -*- C++ -*- // copyright : Copyright (c) 2014-2016 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file #ifndef BPKG_UTILITY #define BPKG_UTILITY #include // make_shared() #include #include // move() #include // uncaught_exception() #include #include namespace bpkg { using std::move; using std::make_shared; using std::to_string; // To complement bpkg::to_string(). // Y/N prompt. The def argument, if specified, should be either 'y' // or 'no'. It is used as the default answer, in case the user just // hits enter. Issue diagnostics and throw failed if no answer could // be extracted from STDOUT (e.g., because it was closed). // bool yn_prompt (const char* prompt, char def = '\0'); // Filesystem. // bool exists (const path&); bool exists (const dir_path&); bool empty (const dir_path&); void mk (const dir_path&); void mk_p (const dir_path&); void rm (const path&); void rm_r (const dir_path&, bool dir = true); using auto_rm = butl::auto_rmfile; using auto_rm_r = butl::auto_rmdir; // Process. // // The process command line is printed for verbosity >= 2 (essential // command lines). // void run (const char* const args[]); inline void run (const cstrings& args) {run (args.data ());} // Run build2, mapping verbosity levels. If quiet is true, then // run build2 quiet if our verbosity level is 1. // class common_options; void run_b (const common_options&, const string& buildspec, bool quiet = false, const strings& vars1 = strings (), const strings& vars2 = strings ()); // Call a function if there is an exception. // // True means we are in the body of a destructor that is being // called as part of the exception stack unwindining. Used to // compensate for the deficiencies of uncaught_exception() until // C++17 uncaught_exceptions() becomes available. // // @@ MT: will have to be TLS. // extern bool exception_unwinding_dtor; template struct exception_guard; template inline exception_guard make_exception_guard (F f) { return exception_guard (std::move (f)); } template struct exception_guard { exception_guard (F f): f_ (std::move (f)) {} ~exception_guard () { if (std::uncaught_exception ()) { exception_unwinding_dtor = true; f_ (); exception_unwinding_dtor = false; } } private: F f_; }; } #endif // BPKG_UTILITY