aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/attr-set.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/libexpr/attr-set.hh')
-rw-r--r--src/libexpr/attr-set.hh47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libexpr/attr-set.hh b/src/libexpr/attr-set.hh
index 7d6ffc9f3..3e4899efc 100644
--- a/src/libexpr/attr-set.hh
+++ b/src/libexpr/attr-set.hh
@@ -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;
+ }
+};
}