aboutsummaryrefslogtreecommitdiff
path: root/build/utility
blob: cd0d8adbf6f7e3aa1de42d49e1479f41757972ab (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
// file      : build/utility -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_UTILITY
#define BUILD_UTILITY

#include <tuple>
#include <limits> // numeric_limits
#include <string>
#include <vector>
#include <utility>
#include <cstring> // strcmp()
#include <cassert>
#include <exception>
#include <unordered_set>
#include <unordered_map>

#include <build/key-set>
#include <build/path>

namespace build
{
  // Empty string and path.
  //
  extern const std::string empty_string;
  extern const path empty_path;

  // Comparators.
  //
  struct compare_c_string
  {
    bool operator() (const char* x, const char* y) const
    {
      return std::strcmp (x, y) < 0;
    }
  };

  struct compare_pointer_target
  {
    template <typename P>
    bool operator() (const P& x, const P& y) const {return *x < *y;}
  };

  // Support for reverse iteration using range-based for-loop:
  //
  // for (... : reverse_iterate (x)) ...
  //
  template <typename T>
  class reverse_range
  {
    T& x_;

  public:
    reverse_range (T& x): x_ (x) {}

    auto begin () const -> decltype (this->x_.rbegin ())
    {
      return x_.rbegin ();
    }

    auto end () const -> decltype (this->x_.rend ())
    {
      return x_.rend ();
    }
  };

  template <typename T>
  inline reverse_range<T>
  reverse_iterate (T& x)
  {
    return reverse_range<T> (x);
  }

  // Call a function if there is an exception.
  //

  // Means we are in the body of a destructor that is being called
  // as part of the exception stack unwindining. Used to compensate
  // for the deficiencies of uncaught_exception() until C++17
  // uncaught_exceptions() becomes available.
  //
  // @@ MT: will have to be TLS.
  //
  extern bool exception_unwinding_dtor;

  template <typename F, typename T>
  struct exception_guard;

  template <typename F, typename... A>
  inline exception_guard<F, std::tuple<A&&...>>
  make_exception_guard (F f, A&&... a)
  {
    return exception_guard<F, std::tuple<A&&...>> (
      std::move (f), std::forward_as_tuple (a...));
  }

  template <typename F, typename... A>
  struct exception_guard<F, std::tuple<A...>>
  {
    typedef std::tuple<A...> T;

    exception_guard (F f, T a): f_ (std::move (f)), a_ (std::move (a)) {}
    ~exception_guard ()
    {
      if (std::uncaught_exception ())
      {
        exception_unwinding_dtor = true;
        call (std::index_sequence_for<A...> ());
        exception_unwinding_dtor = false;
      }
    }

  private:
    template <std::size_t... I>
    void
    call (std::index_sequence<I...>) {f_ (std::get<I> (a_)...);}

    F f_;
    T a_;
  };

  // Pools (@@ perhaps move into a separate header).
  //
  struct string_pool: std::unordered_set<std::string>
  {
    const std::string&
    find (const char* s) {return *emplace (s).first;}
  };

  extern string_pool extension_pool;

  // A pool of strings and, optionally, other accompanying data in which
  // each entry is assigned an individual index (or id) of type I (e.g.,
  // uint8_t, uint16_t, etc., depending on how many entries are expected).
  // Index value 0 is reserved to indicate the no entry condition.
  //
  template <typename I, typename D>
  struct string_table_element
  {
    const I i;
    const D d;
  };

  template <typename I>
  struct string_table_element<I, std::string>
  {
    const I i;
    const std::string d;
  };

  template <typename D>
  struct string_table_traits
  {
    // By default, look for the key() function in D. But you can
    // also specialize this class template.
    //
    static const std::string&
    key (const D& d) {return d.key ();}
  };

  template <>
  struct string_table_traits<std::string>
  {
    static const std::string&
    key (const std::string& d) {return d;}
  };

  template <typename I, typename D = std::string>
  struct string_table
  {
    // Insert new entry unless one already exists.
    //
    I
    insert (const D& d)
    {
      std::size_t i (vec_.size () + 1);

      // Note: move(d) would be tricky since key still points to it.
      //
      auto r (map_.emplace (
                key_type (&traits::key (d)),
                value_type {static_cast<I> (i), d}));

      if (r.second)
      {
        assert (i <= std::numeric_limits<I>::max ());

        r.first->first.p = &traits::key (r.first->second.d); // Update key.
        vec_.push_back (r.first);
      }

      return r.first->second.i;
    }

    // Find existing.
    //
    I
    find (const std::string& k) const
    {
      auto i (map_.find (key_type (&k)));
      return i != map_.end () ? i->second.i : 0;
    }

    // Reverse lookup.
    //
    const D&
    operator[] (I i) const {assert (i > 0); return vec_[i - 1]->second.d;}

    I
    size () const {return static_cast<I> (vec_.size ());}

  private:
    using key_type = set_key<std::string>;
    using value_type = string_table_element<I, D>;
    using map_type = std::unordered_map<key_type, value_type>;
    using traits = string_table_traits<D>;

    map_type map_;
    std::vector<typename map_type::const_iterator> vec_;
  };
}

#endif // BUILD_UTILITY