utility 11.3 KB
Newer Older
Virgile Prevosto's avatar
Virgile Prevosto committed
1
2
3
4
/**************************************************************************/
/*                                                                        */
/*  This file is part of Frama-Clang                                      */
/*                                                                        */
Virgile Prevosto's avatar
Virgile Prevosto committed
5
/*  Copyright (C) 2012-2022                                               */
Virgile Prevosto's avatar
Virgile Prevosto committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*    CEA (Commissariat à l'énergie atomique et aux énergies              */
/*         alternatives)                                                  */
/*                                                                        */
/*  you can redistribute it and/or modify it under the terms of the GNU   */
/*  Lesser General Public License as published by the Free Software       */
/*  Foundation, version 2.1.                                              */
/*                                                                        */
/*  It is distributed in the hope that it will be useful,                 */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/*  GNU Lesser General Public License for more details.                   */
/*                                                                        */
/*  See the GNU Lesser General Public License version 2.1                 */
/*  for more details (enclosed in the file LICENSE).                      */
/*                                                                        */
/**************************************************************************/

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
#ifndef __STD_UTILITY

#define __STD_UTILITY

#include <initializer_list>
#include <type_traits>

namespace std {

  namespace rel_ops {
    template<class T> bool operator!=(const T& x, const T& y) { return !(x == y); }

    template<class T> bool operator> (const T& x, const T& y) { return y < x; }

    template<class T> bool operator<=(const T& x, const T& y) { return !(y<x); }

    template<class T> bool operator>=(const T& x, const T& y) { return !(x<y); }
  }

  template<class T> void swap(T& a, T& b) noexcept {
    T tmp = a;
    a = b;
    b = tmp;
  }

  template <class T, size_t N> void swap(T (&a)[N], T (&b)[N])
    noexcept(noexcept(swap(*a, *b))) {
    for (size_t i = 0; i < N; i++) swap<T>(a[i],b[i]);
  }

  template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept {
    return static_cast<T&&>(t);
  }

  template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept {
58
    return static_cast<T&&>(t);
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
  }

  template <class T> typename remove_reference<T>::type&& move(T&& t) noexcept {
    return static_cast<typename remove_reference<T>::type&&>(t);
  }

  template <class T>
  typename conditional<!is_nothrow_move_constructible<T>::value &&
                       is_copy_constructible<T>::value,
                       const T&, T&&>::type
  move_if_noexcept(T& x) noexcept { return std::move<T&>(x); }

  // declval is defined in type_traits, which is included by this header.

  struct piecewise_construct_t { };
  /*constexpr*/ //TODO avoid making it a const in FC's translation
  piecewise_construct_t piecewise_construct = piecewise_construct_t();

  //forward decl

  template<class ... _Args> struct tuple;

  template <class T1, class T2> struct pair {
    typedef T1 first_type;
    typedef T2 second_type;
    T1 first;
    T2 second;
    pair(const pair&) = default;
    pair(pair&&) = default;

    template<typename _T1 = T1, typename _T2 = T2,
             typename =
             typename
             enable_if<__and<typename is_default_constructible<_T1>::type,
                             typename is_default_constructible<_T2>::type>
                       ::value>::type>
    constexpr pair(): first(), second() { };

    template<typename _T1 = T1, typename _T2 = T2,
             typename =
             typename
             enable_if<
               __and<
                 typename is_copy_constructible<_T1>::type,
                 typename is_copy_constructible<_T2>::type>::value>::type>
    pair(const T1& x, const T2& y): first(x), second(y) { };

    template<class U, class V,
             typename =
             typename
             enable_if<
               __and<
               __and<
               __and<
                 typename is_constructible<T1,U&&>::type,
                 typename is_constructible<T2,V&&>::type>,
                 typename is_convertible<U&&,T1>::type>,
                 typename is_convertible<V&&,T2>::type>::value>::type>
    pair(U&& x, V&& y)
      noexcept(is_nothrow_constructible<T1, U&&>::value &&
               is_nothrow_constructible<T2, V&&>::value):
      first(std::forward<U>(x)), second(std::forward<V>(y)) { };

    template<class U, class V,
             typename =
             typename
             enable_if<
               __and<
                 typename is_constructible<T1,const U&>::type,
                 typename is_constructible<T2,const V&>::type>::value>::type>
    pair(const pair<U, V>& p): first(p.first), second(p.second) { };

    template<class U, class V,
             typename =
             typename
             enable_if<
               __and<
               __and<
               __and<
                 typename is_constructible<T1,U&&>::type,
                 typename is_constructible<T2,V&&>::type>,
                 typename is_convertible<U&&,T1>::type>,
                 typename is_convertible<V&&,T2>::type>::value>::type>
    pair(pair<U, V>&& p)
      noexcept(is_nothrow_constructible<T1, U&&>::value &&
               is_nothrow_constructible<T2, V&&>::value):
      first(forward<U>(p.first)), second(forward<V>(p.second)) { };

    template <class... Args1, class... Args2,
              typename =
              typename
              enable_if<
                __and<
                  typename is_constructible<T1, Args1...>::type,
                  typename is_constructible<T2, Args2...>::type>::value>::type>
    pair(piecewise_construct_t,
         tuple<Args1...> first_args, tuple<Args2...> second_args)
      noexcept(is_nothrow_constructible<T1,Args1...>::value &&
               is_nothrow_constructible<T2,Args2...>::value);
    template<class _T1=T1, class _T2=T2,
             typename =
             typename
             enable_if<
               __and<
                 typename is_copy_assignable<T1>::type,
                 typename is_copy_assignable<T2>::type>::value>::type>
    pair& operator=(const pair& p) {
      first = p.first;
      second = p.second;
      return this;
    };

    template<class U, class V,
             typename=
             typename
             enable_if<
               __and<
                 typename is_assignable<T1&, const U&>::type,
                 typename is_assignable<T2&, const V&>::type>::value>::type>
    pair& operator=(const pair<U, V>& p) {
      first = p.first;
      second = p.second;
      return this;
    };

    template<class _T1=T1, class _T2=T2,
             typename =
             typename
             enable_if<
               __and<
                 typename is_move_assignable<T1>::type,
                 typename is_move_assignable<T2>::type>::value>::type>
    pair& operator=(pair&& p)
      noexcept(is_nothrow_move_assignable<T1>::value &&
               is_nothrow_move_assignable<T2>::value) {
      first = forward<T1>(p.first);
      second = forward<T2>(p.second);
      return *this;
    }

    template<class U, class V,
             typename =
             typename
             enable_if<
               __and<
                 typename is_assignable<T1&,U&&>::type,
                 typename is_assignable<T2&,V&&>::type>::value>::type>
    pair& operator=(pair<U, V>&& p)
      noexcept(is_nothrow_assignable<T1&,U&&>::value &&
               is_nothrow_assignable<T2&,V&&>::value) {
      first = forward<U>(p.first);
      second = forward<V>(p.second);
      return *this;
    }

    void swap(pair& p) noexcept(noexcept(std::swap<T1>(first, p.first)) &&
                                noexcept(std::swap<T2>(second, p.second))) {
      std::swap<T1>(first, p.first);
      std::swap<T2>(second, p.second);
    };

  };

  template <class T1, class T2> bool operator==(
    const pair<T1,T2>& x, const pair<T1,T2>& y) {
    return x.first == y.first && x.second == y.second;
  };

  template <class T1, class T2>  bool operator<(
    const pair<T1,T2>& x, const pair<T1,T2>& y) {
    return
      (x.first < y.first) ||
      (!(y.first < x.first) && x.second < y.second);
  };

  template <class T1, class T2>  bool operator!=(
    const pair<T1,T2>& x, const pair<T1,T2>& y) {
    return !(x==y);
  };

  template <class T1, class T2> bool operator> (
    const pair<T1,T2>& x, const pair<T1,T2>& y) {
    return y < x;
  };

  template <class T1, class T2> bool operator>=(
    const pair<T1,T2>&x, const pair<T1,T2>& y) {
    return !(x<y);
  };

  template <class T1, class T2> bool operator<=(
    const pair<T1,T2>& x, const pair<T1,T2>& y) {
    return !(y < x);
  }

  template <class T1, class T2>  void swap(pair<T1,T2>& x, pair<T1,T2>& y)
    noexcept(noexcept(x.swap(y))) {
    x.swap(y);
  }


  //TODO: result type should be refined in presence of functional objects
  template <class T1, class T2>
  pair<typename remove_reference<T1>::type,
       typename remove_reference<T2>::type> make_pair(T1&& x, T2&& y)
    noexcept(
      is_nothrow_constructible<T1,T1&&>::value &&
      is_nothrow_constructible<T2,T2&&>::value) {
    return pair<typename remove_reference<T1>::type,
                typename remove_reference<T2>::type>(
      std::forward<T1>(x),std::forward<T2>(y));
  }

  template <class T> class tuple_size;
  template <size_t I, class T> class tuple_element;

  template <class T1, class T2> struct tuple_size<std::pair<T1, T2> > {
    static const size_t value = 2;
  };

  template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> > {
    typedef T1 type;
  };

  template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> > {
    typedef T2 type;
  };

  template<size_t I, class T1, class T2> struct _get_helper;

  template<class T1, class T2> struct _get_helper<0,T1,T2>{
    typedef pair<T1, T2> type;
    typedef typename tuple_element<0, std::pair<T1, T2>>::type elt_type;
    static elt_type& get(type &p) noexcept { return p.first; };
    static elt_type&& get(type &&p) noexcept { return p.first; };
    static const elt_type& get(const type &p) noexcept { return p.first; };
  };

  template<class T1, class T2> struct _get_helper<1,T1,T2>{
    typedef pair<T1, T2> type;
    typedef typename tuple_element<1, std::pair<T1, T2>>::type elt_type;
    static elt_type& get(type &p) noexcept { return p.second; };
    static elt_type&& get(type &&p) noexcept { return p.second; };
    static const elt_type& get(const type &p) noexcept { return p.second; };
  };

  template<size_t I, class T1, class T2>
  typename tuple_element<I, std::pair<T1, T2> >::type&
  get(std::pair<T1, T2>& p) noexcept { return _get_helper<I,T1,T2>::get(p); };

  template<size_t I, class T1, class T2>
  typename tuple_element<I, std::pair<T1, T2> >::type&&
  get(std::pair<T1, T2>&& p) noexcept { return _get_helper<I,T1,T2>::get(p); };

  template<size_t I, class T1, class T2>
  const typename tuple_element<I, std::pair<T1, T2> >::type&
  get(const std::pair<T1, T2>& p) noexcept
  { return _get_helper<I,T1,T2>::get(p);};

  // TODO: tuple

}
#endif

/*
  Local Variables:
  mode: C++
  End:
*/