6 #include <condition_variable>
23 #include <type_traits>
29 using cond = std::condition_variable;
30 using lock = std::unique_lock<std::mutex>;
33 using std::shared_ptr;
34 using std::unique_ptr;
39 using std::false_type;
42 // type traits and manipulators
44 template <class A, typename I=void> struct is_const_iterable : false_type {};
46 template<class A> struct is_const_iterable<A,
47 decltype(std::declval<A &>().cbegin(), std::declval<A &>().cend(), void())
50 template <class A, typename I=void> struct supports_emplace_back : false_type {};
52 template<class A> struct supports_emplace_back<A,
53 decltype(std::declval<A &>().emplace_back(std::declval<typename A::value_type>()), void())
56 template<typename E> using enum_type_t = typename enable_if<
57 std::is_enum<E>::value, typename std::underlying_type<E>::type>::type;
59 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
60 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
63 template <class A, typename I=void> struct is_tuple_convertible : false_type {};
65 template<class A> struct is_tuple_convertible<A,
66 decltype(std::declval<A &>()._tuple_(), void())
69 // string manipulation
71 template <class A, class B>
72 std::ostream & operator<<(std::ostream & o, const std::pair<A,B> & d) {
73 return o << "<" << d.first << "," << d.second << ">";
77 inline typename enable_if<is_const_iterable<C>::value, string>::type
78 implode(const C & v, string delim=" ") {
79 auto i=v.cbegin(), end=v.cend();
82 std::ostringstream oss;
89 inline std::vector<string> explode(const string & s, string delim=" ") {
90 std::vector<string> out;
91 size_t start = 0, end = 0;
92 while ((end = s.find(delim, start)) != string::npos) {
93 out.push_back(s.substr(start, end - start));
94 start = end + delim.size();
96 out.push_back(s.substr(start));
102 is_const_iterable<A>::value &&
103 !std::is_same<A,string>::value, std::ostream>::type &
104 operator<<(std::ostream & o, const A & a) {
105 return o << "[" << implode(a, ", ") << "]";
109 #include "threaded_log.h"
111 // struct tuple adapter, useful for marshalling and endian swapping. usage:
118 #define MEMBERS(...) \
119 inline auto _tuple_() -> decltype(std::tie(__VA_ARGS__)) { return std::tie(__VA_ARGS__); } \
120 inline auto _tuple_() const -> decltype(std::tie(__VA_ARGS__)) { return std::tie(__VA_ARGS__); }
122 // struct ordering and comparison operations; requires the use of MEMBERS.
125 // LEXICOGRAPHIC_COMPARISON(foo)
127 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
128 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
130 #define LEXICOGRAPHIC_COMPARISON(_c_) \
131 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
132 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
133 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
135 // Tuple indexing in variadic templates.
136 // This implementation of tuple_indices is redistributed under the MIT
137 // License as an insubstantial portion of the LLVM compiler infrastructure.
139 template <size_t...> struct tuple_indices {};
140 template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
141 template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
142 typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
144 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
145 typedef tuple_indices<Indices...> type;
147 template <size_t E, size_t S=0> struct make_tuple_indices {
148 typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
151 #define TUPLE_INDICES(_ArgPack_) typename make_tuple_indices<sizeof...(_ArgPack_)>::type()
153 // Template parameter pack expansion is not allowed in certain contexts, but
154 // brace initializers (for instance, calls to constructors of empty structs)
156 struct pass { template <typename... Args> inline pass(Args && ...) {} };
160 #ifndef __has_attribute
161 #define __has_attribute(x) 0
164 #if __has_attribute(noreturn)
165 #define NORETURN [[noreturn]]