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