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 <class A, typename I=void> struct is_tuple_convertible : false_type {};
59 template<class A> struct is_tuple_convertible<A,
60 decltype(std::declval<A &>()._tuple_(), void())
63 // string manipulation
65 template <class A, class B>
66 std::ostream & operator<<(std::ostream & o, const std::pair<A,B> & d) {
67 return o << "<" << d.first << "," << d.second << ">";
71 inline typename enable_if<is_const_iterable<C>::value, string>::type
72 implode(const C & v, string delim=" ") {
73 auto i=v.cbegin(), end=v.cend();
76 std::ostringstream oss;
83 inline std::vector<string> explode(const string & s, string delim=" ") {
84 std::vector<string> out;
85 size_t start = 0, end = 0;
86 while ((end = s.find(delim, start)) != string::npos) {
87 out.push_back(s.substr(start, end - start));
88 start = end + delim.size();
90 out.push_back(s.substr(start));
96 is_const_iterable<A>::value &&
97 !std::is_same<A,string>::value, std::ostream>::type &
98 operator<<(std::ostream & o, const A & a) {
99 return o << "[" << implode(a, ", ") << "]";
103 #define VERIFY(expr) { if (!(expr)) abort(); }
105 // struct tuple adapter, useful for marshalling and endian swapping. usage:
112 #define MEMBERS(...) \
113 inline auto _tuple_() { return std::tie(__VA_ARGS__); } \
114 inline auto _tuple_() const { return std::tie(__VA_ARGS__); }
116 template <class T> inline auto _tuple_(T & t) { return t._tuple_(); }
118 // specialized tuple adapter for std::pair
120 template <class A, class B> struct is_tuple_convertible<std::pair<A, B>> : true_type {};
121 template <class A, class B> inline auto _tuple_(std::pair<A, B> & t) { return std::tie(t.first, t.second); }
122 template <class A, class B> inline auto _tuple_(const std::pair<A, B> & t) { return std::tie(t.first, t.second); }
124 // struct ordering and comparison operations; requires the use of MEMBERS.
127 // LEXICOGRAPHIC_COMPARISON(foo)
129 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
130 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
132 #define LEXICOGRAPHIC_COMPARISON(_c_) \
133 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
134 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
135 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
137 // Template parameter pack expansion is not allowed in certain contexts, but
138 // brace initializers (for instance, calls to constructors of empty structs)
140 struct pass { template <typename... Args> inline pass(Args && ...) {} };
142 #define UNPACK_STATEMENT(_x_) (void)pass{(_x_)...}
144 #include "include/endian.h"
146 #ifndef __has_attribute
147 #define __has_attribute(x) 0
150 #if __has_attribute(noreturn)
151 #define NORETURN [[noreturn]]
156 template <class... Args, size_t... Indices> inline void
157 tuple_ostream_imp(std::ostream & m, tuple<Args...> & t, std::index_sequence<Indices...>) {
158 UNPACK_STATEMENT(m << std::get<Indices>(t));
161 template <class... Args> inline std::ostream &
162 operator<<(std::ostream & m, tuple<Args...> && t) {
163 tuple_ostream_imp(m, t, std::index_sequence_for<Args...>{});
167 template <class T> inline typename std::enable_if<is_tuple_convertible<T>::value, std::ostream &>::type
168 operator<<(std::ostream & os, const T & t) { return os << _tuple_(t); }