aboutsummaryrefslogtreecommitdiff
path: root/build/config/utility.txx
blob: cffdecf11a496352449cddbbbb9f1eb61aa6ce8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// file      : build/config/utility.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// 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;
    }

    template <typename T>
    void
    append_options (cstrings& args, T& s, const char* var)
    {
      if (auto val = s[var])
        append_options (args, val.template as<const list_value&> (), var);
    }

    template <typename T>
    bool
    find_option (const char* option, T& s, const char* var)
    {
      if (auto val = s[var])
      {
        for (const name& n: val.template as<const list_value&> ())
        {
          if (n.simple () && n.value == option)
            return true;
        }
      }

      return false;
    }
  }
}