diff options
Diffstat (limited to 'libbuild2/config/utility.hxx')
-rw-r--r-- | libbuild2/config/utility.hxx | 88 |
1 files changed, 71 insertions, 17 deletions
diff --git a/libbuild2/config/utility.hxx b/libbuild2/config/utility.hxx index cec4bc3..1e2ff53 100644 --- a/libbuild2/config/utility.hxx +++ b/libbuild2/config/utility.hxx @@ -11,6 +11,8 @@ #include <libbuild2/scope.hxx> #include <libbuild2/variable.hxx> +#include <libbuild2/config/types.hxx> + #include <libbuild2/export.hxx> namespace build2 @@ -58,10 +60,20 @@ namespace build2 { // Mark a variable to be saved during configuration. // + // Note: the save_*_omitted flags work best when undefined or (one of) the + // omitted value(s) is the default (see a note in lookup_config() + // documentation for details). + // + // The below lookup_*() functions mark the default value by setting + // value::extra to 1. Note that it's exactly 1 and not "not 0" since other + // values could have other meaning (see, for example, package skeleton + // in bpkg). + // const uint64_t save_default_commented = 0x01; // Based on value::extra. const uint64_t save_null_omitted = 0x02; // Treat NULL as undefined. const uint64_t save_empty_omitted = 0x04; // Treat empty as undefined. - const uint64_t save_base = 0x08; // Custom save with base. + const uint64_t save_false_omitted = 0x08; // Treat false as undefined. + const uint64_t save_base = 0x10; // Custom save with base. inline void save_variable (scope& rs, const variable& var, uint64_t flags = 0) @@ -228,9 +240,11 @@ namespace build2 // // Unlike the rest of the lookup_config() versions, this one leaves the // unspecified value as undefined rather than setting it to a default - // value. This can be useful when we don't have a default value or in case - // we want the mentioning of the variable to be omitted from persistent - // storage (e.g., a config file) if the default value is used. + // value (in this case it also doesn't mark the variable for saving with + // the specified flags). This can be useful when we don't have a default + // value or in case we want the mentioning of the variable to be omitted + // from persistent storage (e.g., a config file) if the default value is + // used. // // Note also that we can first do the lookup without the default value and // then, if there is no value, call the version with the default value and @@ -239,27 +253,38 @@ namespace build2 // expensive. It is also ok to call both versions multiple times provided // the flags are the same. // - // @@ Should we pass flags and interpret save_null_omitted to treat null - // as undefined? Sounds logical. - // lookup - lookup_config (scope& rs, const variable&); + lookup_config (scope& rs, + const variable&, + uint64_t save_flags = 0); lookup - lookup_config (bool& new_value, scope& rs, const variable&); + lookup_config (bool& new_value, + scope& rs, + const variable&, + uint64_t save_flags = 0); // Note that the variable is expected to have already been entered. // inline lookup - lookup_config (scope& rs, const string& var) + lookup_config (scope& rs, + const string& var, + uint64_t save_flags = 0) { - return lookup_config (rs, rs.ctx.var_pool[var]); + // Note: go straight for the public variable pool. + // + return lookup_config (rs, rs.ctx.var_pool[var], save_flags); } inline lookup - lookup_config (bool& new_value, scope& rs, const string& var) + lookup_config (bool& new_value, + scope& rs, + const string& var, + uint64_t save_flags = 0) { - return lookup_config (new_value, rs, rs.ctx.var_pool[var]); + // Note: go straight for the public variable pool. + // + return lookup_config (new_value, rs, rs.ctx.var_pool[var], save_flags); } // Lookup a config.* variable value and, if the value is undefined, set it @@ -287,8 +312,14 @@ namespace build2 // or from the command line (i.e., it is inherited from the amalgamation), // then its value is "overridden" to the default value on this root scope. // - // @@ Should save_null_omitted be interpreted to treat null as undefined? - // Sounds logical. + // Note that while it may seem logical, these functions do not + // "reinterpret" defined values according to the save_*_omitted flags (for + // example, by returning the default value if the defined value is NULL + // and the save_null_omitted flag is specified). This is because such a + // reinterpretation may cause a diversion between the returned value and + // the re-queried config.* variable value if the defined value came from + // an override. To put another way, the save_*_omitted flags are purely to + // reduce the noise in config.build. // template <typename T> lookup @@ -340,6 +371,8 @@ namespace build2 uint64_t save_flags = 0, bool override = false) { + // Note: go straight for the public variable pool. + // return lookup_config (rs, rs.ctx.var_pool[var], std::forward<T> (default_value), // VC14 @@ -356,6 +389,8 @@ namespace build2 uint64_t save_flags = 0, bool override = false) { + // Note: go straight for the public variable pool. + // return lookup_config (new_value, rs, rs.ctx.var_pool[var], @@ -400,7 +435,7 @@ namespace build2 const V* cv ( cast_null<V> ( lookup_config (rs, - rs.var_pool ().insert<V> ("config." + var), + rs.var_pool (true).insert<V> ("config." + var), std::forward<T> (default_value)))); // VC14 value& v (bs.assign<V> (move (var))); @@ -418,7 +453,7 @@ namespace build2 const V* cv ( cast_null<V> ( lookup_config (rs, - rs.var_pool ().insert<V> ("config." + var), + rs.var_pool (true).insert<V> ("config." + var), std::forward<T> (default_value)))); // VC14 value& v (bs.append<V> (move (var))); @@ -481,6 +516,25 @@ namespace build2 // LIBBUILD2_SYMEXPORT bool unconfigured (scope& rs, const string& var, bool value); + + // Return the origin of the value of the specified configuration variable + // plus the value itself. See $config.origin() for details. + // + // Throws invalid_argument if the passed variable is not config.*. + // + LIBBUILD2_SYMEXPORT pair<variable_origin, lookup> + origin (const scope& rs, const string& name); + + LIBBUILD2_SYMEXPORT pair<variable_origin, lookup> + origin (const scope& rs, const variable&); + + // As above but using the result of scope::lookup_original() or + // semantically equivalent (e.g., lookup_namespace()). + // + // Note that this version does not check that the variable is config.*. + // + LIBBUILD2_SYMEXPORT pair<variable_origin, lookup> + origin (const scope& rs, const variable&, pair<lookup, size_t> original); } } |