Rewrote threaded log code to be more idiomatic.
[invirt/third/libt4.git] / types.h
diff --git a/types.h b/types.h
index d2d5411..0241bd3 100644 (file)
--- a/types.h
+++ b/types.h
@@ -5,37 +5,40 @@
 
 #include <algorithm>
 using std::copy;
-using std::move;
+using std::count_if;
+using std::find;
 using std::max;
 using std::min;
 using std::min_element;
-using std::find;
-using std::count_if;
+using std::move;
+using std::swap;
+
+#include <condition_variable>
+using cond = std::condition_variable;
+using std::cv_status;
 
 #include <chrono>
-using std::chrono::seconds;
-using std::chrono::milliseconds;
+using std::chrono::duration_cast;
 using std::chrono::microseconds;
+using std::chrono::milliseconds;
 using std::chrono::nanoseconds;
+using std::chrono::seconds;
 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;
+using std::chrono::time_point_cast;
 
 #include <exception>
 using std::exception;
 
 #include <fstream>
-using std::ofstream;
 using std::ifstream;
+using std::ofstream;
 
-#ifndef LIBT4_NO_FUNCTIONAL
 #include <functional>
+// std::bind conflicts with BIND(2)
 using std::function;
-using std::bind;
 using std::placeholders::_1;
-#endif
 
 #include <iomanip>
 #include <iostream>
@@ -66,12 +69,14 @@ using std::enable_shared_from_this;
 using std::make_shared;
 using std::shared_ptr;
 using std::unique_ptr;
+using std::weak_ptr;
 
 #include <mutex>
 using std::mutex;
 using lock = std::unique_lock<std::mutex>;
-using cond = std::condition_variable;
-using std::cv_status;
+
+#include <stdexcept>
+using std::runtime_error;
 
 #include <sstream>
 using std::ostringstream;
@@ -120,13 +125,13 @@ using std::vector;
 template <class A, typename I=void> struct is_const_iterable : false_type {};
 
 template<class A> struct is_const_iterable<A,
-    decltype(declval<A&>().cbegin(), declval<A&>().cend(), void())
+    decltype(declval<A &>().cbegin(), declval<A &>().cend(), void())
 > : true_type {};
 
 template <class A, typename I=void> struct supports_emplace_back : false_type {};
 
 template<class A> struct supports_emplace_back<A,
-    decltype(declval<A&>().emplace_back(declval<typename A::value_type>()), void())
+    decltype(declval<A &>().emplace_back(declval<typename A::value_type>()), void())
 > : true_type {};
 
 template<typename E>
@@ -136,6 +141,11 @@ template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept {
 
 // string manipulation
 
+template <class A, class B>
+ostream & operator<<(ostream & o, const pair<A,B> & d) {
+    return o << "<" << d.first << "," << d.second << ">";
+}
+
 template <class C>
 inline typename enable_if<is_const_iterable<C>::value, string>::type
 implode(const C & v, string delim=" ") {
@@ -149,7 +159,7 @@ implode(const C & v, string delim=" ") {
     return oss.str();
 }
 
-inline vector<string> explode(const string &s, string delim=" ") {
+inline vector<string> explode(const string & s, string delim=" ") {
     vector<string> out;
     size_t start = 0, end = 0;
     while ((end = s.find(delim, start)) != string::npos) {
@@ -160,7 +170,13 @@ inline vector<string> explode(const string &s, string delim=" ") {
     return out;
 }
 
-#include "lang/verify.h"
+template <class A>
+typename enable_if<is_const_iterable<A>::value && !is_same<A,string>::value, ostream>::type &
+operator<<(ostream & o, const A & a) {
+    return o << "[" << implode(a, ", ") << "]";
+}
+
+#include "verify.h"
 #include "threaded_log.h"
 
 // struct tuple adapter, useful for marshalling
@@ -183,7 +199,7 @@ inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS
 // LEXICOGRAPHIC_COMPARISON(foo)
 
 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
-inline bool operator _op_(const _c_ &b) const { return _tuple_() _op_ b._tuple_(); }
+inline bool operator _op_(const _c_ & b) const { return _tuple_() _op_ b._tuple_(); }
 
 #define LEXICOGRAPHIC_COMPARISON(_c_) \
 LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
@@ -210,8 +226,18 @@ template <size_t E, size_t S=0> struct make_tuple_indices {
 // 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 <typename... Args> inline pass(Args&&...) {} };
+struct pass { template <typename... Args> 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