Clean-ups and fixes to compile with more warnings enabled and with g++.
[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 <stdexcept>
79 using std::runtime_error;
80
81 #include <sstream>
82 using std::ostringstream;
83 using std::istringstream;
84
85 #include <string>
86 using std::string;
87 using std::to_string;
88 using std::stoi;
89
90 #include <thread>
91 using std::thread;
92 using std::call_once;
93 using std::once_flag;
94 namespace this_thread {
95     using namespace std::this_thread;
96 }
97
98 #include <tuple>
99 using std::tuple;
100 using std::get;
101 using std::tie;
102
103 #include <type_traits>
104 using std::decay;
105 using std::true_type;
106 using std::false_type;
107 using std::is_enum;
108 using std::is_member_function_pointer;
109 using std::is_same;
110 using std::underlying_type;
111 using std::enable_if;
112 using std::remove_reference;
113 using std::add_const;
114
115 #include <utility>
116 using std::pair;
117 using std::declval;
118 using std::forward;
119
120 #include <vector>
121 using std::vector;
122
123 // type traits and manipulators
124
125 template <class A, typename I=void> struct is_const_iterable : false_type {};
126
127 template<class A> struct is_const_iterable<A,
128     decltype(declval<A &>().cbegin(), declval<A &>().cend(), void())
129 > : true_type {};
130
131 template <class A, typename I=void> struct supports_emplace_back : false_type {};
132
133 template<class A> struct supports_emplace_back<A,
134     decltype(declval<A &>().emplace_back(declval<typename A::value_type>()), void())
135 > : true_type {};
136
137 template<typename E>
138 using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
139 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
140 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
141
142 // string manipulation
143
144 template <class A, class B>
145 ostream & operator<<(ostream & o, const pair<A,B> & d) {
146     return o << "<" << d.first << "," << d.second << ">";
147 }
148
149 template <class C>
150 inline typename enable_if<is_const_iterable<C>::value, string>::type
151 implode(const C & v, string delim=" ") {
152     auto i=v.cbegin(), end=v.cend();
153     if (i == end)
154         return string();
155     ostringstream oss;
156     oss << *i++;
157     while (i != end)
158         oss << delim << *i++;
159     return oss.str();
160 }
161
162 inline vector<string> explode(const string & s, string delim=" ") {
163     vector<string> out;
164     size_t start = 0, end = 0;
165     while ((end = s.find(delim, start)) != string::npos) {
166         out.push_back(s.substr(start, end - start));
167         start = end + delim.size();
168     }
169     out.push_back(s.substr(start));
170     return out;
171 }
172
173 template <class A>
174 typename enable_if<is_const_iterable<A>::value && !is_same<A,string>::value, ostream>::type &
175 operator<<(ostream & o, const A & a) {
176     return o << "[" << implode(a, ", ") << "]";
177 }
178
179 #include "verify.h"
180 #include "threaded_log.h"
181
182 // struct tuple adapter, useful for marshalling
183 // used like
184 // struct foo {
185 //     int a, b;
186 //     MEMBERS(a, b)
187 // };
188
189 #define MEMBERS(...) \
190 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
191 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
192
193 // struct ordering and comparison
194 // used like
195 // struct foo {
196 //     int a, b;
197 //     MEMBERS(a, b)
198 // };
199 // LEXICOGRAPHIC_COMPARISON(foo)
200
201 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
202 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
203
204 #define LEXICOGRAPHIC_COMPARISON(_c_) \
205 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
206 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
207 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
208
209 // crucial tool for tuple indexing in variadic templates
210 //
211 // This implementation of tuple_indices is redistributed under the MIT
212 // License as an insubstantial portion of the LLVM compiler infrastructure.
213
214 template <size_t...> struct tuple_indices {};
215 template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
216 template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
217     typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
218 };
219 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
220     typedef tuple_indices<Indices...> type;
221 };
222 template <size_t E, size_t S=0> struct make_tuple_indices {
223     typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
224 };
225
226 // Template parameter pack expansion is not allowed in certain contexts, but
227 // brace initializers (for instance, calls to constructors of empty structs)
228 // are fair game.  
229 struct pass { template <typename... Args> inline pass(Args && ...) {} };
230
231 #include "endian.h"
232
233 #ifndef __has_attribute
234 #define __has_attribute(x) 0
235 #endif
236
237 #if __has_attribute(noreturn)
238 #define NORETURN [[noreturn]]
239 #else
240 #define NORETURN
241 #endif
242
243 #endif