aboutsummaryrefslogtreecommitdiff
path: root/build/variable
blob: 368b2f876fdba95d733267862f06df07af2a6e68 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// file      : build/variable -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_VARIABLE
#define BUILD_VARIABLE

#include <string>
#include <memory>        // unique_ptr
#include <cstddef>       // nullptr_t
#include <utility>       // move(), pair, make_pair()
#include <cassert>
#include <functional>    // hash
#include <typeindex>
#include <unordered_set>

#include <butl/prefix-map>

#include <build/types>

namespace build
{
  struct value;

  struct value_type
  {
    std::type_index id;
    value* (*const factory) ();
  };

  // variable
  //
  // The two variables are considered the same if they have the same name.
  //
  struct variable
  {
    explicit
    variable (std::string n, char p = '\0'): name (std::move (n)), pairs (p) {}

    std::string name;
    char pairs;
    //const value_type* type = nullptr; // If NULL, then no fixed type.
  };

  inline bool
  operator== (const variable& x, const variable& y) {return x.name == y.name;}

  typedef std::reference_wrapper<const variable> variable_cref;

  // value
  //
  struct value;
  typedef std::unique_ptr<value> value_ptr;

  struct value
  {
  public:
    virtual value_ptr
    clone () const = 0;

    virtual bool
    compare (const value&) const = 0;

    virtual
    ~value () = default;
  };

  class list_value: public value, public names
  {
  public:
    using names::names;

    list_value () = default;
    list_value (names d): names (std::move (d)) {}
    list_value (name n) {emplace_back (std::move (n));}
    list_value (dir_path d) {emplace_back (std::move (d));}
    list_value (std::string d) {emplace_back (std::move (d));}

    virtual value_ptr
    clone () const {return value_ptr (new list_value (*this));}

    virtual bool
    compare (const value& v) const
    {
      const list_value* lv (dynamic_cast<const list_value*> (&v));
      return lv != nullptr && static_cast<const names&> (*this) == *lv;
    }

    // Pair (i.e., key-value) search. Note that this funtion assumes
    // the list contains only pairs and keys are simple names. Returns
    // NULL if not found.
    //
    const name*
    find_pair (const std::string& key) const
    {
      for (auto i (begin ()); i != end (); i += 2)
        if (i->value == key)
          return &*++i;
      return nullptr;
    }
  };
  typedef std::unique_ptr<list_value> list_value_ptr;

  // value_proxy
  //
  // A variable can be undefined, null, or contain some actual value.
  // Note that once value_proxy is bound to a value, the only way to
  // rebind it to a different value is by using explicit rebind(). In
  // particular, assigning one value proxy to another will assing the
  // values.
  //
  struct variable_map;

  struct value_proxy
  {
    bool
    defined () const {return p != nullptr;}

    bool
    null () const {return *p == nullptr;}

    bool
    empty () const;

    explicit operator bool () const {return defined () && !null ();}
    explicit operator value_ptr& () const {return *p;}

    // Get interface. See available specializations below.
    //
    template <typename T>
    T
    as () const;

    // Assign.
    //
    const value_proxy&
    operator= (value_ptr) const;

    const value_proxy&
    operator= (const value_proxy&) const;

    const value_proxy&
    operator= (list_value) const;

    const value_proxy&
    operator= (std::string) const;

    const value_proxy&
    operator= (dir_path) const;

    const value_proxy&
    operator= (std::nullptr_t) const;

    // Append.
    //
    const value_proxy&
    operator+= (const value_proxy&) const;

    const value_proxy&
    operator+= (const list_value&) const;

    const value_proxy&
    operator+= (std::string) const; // Append simple name to list_value.

    // Return true if this value belongs to the specified scope or target.
    //
    template <typename T>
    bool
    belongs (const T& x) const {return map == &x.vars;}

    // Implementation details.
    //
    const variable_map* map; // Variable map to which this value belongs.

    value_proxy (): map (nullptr), p (nullptr) {}
    value_proxy (value_ptr* p, const variable_map* m): map (m), p (p) {}

    template <typename T>
    value_proxy (value_ptr& p, const T& x): map (&x.vars), p (&p) {}

    void
    rebind (const value_proxy& x) {map = x.map; p = x.p;}

  private:
    value_ptr* p;
  };

  template <>
  inline value& value_proxy::
  as<value&> () const {return **p;}

  template <>
  inline const value& value_proxy::
  as<const value&> () const {return **p;}

  template <>
  inline list_value& value_proxy::
  as<list_value&> () const
  {
    list_value* lv (dynamic_cast<list_value*> (p->get ()));
    assert (lv != nullptr);
    return *lv;
  }

  template <>
  inline const list_value& value_proxy::
  as<const list_value&> () const {return as<list_value&> ();}

  template <>
  inline const name* value_proxy::
  as<const name*> () const
  {
    const auto& lv (as<const list_value&> ());
    assert (lv.size () < 2);
    return lv.empty () ? nullptr : &lv.front ();
  }

  template <>
  std::string& value_proxy::
  as<std::string&> () const;

  template <>
  const std::string& value_proxy::
  as<const std::string&> () const;

  template <>
  dir_path& value_proxy::
  as<dir_path&> () const;

  template <>
  const dir_path& value_proxy::
  as<const dir_path&> () const;

  template <>
  bool value_proxy::
  as<bool> () const;
}

namespace std
{
  template <>
  struct hash<build::variable>: hash<string>
  {
    size_t
    operator() (const build::variable& v) const noexcept
    {
      return hash<string>::operator() (v.name);
    }
  };
}

namespace butl
{
  template <>
  struct compare_prefix<build::variable_cref>: compare_prefix<std::string>
  {
    typedef compare_prefix<std::string> base;

    explicit
    compare_prefix (char d): base (d) {}

    bool
    operator() (const build::variable& x, const build::variable& y) const
    {
      return base::operator() (x.name, y.name);
    }

    bool
    prefix (const build::variable& p, const build::variable& k) const
    {
      return base::prefix (p.name, k.name);
    }
  };
}

namespace build
{
  // variable_pool
  //
  struct variable_set: std::unordered_set<variable>
  {
    // @@ Need to check/set type?
    //
    const variable&
    find (std::string name) {return *emplace (std::move (name)).first;}

    const variable&
    insert (variable v) {return *emplace (std::move (v)).first;}
  };

  extern variable_set variable_pool;

  // variable_map
  //
  using variable_map_base = butl::prefix_map<variable_cref, value_ptr, '.'>;
  struct variable_map: variable_map_base
  {
    value_proxy
    operator[] (const variable& var) const
    {
      auto i (find (var));
      return i != end ()
        // @@ To do this properly we seem to need ro_value_proxy.
        //
        ? value_proxy (&const_cast<value_ptr&> (i->second), this)
        : value_proxy (nullptr, nullptr);
    }

    value_proxy
    operator[] (const std::string& name) const
    {
      return operator[] (variable_pool.find (name));
    }

    // The second member in the pair indicates whether new (NULL)
    // value was set.
    //
    std::pair<value_proxy, bool>
    assign (const variable& var)
    {
      auto r (emplace (var, value_ptr ()));
      return std::make_pair (value_proxy (&r.first->second, this), r.second);
    }

    std::pair<value_proxy, bool>
    assign (const std::string& name)
    {
      return assign (variable_pool.find (name));
    }

    std::pair<iterator, iterator>
    find_namespace (const std::string& ns)
    {
      return find_prefix (variable_pool.find (ns));
    }

    std::pair<const_iterator, const_iterator>
    find_namespace (const std::string& ns) const
    {
      return find_prefix (variable_pool.find (ns));
    }
  };
}

#include <build/variable.ixx>

#endif // BUILD_VARIABLE