6 #include <condition_variable>
24 #include <type_traits>
30 using cond = std::condition_variable;
31 using lock = std::unique_lock<std::mutex>;
34 using std::shared_ptr;
35 using std::unique_ptr;
40 using std::false_type;
43 // type traits and manipulators
45 template <class A, typename I=void> struct is_const_iterable : false_type {};
47 template<class A> struct is_const_iterable<A,
48 decltype(std::declval<A &>().cbegin(), std::declval<A &>().cend(), void())
51 template <class A, typename I=void> struct supports_emplace_back : false_type {};
53 template<class A> struct supports_emplace_back<A,
54 decltype(std::declval<A &>().emplace_back(std::declval<typename A::value_type>()), void())
57 template<typename E> using enum_type_t = typename enable_if<
58 std::is_enum<E>::value, typename std::underlying_type<E>::type>::type;
60 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
61 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
64 template <class A, typename I=void> struct is_tuple_convertible : false_type {};
66 template<class A> struct is_tuple_convertible<A,
67 decltype(std::declval<A &>()._tuple_(), void())
70 // string manipulation
72 template <class A, class B>
73 std::ostream & operator<<(std::ostream & o, const std::pair<A,B> & d) {
74 return o << "<" << d.first << "," << d.second << ">";
78 inline typename enable_if<is_const_iterable<C>::value, string>::type
79 implode(const C & v, string delim=" ") {
80 auto i=v.cbegin(), end=v.cend();
83 std::ostringstream oss;
90 inline std::vector<string> explode(const string & s, string delim=" ") {
91 std::vector<string> out;
92 size_t start = 0, end = 0;
93 while ((end = s.find(delim, start)) != string::npos) {
94 out.push_back(s.substr(start, end - start));
95 start = end + delim.size();
97 out.push_back(s.substr(start));
103 is_const_iterable<A>::value &&
104 !std::is_same<A,string>::value, std::ostream>::type &
105 operator<<(std::ostream & o, const A & a) {
106 return o << "[" << implode(a, ", ") << "]";
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]]