aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2018-11-26 19:57:20 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-02-11 11:55:35 +0100
commitf216c76c56cdffb5214d074a7d44812843dd174f (patch)
tree976902e380765561e2c241e2e523c3463af14590
parentc02da997570ac0d9b595d787bea8cb5a4e3cc1f5 (diff)
Bindings::get(): Add convenience method
This allows writing attribute lookups as if (auto name = value.attrs->get(state.sName)) ...
-rw-r--r--configure.ac2
-rw-r--r--src/libexpr/attr-set.hh9
2 files changed, 10 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac
index 5a2526672..b550231cb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,7 +62,7 @@ CXXFLAGS=
AC_PROG_CC
AC_PROG_CXX
AC_PROG_CPP
-AX_CXX_COMPILE_STDCXX_14
+AX_CXX_COMPILE_STDCXX_17
# Use 64-bit file system calls so that we can support files > 2 GiB.
diff --git a/src/libexpr/attr-set.hh b/src/libexpr/attr-set.hh
index 3119a1848..6c5fb21ad 100644
--- a/src/libexpr/attr-set.hh
+++ b/src/libexpr/attr-set.hh
@@ -4,6 +4,7 @@
#include "symbol-table.hh"
#include <algorithm>
+#include <optional>
namespace nix {
@@ -63,6 +64,14 @@ public:
return end();
}
+ std::optional<Attr *> get(const Symbol & name)
+ {
+ Attr key(name, 0);
+ iterator i = std::lower_bound(begin(), end(), key);
+ if (i != end() && i->name == name) return &*i;
+ return {};
+ }
+
iterator begin() { return &attrs[0]; }
iterator end() { return &attrs[size_]; }