53f496d75495411b86d4ed2826dacf44784c6732
[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 <cstdlib>
110 #define VERIFY(expr) { if (!(expr)) abort(); }
111
112 // struct tuple adapter, useful for marshalling and endian swapping.  usage:
113 //
114 // struct foo {
115 //     int a, b;
116 //     MEMBERS(a, b)
117 // };
118
119 #define MEMBERS(...) \
120 inline auto _tuple_() { return std::tie(__VA_ARGS__); } \
121 inline auto _tuple_() const { return std::tie(__VA_ARGS__); }
122
123 // struct ordering and comparison operations; requires the use of MEMBERS.
124 // usage:
125 //
126 // LEXICOGRAPHIC_COMPARISON(foo)
127
128 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
129 inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
130
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_, !=)
135
136 // Template parameter pack expansion is not allowed in certain contexts, but
137 // brace initializers (for instance, calls to constructors of empty structs)
138 // are fair game.  
139 struct pass { template <typename... Args> inline pass(Args && ...) {} };
140
141 #include "endian.h"
142
143 #ifndef __has_attribute
144 #define __has_attribute(x) 0
145 #endif
146
147 #if __has_attribute(noreturn)
148 #define NORETURN [[noreturn]]
149 #else
150 #define NORETURN
151 #endif
152
153 #endif