aboutsummaryrefslogtreecommitdiff
path: root/build/config
diff options
context:
space:
mode:
Diffstat (limited to 'build/config')
-rw-r--r--build/config/utility45
-rw-r--r--build/config/utility.cxx41
-rw-r--r--build/config/utility.txx61
3 files changed, 147 insertions, 0 deletions
diff --git a/build/config/utility b/build/config/utility
new file mode 100644
index 0000000..2973516
--- /dev/null
+++ b/build/config/utility
@@ -0,0 +1,45 @@
+// file : build/config/utility -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#ifndef BUILD_CONFIG_UTILITY
+#define BUILD_CONFIG_UTILITY
+
+#include <string>
+#include <utility> // pair
+
+namespace build
+{
+ class scope;
+ class list_value;
+
+ namespace config
+ {
+ // Set, if necessary, a required config.* variable.
+ //
+ // Return the reference to the value as well as the indication of
+ // whether the variable has actually been set.
+ //
+ template <typename T>
+ std::pair<const T&, bool>
+ required (scope& root, const char* name, const T& default_value);
+
+ std::pair<const std::string&, bool>
+ required (scope& root, const char* name, const char* default_value);
+
+ // Set, if necessary, an optional config.* variable. In particular,
+ // an unspecified variable is set to NULL which is used to to
+ // distinguish between the "configured as unspecified" and "not
+ // yet configured" cases.
+ //
+ // Return the pointer to the value, which can be NULL.
+ //
+ template <typename T>
+ const T*
+ optional (scope& root, const char* name);
+ }
+}
+
+#include <build/config/utility.txx>
+
+#endif // BUILD_CONFIG_UTILITY
diff --git a/build/config/utility.cxx b/build/config/utility.cxx
new file mode 100644
index 0000000..8b26239
--- /dev/null
+++ b/build/config/utility.cxx
@@ -0,0 +1,41 @@
+// file : build/config/utility.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <build/config/utility>
+
+using namespace std;
+
+namespace build
+{
+ namespace config
+ {
+ // The same as the template except it is a bit more efficient
+ // when it comes to not creating the default value string
+ // unnecessarily.
+ //
+ pair<const string&, bool>
+ required (scope& root, const char* name, const char* def_value)
+ {
+ string r;
+ const variable& var (variable_pool.find (name));
+
+ if (auto v = root[var])
+ {
+ const string& s (v.as<const string&> ());
+
+ if (!v.belongs (*global_scope)) // A value from (some) config.build.
+ return pair<const string&, bool> (s, false);
+
+ r = s;
+ }
+ else
+ r = def_value;
+
+ auto v (root.assign (var));
+ v = move (r);
+
+ return pair<const string&, bool> (v.as<const string&> (), true);
+ }
+ }
+}
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;
+ }
+ }
+}