More logging clean-ups. Static type-checking for RPC calls and
[invirt/third/libt4.git] / types.h
1 #ifndef types_h
2 #define types_h
3
4 #include <sys/types.h>
5
6 #include <algorithm>
7 using std::copy;
8 using std::move;
9 using std::max;
10 using std::min;
11 using std::min_element;
12 using std::find;
13 using std::count_if;
14
15 #include <chrono>
16 using std::chrono::seconds;
17 using std::chrono::milliseconds;
18 using std::chrono::microseconds;
19 using std::chrono::nanoseconds;
20 using std::chrono::steady_clock;
21 using std::chrono::system_clock;
22 using std::chrono::duration_cast;
23 using std::chrono::time_point_cast;
24 using std::chrono::time_point;
25
26 #include <exception>
27 using std::exception;
28
29 #include <fstream>
30 using std::ofstream;
31 using std::ifstream;
32
33 #ifndef LIBT4_NO_FUNCTIONAL
34 #include <functional>
35 using std::function;
36 using std::bind;
37 using std::placeholders::_1;
38 #endif
39
40 #include <iomanip>
41 #include <iostream>
42 using std::cout;
43 using std::cerr;
44 using std::endl;
45 using std::dec;
46 using std::hex;
47 using std::left;
48 using std::setw;
49 using std::setfill;
50 using std::setprecision;
51 using std::ostream;
52 using std::istream;
53 using std::ios;
54
55 #include <limits>
56 using std::numeric_limits;
57
58 #include <list>
59 using std::list;
60
61 #include <map>
62 using std::map;
63
64 #include <memory>
65 using std::enable_shared_from_this;
66 using std::make_shared;
67 using std::shared_ptr;
68 using std::unique_ptr;
69 using std::weak_ptr;
70
71 #include <mutex>
72 using std::mutex;
73 using lock = std::unique_lock<std::mutex>;
74 using cond = std::condition_variable;
75 using std::cv_status;
76
77 #include <sstream>
78 using std::ostringstream;
79 using std::istringstream;
80
81 #include <string>
82 using std::string;
83 using std::to_string;
84 using std::stoi;
85
86 #include <thread>
87 using std::thread;
88 using std::call_once;
89 using std::once_flag;
90 namespace this_thread {
91     using namespace std::this_thread;
92 }
93
94 #include <tuple>
95 using std::tuple;
96 using std::get;
97 using std::tie;
98
99 #include <type_traits>
100 using std::decay;
101 using std::true_type;
102 using std::false_type;
103 using std::is_enum;
104 using std::is_member_function_pointer;
105 using std::is_same;
106 using std::underlying_type;
107 using std::enable_if;
108 using std::remove_reference;
109 using std::add_const;
110
111 #include <utility>
112 using std::pair;
113 using std::declval;
114 using std::forward;
115
116 #include <vector>
117 using std::vector;
118
119 // type traits and manipulators
120
121 template <class A, typename I=void> struct is_const_iterable : false_type {};
122
123 template<class A> struct is_const_iterable<A,
124     decltype(declval<A&>().cbegin(), declval<A&>().cend(), void())
125 > : true_type {};
126
127 template <class A, typename I=void> struct supports_emplace_back : false_type {};
128
129 template<class A> struct supports_emplace_back<A,
130     decltype(declval<A&>().emplace_back(declval<typename A::value_type>()), void())
131 > : true_type {};
132
133 template<typename E>
134 using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
135 template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
136 template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
137
138 // string manipulation
139
140 template <class C>
141 inline typename enable_if<is_const_iterable<C>::value, string>::type
142 implode(const C & v, string delim=" ") {
143     auto i=v.cbegin(), end=v.cend();
144     if (i == end)
145         return string();
146     ostringstream oss;
147     oss << *i++;
148     while (i != end)
149         oss << delim << *i++;
150     return oss.str();
151 }
152
153 inline vector<string> explode(const string &s, string delim=" ") {
154     vector<string> out;
155     size_t start = 0, end = 0;
156     while ((end = s.find(delim, start)) != string::npos) {
157         out.push_back(s.substr(start, end - start));
158         start = end + delim.size();
159     }
160     out.push_back(s.substr(start));
161     return out;
162 }
163
164 #include "lang/verify.h"
165 #include "threaded_log.h"
166
167 // struct tuple adapter, useful for marshalling
168 // used like
169 // struct foo {
170 //     int a, b;
171 //     MEMBERS(a, b)
172 // };
173
174 #define MEMBERS(...) \
175 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
176 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
177
178 // struct ordering and comparison
179 // used like
180 // struct foo {
181 //     int a, b;
182 //     MEMBERS(a, b)
183 // };
184 // LEXICOGRAPHIC_COMPARISON(foo)
185
186 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
187 inline bool operator _op_(const _c_ &b) const { return _tuple_() _op_ b._tuple_(); }
188
189 #define LEXICOGRAPHIC_COMPARISON(_c_) \
190 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
191 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
192 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
193
194 // crucial tool for tuple indexing in variadic templates
195 //
196 // This implementation of tuple_indices is redistributed under the MIT
197 // License as an insubstantial portion of the LLVM compiler infrastructure.
198
199 template <size_t...> struct tuple_indices {};
200 template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
201 template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
202     typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
203 };
204 template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
205     typedef tuple_indices<Indices...> type;
206 };
207 template <size_t E, size_t S=0> struct make_tuple_indices {
208     typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
209 };
210
211 // Template parameter pack expansion is not allowed in certain contexts, but
212 // brace initializers (for instance, calls to constructors of empty structs)
213 // are fair game.  
214 struct pass { template <typename... Args> inline pass(Args&&...) {} };
215
216 #include "endian.h"
217
218 #endif