From 9c82f6586cde0ac1111a1e96a705fb624e79a564 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 10 Apr 2018 19:57:16 +1000 Subject: [PATCH] FaceRegistry: Support referencing a named face from a parent scope --- src/face_registry.cc | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/face_registry.cc b/src/face_registry.cc index 880ef6e9..5a7a8df6 100644 --- a/src/face_registry.cc +++ b/src/face_registry.cc @@ -98,26 +98,26 @@ void FaceRegistry::add_face(StringView name, StringView facedesc, bool override) if (name == facedesc) throw runtime_error(format("cannot alias face '{}' to itself", name)); - FaceOrAlias& face = m_faces[name]; - auto it = m_faces.find(facedesc); - if (it != m_faces.end()) + for (auto it = m_faces.find(facedesc); + it != m_faces.end() and not it->value.alias.empty(); + it = m_faces.find(it->value.alias)) { - while (it != m_faces.end()) - { - if (it->value.alias.empty()) - break; - if (it->value.alias == name) - throw runtime_error("face cycle detected"); - it = m_faces.find(it->value.alias); - } + if (it->value.alias == name) + throw runtime_error("face cycle detected"); + } - face.alias = facedesc.str(); - } - else + FaceOrAlias& face = m_faces[name]; + + for (auto* registry = this; registry != nullptr; registry = registry->m_parent.get()) { - face.alias = ""; - face.face = parse_face(facedesc); + if (not registry->m_faces.contains(facedesc)) + continue; + face.alias = facedesc.str(); // This is referencing another face + return; } + + face.alias = ""; + face.face = parse_face(facedesc); } void FaceRegistry::remove_face(StringView name)