12 using std::min_element;
16 #include <condition_variable>
17 using cond = std::condition_variable;
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;
39 // std::bind conflicts with BIND(2)
41 using std::placeholders::_1;
53 using std::setprecision;
59 using std::numeric_limits;
68 using std::enable_shared_from_this;
69 using std::make_shared;
70 using std::shared_ptr;
71 using std::unique_ptr;
76 using lock = std::unique_lock<std::mutex>;
79 using std::runtime_error;
82 using std::ostringstream;
83 using std::istringstream;
94 namespace this_thread {
95 using namespace std::this_thread;
103 #include <type_traits>
105 using std::true_type;
106 using std::false_type;
108 using std::is_member_function_pointer;
110 using std::underlying_type;
111 using std::enable_if;
112 using std::remove_reference;
113 using std::add_const;
123 // type traits and manipulators
125 template <class A, typename I=void> struct is_const_iterable : false_type {};
127 template<class A> struct is_const_iterable<A,
128 decltype(declval<A &>().cbegin(), declval<A &>().cend(), void())
131 template <class A, typename I=void> struct supports_emplace_back : false_type {};
133 template<class A> struct supports_emplace_back<A,
134 decltype(declval<A &>().emplace_back(declval<typename A::value_type>()), void())
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; }
142 // string manipulation
144 template <class A, class B>
145 ostream & operator<<(ostream & o, const pair<A,B> & d) {
146 return o << "<" << d.first << "," << d.second << ">";
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();
158 oss << delim << *i++;
162 inline vector<string> explode(const string & s, string delim=" ") {
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();
169 out.push_back(s.substr(start));
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, ", ") << "]";
180 #include "threaded_log.h"
182 // struct tuple adapter, useful for marshalling
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__); }
193 // struct ordering and comparison
199 // LEXICOGRAPHIC_COMPARISON(foo)
201 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
202 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
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_, !=)
209 // crucial tool for tuple indexing in variadic templates
211 // This implementation of tuple_indices is redistributed under the MIT
212 // License as an insubstantial portion of the LLVM compiler infrastructure.
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;
219 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
220 typedef tuple_indices<Indices...> type;
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;
226 // Template parameter pack expansion is not allowed in certain contexts, but
227 // brace initializers (for instance, calls to constructors of empty structs)
229 struct pass { template <typename... Args> inline pass(Args && ...) {} };
233 #ifndef __has_attribute
234 #define __has_attribute(x) 0
237 #if __has_attribute(noreturn)
238 #define NORETURN [[noreturn]]