From 1cdb233cedd12069a4aa5d761e20b42e842ec58f Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 5 Aug 2021 11:20:30 +0300 Subject: Convert default_options_start() to template function which accepts iterators range --- libbutl/default-options.cxx | 73 ---------------------------------------- libbutl/default-options.mxx | 23 ++++++++++--- libbutl/default-options.txx | 39 +++++++++++++++++++++ tests/default-options/driver.cxx | 2 +- 4 files changed, 58 insertions(+), 79 deletions(-) delete mode 100644 libbutl/default-options.cxx diff --git a/libbutl/default-options.cxx b/libbutl/default-options.cxx deleted file mode 100644 index 28f6fb7..0000000 --- a/libbutl/default-options.cxx +++ /dev/null @@ -1,73 +0,0 @@ -// file : libbutl/default-options.cxx -*- C++ -*- -// license : MIT; see accompanying LICENSE file - -#ifndef __cpp_modules_ts -#include -#endif - -#include - -#ifndef __cpp_lib_modules_ts -#include -#endif - -// Other includes. - -#ifdef __cpp_modules_ts -module butl.default_options; - -// Only imports additional to interface. -#ifdef __clang__ -#ifdef __cpp_lib_modules_ts -import std.core; -#endif -import butl.path; -import butl.optional; -import butl.small_vector; -#endif - -#endif - -using namespace std; - -namespace butl -{ - optional - default_options_start (const optional& home, - const vector& dirs) - { - if (home) - assert (home->absolute () && home->normalized ()); - - if (dirs.empty ()) - return nullopt; - - // Use the first directory as a start. - // - auto i (dirs.begin ()); - dir_path d (*i); - - // Try to find a common prefix for each subsequent directory. - // - for (++i; i != dirs.end (); ++i) - { - bool p (false); - - for (; - !(d.root () || (home && d == *home)); - d = d.directory ()) - { - if (i->sub (d)) - { - p = true; - break; - } - } - - if (!p) - return nullopt; - } - - return d; - } -} diff --git a/libbutl/default-options.mxx b/libbutl/default-options.mxx index 11f7bb2..2d44333 100644 --- a/libbutl/default-options.mxx +++ b/libbutl/default-options.mxx @@ -152,12 +152,25 @@ LIBBUTL_MODEXPORT namespace butl AS merge_default_arguments (const default_options&, const AS&, F&&); - // Find a common start (parent) directory stopping at home or root - // (excluding). + // Find a common start (parent) directory for directories specified as an + // iterator range, stopping at home or root (excluding). Optionally pass a + // function resolving an iterator into a directory in a way other than just + // dereferencing it. The function signature is: // - LIBBUTL_SYMEXPORT optional - default_options_start (const optional& home_dir, - const std::vector&); + // const dir_path& (I) + // + template + optional + default_options_start (const optional& home, I, I, F&&); + + template + inline optional + default_options_start (const optional& home, I b, I e) + { + return default_options_start (home, + b, e, + [] (I i) -> const dir_path& {return *i;}); + } } #include diff --git a/libbutl/default-options.txx b/libbutl/default-options.txx index eaf4235..dc809ad 100644 --- a/libbutl/default-options.txx +++ b/libbutl/default-options.txx @@ -318,4 +318,43 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. r.insert (r.end (), cmd_args.begin (), cmd_args.end ()); return r; } + + template + optional + default_options_start (const optional& home, I b, I e, F&& f) + { + if (home) + assert (home->absolute () && home->normalized ()); + + if (b == e) + return nullopt; + + // Use the first directory as a start. + // + I i (b); + dir_path d (f (i)); + + // Try to find a common prefix for each subsequent directory. + // + for (++i; i != e; ++i) + { + bool p (false); + + for (; + !(d.root () || (home && d == *home)); + d = d.directory ()) + { + if (f (i).sub (d)) + { + p = true; + break; + } + } + + if (!p) + return nullopt; + } + + return d; + } } diff --git a/tests/default-options/driver.cxx b/tests/default-options/driver.cxx index b3f66bf..009cddb 100644 --- a/tests/default-options/driver.cxx +++ b/tests/default-options/driver.cxx @@ -250,7 +250,7 @@ main (int argc, const char* argv[]) // Deduce a common start directory. // - fs.start = default_options_start (home_dir, dirs); + fs.start = default_options_start (home_dir, dirs.begin (), dirs.end ()); // Load and print the default options. // -- cgit v1.1