cdb629ae803f1c030a2a521a8d6fe4e4a95cb45e
[invirt/third/libt4.git] / types.h
1 #ifndef types_h
2 #define types_h
3
4 #include <algorithm>
5 using std::copy;
6 using std::move;
7 using std::max;
8 using std::min;
9 using std::min_element;
10 using std::find;
11 using std::count_if;
12
13 #include <chrono>
14 using std::chrono::seconds;
15 using std::chrono::milliseconds;
16 using std::chrono::microseconds;
17 using std::chrono::nanoseconds;
18 using std::chrono::steady_clock;
19 using std::chrono::system_clock;
20 using std::chrono::duration_cast;
21 using std::chrono::time_point_cast;
22 using std::chrono::time_point;
23
24 #include <exception>
25 using std::exception;
26
27 #include <fstream>
28 using std::ofstream;
29 using std::ifstream;
30
31 #ifndef LIBT4_NO_FUNCTIONAL
32 #include <functional>
33 using std::function;
34 using std::bind;
35 using std::placeholders::_1;
36 #endif
37
38 #include <iomanip>
39 #include <iostream>
40 using std::cout;
41 using std::cerr;
42 using std::endl;
43 using std::dec;
44 using std::hex;
45 using std::left;
46 using std::setw;
47 using std::setfill;
48 using std::setprecision;
49 using std::ostream;
50 using std::istream;
51 using std::ostream_iterator;
52 using std::istream_iterator;
53 using std::ios;
54
55 #include <limits>
56 using std::numeric_limits;
57
58 #include <list>
59 using std::list;
60
61 #include <map>
62 using std::map;
63
64 #include <mutex>
65 using std::mutex;
66 using lock = std::unique_lock<std::mutex>;
67 using cond = std::condition_variable;
68 using std::cv_status;
69
70 #include <sstream>
71 using std::ostringstream;
72 using std::istringstream;
73
74 #include <string>
75 using std::string;
76 using std::to_string;
77 using std::stoi;
78
79 #include <thread>
80 using std::thread;
81 using std::call_once;
82 using std::once_flag;
83 namespace this_thread {
84     using namespace std::this_thread;
85 }
86
87 #include <tuple>
88 using std::tuple;
89 using std::get;
90 using std::tie;
91
92 #include <type_traits>
93 using std::decay;
94 using std::true_type;
95 using std::false_type;
96 using std::is_enum;
97 using std::is_member_function_pointer;
98 using std::is_same;
99 using std::underlying_type;
100 using std::enable_if;
101 using std::remove_reference;
102 using std::add_const;
103
104 #include <utility>
105 using std::pair;
106 using std::declval;
107 using std::forward;
108
109 #include <vector>
110 using std::vector;
111
112 // type traits and manipulators
113
114 template <class A, typename I=void> struct is_const_iterable : false_type {};
115
116 template<class A> struct is_const_iterable<A,
117     decltype(declval<A&>().cbegin(), declval<A&>().cend(), void())
118 > : true_type {};
119
120 template <class A, typename I=void> struct supports_emplace_back : false_type {};
121
122 template<class A> struct supports_emplace_back<A,
123     decltype(declval<A&>().emplace_back(declval<typename A::value_type>()), void())
124 > : true_type {};
125
126 template<typename E>
127 using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
128 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
129 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
130
131 // string manipulation
132
133 template <class C>
134 inline typename enable_if<is_const_iterable<C>::value, string>::type
135 implode(const C & v, string delim=" ") {
136     if (v.begin() == v.end())
137         return string();
138     ostringstream oss;
139     auto last = prev(v.end());
140     copy(v.begin(), last, ostream_iterator<typename C::value_type>(oss, delim.c_str()));
141     oss << *last;
142     return oss.str();
143 }
144
145 inline vector<string> explode(const string &s, string delim=" ") {
146     vector<string> out;
147     size_t start = 0, end = 0;
148     while ((end = s.find(delim, start)) != string::npos) {
149         out.push_back(s.substr(start, end - start));
150         start = end + 1;
151     }
152     out.push_back(s.substr(start));
153     return out;
154 }
155
156 #include "lang/verify.h"
157 #include "threaded_log.h"
158
159 // struct tuple adapter, useful for marshalling
160 // used like
161 // struct foo {
162 //     int a, b;
163 //     MEMBERS(a, b)
164 // };
165
166 #define MEMBERS(...) \
167 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
168 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
169
170 // struct ordering and comparison
171 // used like
172 // struct foo {
173 //     int a, b;
174 //     MEMBERS(a, b)
175 // };
176 // LEXICOGRAPHIC_COMPARISON(foo)
177
178 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
179 inline bool operator _op_(const _c_ &b) const { return _tuple_() _op_ b._tuple_(); }
180
181 #define LEXICOGRAPHIC_COMPARISON(_c_) \
182 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
183 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
184 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
185
186 // crucial tool for tuple indexing in variadic templates
187 //
188 // This implementation of tuple_indices is redistributed under the MIT
189 // License as an insubstantial portion of the LLVM compiler infrastructure.
190
191 template <size_t...> struct tuple_indices {};
192 template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
193 template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
194     typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
195 };
196 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
197     typedef tuple_indices<Indices...> type;
198 };
199 template <size_t E, size_t S=0> struct make_tuple_indices {
200     typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
201 };
202
203 // Template parameter pack expansion is not allowed in certain contexts, but
204 // brace initializers (for instance, calls to constructors of empty structs)
205 // are fair game.  
206 struct pass { template <typename... Args> inline pass(Args&&...) {} };
207
208 #include "endian.h"
209
210 #endif