8 #include <condition_variable>
9 using cond = std::condition_variable;
13 using std::chrono::duration_cast;
14 using std::chrono::microseconds;
15 using std::chrono::milliseconds;
16 using std::chrono::nanoseconds;
17 using std::chrono::seconds;
18 using std::chrono::steady_clock;
19 using std::chrono::system_clock;
20 using std::chrono::time_point;
21 using std::chrono::time_point_cast;
35 using std::numeric_limits;
44 using std::enable_shared_from_this;
45 using std::make_shared;
46 using std::shared_ptr;
47 using std::unique_ptr;
52 using lock = std::unique_lock<std::mutex>;
55 using std::runtime_error;
68 namespace this_thread {
69 using namespace std::this_thread;
77 #include <type_traits>
80 using std::false_type;
82 using std::is_member_function_pointer;
84 using std::underlying_type;
86 using std::remove_reference;
97 // type traits and manipulators
99 template <class A, typename I=void> struct is_const_iterable : false_type {};
101 template<class A> struct is_const_iterable<A,
102 decltype(declval<A &>().cbegin(), declval<A &>().cend(), void())
105 template <class A, typename I=void> struct supports_emplace_back : false_type {};
107 template<class A> struct supports_emplace_back<A,
108 decltype(declval<A &>().emplace_back(declval<typename A::value_type>()), void())
112 using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
113 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
114 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
117 template <class A, typename I=void> struct is_tuple_convertible : false_type {};
119 template<class A> struct is_tuple_convertible<A,
120 decltype(declval<A &>()._tuple_(), void())
123 // string manipulation
125 template <class A, class B>
126 std::ostream & operator<<(std::ostream & o, const pair<A,B> & d) {
127 return o << "<" << d.first << "," << d.second << ">";
131 inline typename enable_if<is_const_iterable<C>::value, string>::type
132 implode(const C & v, string delim=" ") {
133 auto i=v.cbegin(), end=v.cend();
136 std::ostringstream oss;
139 oss << delim << *i++;
143 inline vector<string> explode(const string & s, string delim=" ") {
145 size_t start = 0, end = 0;
146 while ((end = s.find(delim, start)) != string::npos) {
147 out.push_back(s.substr(start, end - start));
148 start = end + delim.size();
150 out.push_back(s.substr(start));
155 typename enable_if<is_const_iterable<A>::value && !is_same<A,string>::value, std::ostream>::type &
156 operator<<(std::ostream & o, const A & a) {
157 return o << "[" << implode(a, ", ") << "]";
161 #include "threaded_log.h"
163 // struct tuple adapter, useful for marshalling and endian swapping. usage:
170 #define MEMBERS(...) \
171 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
172 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
174 // struct ordering and comparison operations; requires the use of MEMBERS.
177 // LEXICOGRAPHIC_COMPARISON(foo)
179 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
180 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
182 #define LEXICOGRAPHIC_COMPARISON(_c_) \
183 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
184 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
185 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
187 // crucial tool for tuple indexing in variadic templates
189 // This implementation of tuple_indices is redistributed under the MIT
190 // License as an insubstantial portion of the LLVM compiler infrastructure.
192 template <size_t...> struct tuple_indices {};
193 template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
194 template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
195 typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
197 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
198 typedef tuple_indices<Indices...> type;
200 template <size_t E, size_t S=0> struct make_tuple_indices {
201 typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
204 #define TUPLE_INDICES(_ArgPack_) typename make_tuple_indices<sizeof...(_ArgPack_)>::type()
206 // Template parameter pack expansion is not allowed in certain contexts, but
207 // brace initializers (for instance, calls to constructors of empty structs)
209 struct pass { template <typename... Args> inline pass(Args && ...) {} };
213 #ifndef __has_attribute
214 #define __has_attribute(x) 0
217 #if __has_attribute(noreturn)
218 #define NORETURN [[noreturn]]