// file : build2/utility -*- C++ -*- // copyright : Copyright (c) 2014-2016 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file #ifndef BUILD2_UTILITY #define BUILD2_UTILITY #include // move(), make_pair() #include // assert() #include // uncaught_exception() #include #include namespace build2 { using std::move; using std::make_pair; // Empty string and path. // extern const std::string empty_string; extern const path empty_path; extern const dir_path empty_dir_path; // Parse version string in the X.Y.Z[-{a|b}N] to a version integer in the // AABBCCDD form, where: // // AA - major version number // BB - minor version number // CC - bugfix version number // DD - alpha / beta (DD + 50) version number // // When DD is not 00, 1 is subtracted from AABBCC. For example: // // Version AABBCCDD // 2.0.0 02000000 // 2.1.0 02010000 // 2.1.1 02010100 // 2.2.0-a1 02019901 // 3.0.0-b2 02999952 // // For a version in the 1.2.3- form, it returns (AABBCC-1)01, which is the // lowest possible version in the 1.2.3 release set. For example: // // Version AABBCCDD // 2.2.0- 02019901 // 1.2.3- 01020201 // // In fact versions 1.2.3- and 1.2.3-a1 are equivalent. // // Throw invalid_argument if the passed string is not a valid version. // unsigned int to_version (const string&); // Call a function if there is an exception. // // 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 (move (f)); } template struct exception_guard { exception_guard (F f): f_ (move (f)) {} ~exception_guard () { if (std::uncaught_exception ()) { exception_unwinding_dtor = true; f_ (); exception_unwinding_dtor = false; } } private: F f_; }; // Pools (@@ perhaps move into a separate header). // struct string_pool: std::unordered_set { const std::string& find (const char* s) {return *emplace (s).first;} const std::string& find (const std::string& s) {return *emplace (s).first;} }; } #endif // BUILD2_UTILITY