Fixed a race condition!
[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::ios;
52
53 #include <limits>
54 using std::numeric_limits;
55
56 #include <list>
57 using std::list;
58
59 #include <map>
60 using std::map;
61
62 #include <memory>
63 using std::enable_shared_from_this;
64 using std::make_shared;
65 using std::shared_ptr;
66 using std::unique_ptr;
67
68 #include <mutex>
69 using std::mutex;
70 using lock = std::unique_lock<std::mutex>;
71 using cond = std::condition_variable;
72 using std::cv_status;
73
74 #include <sstream>
75 using std::ostringstream;
76 using std::istringstream;
77
78 #include <string>
79 using std::string;
80 using std::to_string;
81 using std::stoi;
82
83 #include <thread>
84 using std::thread;
85 using std::call_once;
86 using std::once_flag;
87 namespace this_thread {
88     using namespace std::this_thread;
89 }
90
91 #include <tuple>
92 using std::tuple;
93 using std::get;
94 using std::tie;
95
96 #include <type_traits>
97 using std::decay;
98 using std::true_type;
99 using std::false_type;
100 using std::is_enum;
101 using std::is_member_function_pointer;
102 using std::is_same;
103 using std::underlying_type;
104 using std::enable_if;
105 using std::remove_reference;
106 using std::add_const;
107
108 #include <utility>
109 using std::pair;
110 using std::declval;
111 using std::forward;
112
113 #include <vector>
114 using std::vector;
115
116 // type traits and manipulators
117
118 template <class A, typename I=void> struct is_const_iterable : false_type {};
119
120 template<class A> struct is_const_iterable<A,
121     decltype(declval<A&>().cbegin(), declval<A&>().cend(), void())
122 > : true_type {};
123
124 template <class A, typename I=void> struct supports_emplace_back : false_type {};
125
126 template<class A> struct supports_emplace_back<A,
127     decltype(declval<A&>().emplace_back(declval<typename A::value_type>()), void())
128 > : true_type {};
129
130 template<typename E>
131 using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
132 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
133 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
134
135 // string manipulation
136
137 template <class C>
138 inline typename enable_if<is_const_iterable<C>::value, string>::type
139 implode(const C & v, string delim=" ") {
140     auto i=v.cbegin(), end=v.cend();
141     if (i == end)
142         return string();
143     ostringstream oss;
144     oss << *i++;
145     while (i != end)
146         oss << delim << *i++;
147     return oss.str();
148 }
149
150 inline vector<string> explode(const string &s, string delim=" ") {
151     vector<string> out;
152     size_t start = 0, end = 0;
153     while ((end = s.find(delim, start)) != string::npos) {
154         out.push_back(s.substr(start, end - start));
155         start = end + delim.size();
156     }
157     out.push_back(s.substr(start));
158     return out;
159 }
160
161 #include "lang/verify.h"
162 #include "threaded_log.h"
163
164 // struct tuple adapter, useful for marshalling
165 // used like
166 // struct foo {
167 //     int a, b;
168 //     MEMBERS(a, b)
169 // };
170
171 #define MEMBERS(...) \
172 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
173 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
174
175 // struct ordering and comparison
176 // used like
177 // struct foo {
178 //     int a, b;
179 //     MEMBERS(a, b)
180 // };
181 // LEXICOGRAPHIC_COMPARISON(foo)
182
183 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
184 inline bool operator _op_(const _c_ &b) const { return _tuple_() _op_ b._tuple_(); }
185
186 #define LEXICOGRAPHIC_COMPARISON(_c_) \
187 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
188 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
189 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
190
191 // crucial tool for tuple indexing in variadic templates
192 //
193 // This implementation of tuple_indices is redistributed under the MIT
194 // License as an insubstantial portion of the LLVM compiler infrastructure.
195
196 template <size_t...> struct tuple_indices {};
197 template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
198 template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
199     typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
200 };
201 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
202     typedef tuple_indices<Indices...> type;
203 };
204 template <size_t E, size_t S=0> struct make_tuple_indices {
205     typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
206 };
207
208 // Template parameter pack expansion is not allowed in certain contexts, but
209 // brace initializers (for instance, calls to constructors of empty structs)
210 // are fair game.  
211 struct pass { template <typename... Args> inline pass(Args&&...) {} };
212
213 #include "endian.h"
214
215 #endif