All sleep calls via std::this_thread
[invirt/third/libt4.git] / types.h
1 #ifndef types_h
2 #define types_h
3
4 #include <sys/types.h>
5 #include <algorithm>
6 #include <condition_variable>
7 #include <chrono>
8 #include <exception>
9 #include <fstream>
10 #include <functional>
11 #include <iomanip>
12 #include <iostream>
13 #include <limits>
14 #include <list>
15 #include <map>
16 #include <memory>
17 #include <mutex>
18 #include <random>
19 #include <stdexcept>
20 #include <sstream>
21 #include <string>
22 #include <thread>
23 #include <tuple>
24 #include <type_traits>
25 #include <utility>
26 #include <vector>
27
28 using std::string;
29
30 using cond = std::condition_variable;
31 using lock = std::unique_lock<std::mutex>;
32 using std::thread;
33
34 using std::shared_ptr;
35 using std::unique_ptr;
36
37 using std::tuple;
38
39 using std::enable_if;
40 using std::false_type;
41 using std::true_type;
42
43 // type traits and manipulators
44
45 template <class A, typename I=void> struct is_const_iterable : false_type {};
46
47 template<class A> struct is_const_iterable<A,
48     decltype(std::declval<A &>().cbegin(), std::declval<A &>().cend(), void())
49 > : true_type {};
50
51 template <class A, typename I=void> struct supports_emplace_back : false_type {};
52
53 template<class A> struct supports_emplace_back<A,
54     decltype(std::declval<A &>().emplace_back(std::declval<typename A::value_type>()), void())
55 > : true_type {};
56
57 template<typename E> using enum_type_t = typename enable_if<
58     std::is_enum<E>::value, typename std::underlying_type<E>::type>::type;
59
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; }
62
63
64 template <class A, typename I=void> struct is_tuple_convertible : false_type {};
65
66 template<class A> struct is_tuple_convertible<A,
67     decltype(std::declval<A &>()._tuple_(), void())
68 > : true_type {};
69
70 // string manipulation
71
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 << ">";
75 }
76
77 template <class C>
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();
81     if (i == end)
82         return string();
83     std::ostringstream oss;
84     oss << *i++;
85     while (i != end)
86         oss << delim << *i++;
87     return oss.str();
88 }
89
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();
96     }
97     out.push_back(s.substr(start));
98     return out;
99 }
100
101 template <class A>
102 typename enable_if<
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, ", ") << "]";
107 }
108
109 #include "verify.h"
110
111 // struct tuple adapter, useful for marshalling and endian swapping.  usage:
112 //
113 // struct foo {
114 //     int a, b;
115 //     MEMBERS(a, b)
116 // };
117
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__); }
121
122 // struct ordering and comparison operations; requires the use of MEMBERS.
123 // usage:
124 //
125 // LEXICOGRAPHIC_COMPARISON(foo)
126
127 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
128 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
129
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_, !=)
134
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.
138
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;
143 };
144 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
145     typedef tuple_indices<Indices...> type;
146 };
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;
149 };
150
151 #define TUPLE_INDICES(_ArgPack_) typename make_tuple_indices<sizeof...(_ArgPack_)>::type()
152
153 // Template parameter pack expansion is not allowed in certain contexts, but
154 // brace initializers (for instance, calls to constructors of empty structs)
155 // are fair game.  
156 struct pass { template <typename... Args> inline pass(Args && ...) {} };
157
158 #include "endian.h"
159
160 #ifndef __has_attribute
161 #define __has_attribute(x) 0
162 #endif
163
164 #if __has_attribute(noreturn)
165 #define NORETURN [[noreturn]]
166 #else
167 #define NORETURN
168 #endif
169
170 #endif