aboutsummaryrefslogtreecommitdiff
path: root/build/config/utility.txx
diff options
context:
space:
mode:
Diffstat (limited to 'build/config/utility.txx')
-rw-r--r--build/config/utility.txx61
1 files changed, 61 insertions, 0 deletions
diff --git a/build/config/utility.txx b/build/config/utility.txx
new file mode 100644
index 0000000..e37c2b5
--- /dev/null
+++ b/build/config/utility.txx
@@ -0,0 +1,61 @@
+// file : build/config/utility.txx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <utility> // move()
+
+#include <build/scope>
+#include <build/variable>
+
+namespace build
+{
+ namespace config
+ {
+ template <typename T>
+ std::pair<const T&, bool>
+ required (scope& root, const char* name, const T& def_value)
+ {
+ T r;
+ const variable& var (variable_pool.find (name));
+
+ if (auto v = root[var])
+ {
+ const T& s (v.as<const T&> ());
+
+ if (!v.belongs (*global_scope)) // A value from (some) config.build.
+ return std::pair<const T&, bool> (s, false);
+
+ r = s;
+ }
+ else
+ r = def_value;
+
+ auto v (root.assign (var));
+ v = std::move (r);
+
+ return std::pair<const T&, bool> (v.as<const T&> (), true);
+ }
+
+ template <typename T>
+ const T*
+ optional (scope& root, const char* name)
+ {
+ const T* r (nullptr);
+ const variable& var (variable_pool.find (name));
+
+ auto v (root[var]);
+
+ if (v.defined ())
+ {
+ if (v.belongs (*global_scope))
+ root.assign (var) = v;
+
+ r = v.null () ? nullptr : &v.as<const T&> ();
+ }
+ else
+ root.assign (var) = nullptr;
+
+ return r;
+ }
+ }
+}