aboutsummaryrefslogtreecommitdiff
path: root/build/variable.txx
blob: e661879e6fec3c1aa9ee34ed902f9165c6e211c6 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// file      : build/variable.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <iterator> // make_move_iterator()

#include <build/diagnostics>

namespace build
{
  // vector<T> value
  //
  template <typename T>
  bool
  vector_assign (names& v, const variable& var)
  {
    // Verify each element has valid value of T.
    //
    for (name& n: v)
    {
      if (!assign<T> (n))
        fail << "invalid " << value_traits<T>::value_type.name << " element "
             << "'" << n << "' in variable '" << var.name << "'";
    }

    return !v.empty ();
  }

  template <typename T>
  bool
  vector_append (names& v, names a, const variable& var)
  {
    // Verify that what we are appending is valid.
    //
    vector_assign<T> (a, var);

    if (v.empty ())
      v = move (a);
    else
      v.insert (v.end (),
                std::make_move_iterator (a.begin ()),
                std::make_move_iterator (a.end ()));

    return !v.empty ();
  }

  template <typename T>
  const std::string value_traits<std::vector<T>>::type_name = std::string (
    value_traits<T>::value_type.name) + 's';

  template <typename T>
  const value_type value_traits<std::vector<T>>::value_type
  {
    value_traits<std::vector<T>>::type_name.c_str (),
    &vector_assign<T>,
    &vector_append<T>
  };

  // map<K, V> value
  //
  template <typename K, typename V, typename D>
  map_value<K, V, D>& map_value<K, V, D>::
  assign (std::map<K, V> m)
  {
    d->clear ();
    for (auto& p: m)
    {
      d->emplace_back (p.first); // Const, can't move.
      d->back ().pair = '=';
      d->emplace_back (std::move (p.second));
    }

    return *this;
  }

  template <typename K, typename V, typename D>
  auto map_value<K, V, D>::
  find (const K& k) -> iterator
  {
    // @@ Scan backwards to handle duplicates.
    //
    for (auto i (d->rbegin ()); i != d->rend (); ++i)
      if (as<K> (*++i) == k)
        return iterator (--(i.base ()));

    return end ();
  }

  template <typename K, typename V, typename D>
  auto map_value<K, V, D>::
  find (const K& k) const -> const_iterator
  {
    // @@ Scan backwards to handle duplicates.
    //
    for (auto i (d->rbegin ()); i != d->rend (); ++i)
      if (as<K> (*++i) == k)
        return const_iterator (--(i.base ()));

    return end ();
  }

  template <typename K, typename V>
  bool
  map_assign (names& v, const variable& var)
  {
    // Verify we have a sequence of pairs and each lhs/rhs is a valid
    // value of K/V.
    //
    for (auto i (v.begin ()); i != v.end (); ++i)
    {
      if (i->pair == '\0')
        fail << value_traits<std::map<K, V>>::value_type.name << " key-value "
             << "pair expected instead of '" << *i << "' "
             << "in variable '" << var.name << "'";

      if (!assign<K> (*i))
        fail << "invalid " << value_traits<K>::value_type.name << " key "
             << "'" << *i << "' in variable '" << var.name << "'";

      ++i; // Got to have the second half of the pair.

      if (!assign<V> (*i))
        fail << "invalid " << value_traits<V>::value_type.name << " value "
             << "'" << *i << "' in variable '" << var.name << "'";
    }

    //@@ When doing sorting, note that assign() can convert the
    //   value.

    //@@ Is sorting really the right trade-off (i.e., insertion
    //   vs search)? Perhaps linear search is ok?

    return !v.empty ();
  }

  template <typename K, typename V>
  bool
  map_append (names& v, names a, const variable& var)
  {
    //@@ Not weeding out duplicates.

    // Verify that what we are appending is valid.
    //
    map_assign<K, V> (a, var);

    if (v.empty ())
      v = move (a);
    else
      v.insert (v.end (),
                std::make_move_iterator (a.begin ()),
                std::make_move_iterator (a.end ()));

    return !v.empty ();
  }

  template <typename K, typename V>
  const std::string value_traits<std::map<K, V>>::type_name = std::string (
    value_traits<K>::value_type.name) + '_' +
    value_traits<V>::value_type.name + "_map";

  template <typename K, typename V>
  const value_type value_traits<std::map<K, V>>::value_type
  {
    value_traits<std::map<K, V>>::type_name.c_str (),
    &map_assign<K, V>,
    &map_append<K, V>
  };
}