More marshalling simplification
[invirt/third/libt4.git] / types.h
diff --git a/types.h b/types.h
index e78186c..cdb629a 100644 (file)
--- a/types.h
+++ b/types.h
@@ -99,6 +99,7 @@ using std::is_same;
 using std::underlying_type;
 using std::enable_if;
 using std::remove_reference;
+using std::add_const;
 
 #include <utility>
 using std::pair;
@@ -108,15 +109,29 @@ using std::forward;
 #include <vector>
 using std::vector;
 
+// type traits and manipulators
 
-template <class A, typename I=void> struct is_iterable : false_type {};
+template <class A, typename I=void> struct is_const_iterable : false_type {};
 
-template<class A> struct is_iterable<A,
+template<class A> struct is_const_iterable<A,
     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())
+> : true_type {};
+
+template<typename E>
+using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
+template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
+template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
+
+// string manipulation
+
 template <class C>
-inline typename enable_if<is_iterable<C>::value, string>::type
+inline typename enable_if<is_const_iterable<C>::value, string>::type
 implode(const C & v, string delim=" ") {
     if (v.begin() == v.end())
         return string();
@@ -141,10 +156,25 @@ inline vector<string> explode(const string &s, string delim=" ") {
 #include "lang/verify.h"
 #include "threaded_log.h"
 
+// struct tuple adapter, useful for marshalling
+// used like
+// struct foo {
+//     int a, b;
+//     MEMBERS(a, b)
+// };
+
 #define MEMBERS(...) \
 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
 
+// struct ordering and comparison
+// used like
+// struct foo {
+//     int a, b;
+//     MEMBERS(a, b)
+// };
+// LEXICOGRAPHIC_COMPARISON(foo)
+
 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
 inline bool operator _op_(const _c_ &b) const { return _tuple_() _op_ b._tuple_(); }
 
@@ -153,7 +183,9 @@ LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
 
-// The following implementation of tuple_indices is redistributed under the MIT
+// crucial tool for 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 <size_t...> struct tuple_indices {};
@@ -168,6 +200,11 @@ template <size_t E, size_t S=0> struct make_tuple_indices {
     typedef typename make_indices_imp<S, tuple_indices<>, E>::type 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 <typename... Args> inline pass(Args&&...) {} };
+
 #include "endian.h"
 
 #endif