From 7253ffee27f6cae34e63a72b2d3d10db10571ecc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 31 Mar 2016 10:59:45 +0200 Subject: Clean up variable lookup interfaces --- build2/variable | 60 ++++++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) (limited to 'build2/variable') diff --git a/build2/variable b/build2/variable index 7b137aa..15f9c47 100644 --- a/build2/variable +++ b/build2/variable @@ -21,7 +21,7 @@ namespace build2 { class value; struct variable; - template struct lookup; + struct lookup; struct value_type { @@ -77,6 +77,8 @@ namespace build2 // // The two variables are considered the same if they have the same name. // + // @@ Document override semantics. + // struct variable { string name; @@ -197,18 +199,14 @@ namespace build2 template T& cast (value&); template T&& cast (value&&); template const T& cast (const value&); - - template T& cast (const lookup&); - template const T& cast (const lookup&); + template const T& cast (const lookup&); // As above but returns NULL if the value is NULL (or not defined, in // case of lookup). // template T* cast_null (value&); template const T* cast_null (const value&); - - template T* cast_null (const lookup&); - template const T* cast_null (const lookup&); + template const T* cast_null (const lookup&); // Assign value type to the value. Variable is normally only used for // diagnostics. @@ -230,10 +228,11 @@ namespace build2 // struct variable_map; - template struct lookup { - V* value; + using value_type = build2::value; + + const value_type* value; const variable_map* vars; bool @@ -243,22 +242,23 @@ namespace build2 // explicit operator bool () const {return defined () && !value->null ();} - V& operator* () const {return *value;} - V* operator-> () const {return value;} + const value_type& operator* () const {return *value;} + const value_type* operator-> () const {return value;} // Return true if this value belongs to the specified scope or target. - // Note that it can also be a target type/pattern-specific value. + // Note that it can also be a target type/pattern-specific value (in + // which case it won't belong to either). // template bool belongs (const T& x) const {return vars == &x.vars;} lookup (): value (nullptr), vars (nullptr) {} - lookup (V* v, const variable_map* vs) + lookup (const value_type* v, const variable_map* vs) : value (v), vars (v != nullptr ? vs : nullptr) {} template - lookup (V& v, const T& x): lookup (&v, &x.vars) {} + lookup (const value_type& v, const T& x): lookup (&v, &x.vars) {} }; // Representation types. @@ -590,32 +590,21 @@ namespace build2 using const_iterator = iterator_adapter; - lookup + // Lookup. Note that variable overrides will not be applied, even if + // set in this map. + // + lookup operator[] (const variable& var) const { - return lookup (find (var), this); + return lookup (find (var), this); } - lookup + lookup operator[] (const string& name) const { return operator[] (var_pool.find (name)); } - // Non-const lookup. Only exposed on the map directly. - // - lookup - operator[] (const variable& var) - { - return lookup (find (var), this); - } - - lookup - operator[] (const string& name) - { - return operator[] (var_pool.find (name)); - } - const value* find (const variable&) const; @@ -675,12 +664,13 @@ namespace build2 // consider its lifetime. // using variable_pattern_map = std::map; + using variable_type_map_base = std::map, + variable_pattern_map>; - struct variable_type_map: std::map, - variable_pattern_map> + struct variable_type_map: variable_type_map_base { - build2::lookup - lookup (const target_type&, const string& name, const variable&) const; + lookup + find (const target_type&, const string& tname, const variable&) const; }; } -- cgit v1.1