Got rid of most using directives. Ported tests to python.
[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 <stdexcept>
19 #include <sstream>
20 #include <string>
21 #include <thread>
22 #include <tuple>
23 #include <type_traits>
24 #include <utility>
25 #include <vector>
26
27 using std::string;
28
29 using cond = std::condition_variable;
30 using lock = std::unique_lock<std::mutex>;
31 using std::thread;
32
33 using std::shared_ptr;
34 using std::unique_ptr;
35
36 using std::tuple;
37
38 using std::enable_if;
39 using std::false_type;
40 using std::true_type;
41
42 // type traits and manipulators
43
44 template <class A, typename I=void> struct is_const_iterable : false_type {};
45
46 template<class A> struct is_const_iterable<A,
47     decltype(std::declval<A &>().cbegin(), std::declval<A &>().cend(), void())
48 > : true_type {};
49
50 template <class A, typename I=void> struct supports_emplace_back : false_type {};
51
52 template<class A> struct supports_emplace_back<A,
53     decltype(std::declval<A &>().emplace_back(std::declval<typename A::value_type>()), void())
54 > : true_type {};
55
56 template<typename E> using enum_type_t = typename enable_if<
57     std::is_enum<E>::value, typename std::underlying_type<E>::type>::type;
58
59 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
60 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
61
62
63 template <class A, typename I=void> struct is_tuple_convertible : false_type {};
64
65 template<class A> struct is_tuple_convertible<A,
66     decltype(std::declval<A &>()._tuple_(), void())
67 > : true_type {};
68
69 // string manipulation
70
71 template <class A, class B>
72 std::ostream & operator<<(std::ostream & o, const std::pair<A,B> & d) {
73     return o << "<" << d.first << "," << d.second << ">";
74 }
75
76 template <class C>
77 inline typename enable_if<is_const_iterable<C>::value, string>::type
78 implode(const C & v, string delim=" ") {
79     auto i=v.cbegin(), end=v.cend();
80     if (i == end)
81         return string();
82     std::ostringstream oss;
83     oss << *i++;
84     while (i != end)
85         oss << delim << *i++;
86     return oss.str();
87 }
88
89 inline std::vector<string> explode(const string & s, string delim=" ") {
90     std::vector<string> out;
91     size_t start = 0, end = 0;
92     while ((end = s.find(delim, start)) != string::npos) {
93         out.push_back(s.substr(start, end - start));
94         start = end + delim.size();
95     }
96     out.push_back(s.substr(start));
97     return out;
98 }
99
100 template <class A>
101 typename enable_if<
102     is_const_iterable<A>::value &&
103     !std::is_same<A,string>::value, std::ostream>::type &
104 operator<<(std::ostream & o, const A & a) {
105     return o << "[" << implode(a, ", ") << "]";
106 }
107
108 #include "verify.h"
109 #include "threaded_log.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