From 7eff6adfe294038c723c8059a5993a533551f6cc Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 13 Aug 2019 21:32:05 +0300 Subject: Add load_default_options() function template overload that accepts tracing function --- libbutl/default-options.ixx | 16 ++++++++++++++++ libbutl/default-options.mxx | 13 ++++++++++--- libbutl/default-options.txx | 39 +++++++++++++++++++++------------------ tests/default-options/driver.cxx | 8 +++++--- 4 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 libbutl/default-options.ixx diff --git a/libbutl/default-options.ixx b/libbutl/default-options.ixx new file mode 100644 index 0000000..e1a3dcf --- /dev/null +++ b/libbutl/default-options.ixx @@ -0,0 +1,16 @@ +// file : libbutl/default-options.ixx -*- C++ -*- +// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. +{ + template + inline O + merge_default_options (const default_options& def_ops, const O& cmd_ops) + { + return merge_default_options ( + def_ops, + cmd_ops, + [] (const default_options_entry&, const O&) {}); + } +} diff --git a/libbutl/default-options.mxx b/libbutl/default-options.mxx index 01d32c1..d7aa70b 100644 --- a/libbutl/default-options.mxx +++ b/libbutl/default-options.mxx @@ -7,7 +7,7 @@ #endif #ifndef __cpp_lib_modules_ts -#include // move() +#include // move(), forward() #endif // Other includes. @@ -58,6 +58,11 @@ LIBBUTL_MODEXPORT namespace butl // Search for and load (using scanner S and parsing in the U::fail mode for // both options and arguments) the specified list of options files in the // specified directories returning a vector of option class instances (O). + // Pass each default options file path to the specified function prior to + // load (can be used for tracing, etc). The function signature is: + // + // void (const path&, bool remote) + // // Throw std::system_error on the underlying OS error and pass through // exceptions thrown by the options scanner/parser. // @@ -80,11 +85,12 @@ LIBBUTL_MODEXPORT namespace butl // sufficient ground to definititevly conclude that the file is not remote; // to be sure we would need to query the VCS or some such). // - template + template default_options load_default_options (const optional& sys_dir, const optional& home_dir, - const default_options_files&); + const default_options_files&, + F&&); // Merge the default options and the command line options. // @@ -112,4 +118,5 @@ LIBBUTL_MODEXPORT namespace butl merge_default_options (const default_options&, const O&, F&&); } +#include #include diff --git a/libbutl/default-options.txx b/libbutl/default-options.txx index 42dd585..2ed12e4 100644 --- a/libbutl/default-options.txx +++ b/libbutl/default-options.txx @@ -17,14 +17,15 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. // in both the directory itself and its local/ subdirectory are considered // remote (see load_default_options() for details). // - template + template void load_default_options_files (const dir_path& d, bool remote, const small_vector& fs, + F&& fn, default_options& r) { - auto load = [&fs, &r] (const dir_path& d, bool remote) + auto load = [&fs, &fn, &r] (const dir_path& d, bool remote) { using namespace std; @@ -34,6 +35,8 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. if (file_exists (p)) // Follows symlinks. { + fn (p, remote); + S s (p.string ()); // @@ Note that the potentially thrown exceptions (unknown option, @@ -63,11 +66,12 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. // to the resulting list. Return true if the directory is "remote" (i.e., // belongs to a VCS repository). // - template + template bool load_default_options_files (const dir_path& start_dir, const optional& home_dir, const small_vector& fs, + F&& fn, default_options& r) { if (start_dir.root () || (home_dir && start_dir == *home_dir)) @@ -76,22 +80,28 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. bool remote (load_default_options_files (start_dir.directory (), home_dir, fs, + std::forward (fn), r) || git_repository (start_dir)); dir_path d (start_dir / dir_path (".build2")); if (dir_exists (d)) - load_default_options_files (d, remote, fs, r); + load_default_options_files (d, + remote, + fs, + std::forward (fn), + r); return remote; } - template + template default_options load_default_options (const optional& sys_dir, const optional& home_dir, - const default_options_files& ofs) + const default_options_files& ofs, + F&& fn) { default_options r; @@ -103,6 +113,7 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. load_default_options_files (*sys_dir, false /* remote */, ofs.files, + std::forward (fn), r); } @@ -116,6 +127,7 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. load_default_options_files (d, false /* remote */, ofs.files, + std::forward (fn), r); } @@ -126,6 +138,7 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. load_default_options_files (*ofs.start_dir, home_dir, ofs.files, + std::forward (fn), r); } @@ -136,7 +149,7 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. O merge_default_options (const default_options& def_ops, const O& cmd_ops, - F&& f) + F&& fn) { // Optimize for the common case. // @@ -146,21 +159,11 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. O r; for (const default_options_entry& e: def_ops) { - f (e, cmd_ops); + fn (e, cmd_ops); r.merge (e.options); } r.merge (cmd_ops); return r; } - - template - inline O - merge_default_options (const default_options& def_ops, const O& cmd_ops) - { - return merge_default_options ( - def_ops, - cmd_ops, - [] (const default_options_entry&, const O&) {}); - } } diff --git a/tests/default-options/driver.cxx b/tests/default-options/driver.cxx index a2ed43d..535df31 100644 --- a/tests/default-options/driver.cxx +++ b/tests/default-options/driver.cxx @@ -150,9 +150,11 @@ main (int argc, const char* argv[]) // Load and print the default options. // default_options def_ops ( - load_default_options (sys_dir, - home_dir, - fs)); + load_default_options ( + sys_dir, + home_dir, + fs, + [] (const path&, bool) {})); if (print_entries) { -- cgit v1.1