diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2022-03-10 17:03:16 +0000 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2022-03-10 17:10:15 +0000 |
commit | e5c42bba9bf76d00fb15ec9a179bf91aa81c38c6 (patch) | |
tree | 7597549f36313859d15cb4ae80c04af955c9a223 /src/libexpr/attr-set.hh | |
parent | 1ef88da3503b88f289a15786dfd48821cf5b6502 (diff) | |
parent | 4d98143914120d0163f5c50f30ce8a5289433f8f (diff) |
Merge remote-tracking branch 'upstream/master' into indexed-store-path-outputs
Diffstat (limited to 'src/libexpr/attr-set.hh')
-rw-r--r-- | src/libexpr/attr-set.hh | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/libexpr/attr-set.hh b/src/libexpr/attr-set.hh index 7d6ffc9f3..cad9743ea 100644 --- a/src/libexpr/attr-set.hh +++ b/src/libexpr/attr-set.hh @@ -105,7 +105,7 @@ public: for (size_t n = 0; n < size_; n++) res.emplace_back(&attrs[n]); std::sort(res.begin(), res.end(), [](const Attr * a, const Attr * b) { - return (const string &) a->name < (const string &) b->name; + return (const std::string &) a->name < (const std::string &) b->name; }); return res; } @@ -113,5 +113,52 @@ public: friend class EvalState; }; +/* A wrapper around Bindings that ensures that its always in sorted + order at the end. The only way to consume a BindingsBuilder is to + call finish(), which sorts the bindings. */ +class BindingsBuilder +{ + Bindings * bindings; + +public: + // needed by std::back_inserter + using value_type = Attr; + + EvalState & state; + + BindingsBuilder(EvalState & state, Bindings * bindings) + : bindings(bindings), state(state) + { } + + void insert(Symbol name, Value * value, ptr<Pos> pos = ptr(&noPos)) + { + insert(Attr(name, value, pos)); + } + + void insert(const Attr & attr) + { + push_back(attr); + } + + void push_back(const Attr & attr) + { + bindings->push_back(attr); + } + + Value & alloc(const Symbol & name, ptr<Pos> pos = ptr(&noPos)); + + Value & alloc(std::string_view name, ptr<Pos> pos = ptr(&noPos)); + + Bindings * finish() + { + bindings->sort(); + return bindings; + } + + Bindings * alreadySorted() + { + return bindings; + } +}; } |