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