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