So many changes. Broken.
[invirt/third/libt4.git] / include / 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 <class A, typename I=void> struct is_tuple_convertible : false_type {};
58
59 template<class A> struct is_tuple_convertible<A,
60     decltype(std::declval<A &>()._tuple_(), void())
61 > : true_type {};
62
63 // string manipulation
64
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 << ">";
68 }
69
70 template <class C>
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();
74     if (i == end)
75         return string();
76     std::ostringstream oss;
77     oss << *i++;
78     while (i != end)
79         oss << delim << *i++;
80     return oss.str();
81 }
82
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();
89     }
90     out.push_back(s.substr(start));
91     return out;
92 }
93
94 template <class A>
95 typename enable_if<
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, ", ") << "]";
100 }
101
102 #include <cstdlib>
103 #define VERIFY(expr) { if (!(expr)) abort(); }
104
105 // struct tuple adapter, useful for marshalling and endian swapping.  usage:
106 //
107 // struct foo {
108 //     int a, b;
109 //     MEMBERS(a, b)
110 // };
111
112 #define MEMBERS(...) \
113 inline auto _tuple_() { return std::tie(__VA_ARGS__); } \
114 inline auto _tuple_() const { return std::tie(__VA_ARGS__); }
115
116 template <class T> inline auto _tuple_(T & t) { return t._tuple_(); }
117
118 // specialized tuple adapter for std::pair
119
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); }
123
124 // struct ordering and comparison operations; requires the use of MEMBERS.
125 // usage:
126 //
127 // LEXICOGRAPHIC_COMPARISON(foo)
128
129 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
130 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
131
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_, !=)
136
137 // Template parameter pack expansion is not allowed in certain contexts, but
138 // brace initializers (for instance, calls to constructors of empty structs)
139 // are fair game.  
140 struct pass { template <typename... Args> inline pass(Args && ...) {} };
141
142 #define UNPACK_STATEMENT(_x_) (void)pass{(_x_)...}
143
144 #include "include/endian.h"
145
146 #ifndef __has_attribute
147 #define __has_attribute(x) 0
148 #endif
149
150 #if __has_attribute(noreturn)
151 #define NORETURN [[noreturn]]
152 #else
153 #define NORETURN
154 #endif
155
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));
159 }
160
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...>{});
164     return m;
165 }
166
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); }
169
170 #endif