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, ", ") << "]";
110 #define VERIFY(expr) { if (!(expr)) abort(); }
112 // struct tuple adapter, useful for marshalling and endian swapping. usage:
119 #define MEMBERS(...) \
120 inline auto _tuple_() { return std::tie(__VA_ARGS__); } \
121 inline auto _tuple_() const { return std::tie(__VA_ARGS__); }
123 // struct ordering and comparison operations; requires the use of MEMBERS.
126 // LEXICOGRAPHIC_COMPARISON(foo)
128 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
129 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
131 #define LEXICOGRAPHIC_COMPARISON(_c_) \
132 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
133 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
134 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
136 // Template parameter pack expansion is not allowed in certain contexts, but
137 // brace initializers (for instance, calls to constructors of empty structs)
139 struct pass { template <typename... Args> inline pass(Args && ...) {} };
143 #ifndef __has_attribute
144 #define __has_attribute(x) 0
147 #if __has_attribute(noreturn)
148 #define NORETURN [[noreturn]]