X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5a5c578e2e358a121cdb9234a6cb11c4ecfbf323..5224670fe4e903bd507eabd486f8723353893bfa:/types.h diff --git a/types.h b/types.h index 9739a2a..797ccf0 100644 --- a/types.h +++ b/types.h @@ -1,113 +1,171 @@ #ifndef types_h #define types_h +#include #include -using std::copy; -using std::move; -using std::max; -using std::min; -using std::min_element; -using std::find; -using std::count_if; - +#include #include -using std::chrono::seconds; -using std::chrono::milliseconds; -using std::chrono::microseconds; -using std::chrono::nanoseconds; -using std::chrono::steady_clock; -using std::chrono::system_clock; -using std::chrono::duration_cast; -using std::chrono::time_point_cast; -using std::chrono::time_point; - #include -using std::exception; - #include -using std::ofstream; -using std::ifstream; - -#ifndef LIBT4_NO_FUNCTIONAL #include -using std::function; -using std::bind; -using std::placeholders::_1; -#endif - #include #include -using std::cout; -using std::cerr; -using std::endl; -using std::dec; -using std::hex; -using std::left; -using std::setw; -using std::setfill; -using std::setprecision; -using std::ostream; -using std::istream; -using std::ostream_iterator; -using std::istream_iterator; - #include -using std::numeric_limits; - #include -using std::list; - #include -using std::map; - +#include #include -using std::mutex; -using lock = std::unique_lock; -using cond = std::condition_variable; -using std::cv_status; - +#include +#include #include -using std::ostringstream; -using std::istringstream; - #include +#include +#include +#include +#include +#include + using std::string; -using std::to_string; -using std::stoi; -#include +using cond = std::condition_variable; +using lock = std::unique_lock; using std::thread; -#include +using std::shared_ptr; +using std::unique_ptr; + using std::tuple; -using std::get; -using std::tie; -#include -using std::decay; -using std::true_type; -using std::false_type; -using std::is_enum; -using std::is_member_function_pointer; -using std::is_same; -using std::underlying_type; using std::enable_if; +using std::false_type; +using std::true_type; -#include -using std::pair; -using std::declval; +// type traits and manipulators -#include -using std::vector; +template struct is_const_iterable : false_type {}; +template struct is_const_iterable().cbegin(), std::declval().cend(), void()) +> : true_type {}; + +template struct supports_emplace_back : false_type {}; + +template struct supports_emplace_back().emplace_back(std::declval()), void()) +> : true_type {}; + +template using enum_type_t = typename enable_if< + std::is_enum::value, typename std::underlying_type::type>::type; -template struct is_iterable : false_type {}; +template constexpr inline enum_type_t from_enum(E e) noexcept { return (enum_type_t)e; } +template constexpr inline E to_enum(enum_type_t value) noexcept { return (E)value; } -template struct is_iterable().cbegin(), declval().cend(), void()) + +template struct is_tuple_convertible : false_type {}; + +template struct is_tuple_convertible()._tuple_(), void()) > : true_type {}; -#include "lang/verify.h" -#include "threaded_log.h" +// string manipulation + +template +std::ostream & operator<<(std::ostream & o, const std::pair & d) { + return o << "<" << d.first << "," << d.second << ">"; +} + +template +inline typename enable_if::value, string>::type +implode(const C & v, string delim=" ") { + auto i=v.cbegin(), end=v.cend(); + if (i == end) + return string(); + std::ostringstream oss; + oss << *i++; + while (i != end) + oss << delim << *i++; + return oss.str(); +} + +inline std::vector explode(const string & s, string delim=" ") { + std::vector out; + size_t start = 0, end = 0; + while ((end = s.find(delim, start)) != string::npos) { + out.push_back(s.substr(start, end - start)); + start = end + delim.size(); + } + out.push_back(s.substr(start)); + return out; +} + +template +typename enable_if< + is_const_iterable::value && + !std::is_same::value, std::ostream>::type & +operator<<(std::ostream & o, const A & a) { + return o << "[" << implode(a, ", ") << "]"; +} + +#include +#define VERIFY(expr) { if (!(expr)) abort(); } + +// struct tuple adapter, useful for marshalling and endian swapping. usage: +// +// struct foo { +// int a, b; +// MEMBERS(a, b) +// }; + +#define MEMBERS(...) \ +inline auto _tuple_() -> decltype(std::tie(__VA_ARGS__)) { return std::tie(__VA_ARGS__); } \ +inline auto _tuple_() const -> decltype(std::tie(__VA_ARGS__)) { return std::tie(__VA_ARGS__); } + +// struct ordering and comparison operations; requires the use of MEMBERS. +// usage: +// +// LEXICOGRAPHIC_COMPARISON(foo) + +#define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \ +inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); } + +#define LEXICOGRAPHIC_COMPARISON(_c_) \ +LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \ +LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \ +LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=) + +// Tuple indexing in variadic templates. +// This implementation of tuple_indices is redistributed under the MIT +// License as an insubstantial portion of the LLVM compiler infrastructure. + +template struct tuple_indices {}; +template struct make_indices_imp; +template struct make_indices_imp, E> { + typedef typename make_indices_imp, E>::type type; +}; +template struct make_indices_imp, E> { + typedef tuple_indices type; +}; +template struct make_tuple_indices { + typedef typename make_indices_imp, E>::type type; +}; + +#define TUPLE_INDICES(_ArgPack_) typename make_tuple_indices::type() + +// Template parameter pack expansion is not allowed in certain contexts, but +// brace initializers (for instance, calls to constructors of empty structs) +// are fair game. +struct pass { template inline pass(Args && ...) {} }; + +#include "endian.h" + +#ifndef __has_attribute +#define __has_attribute(x) 0 +#endif + +#if __has_attribute(noreturn) +#define NORETURN [[noreturn]] +#else +#define NORETURN +#endif #endif