Make folly's T_CHECK_TIMEOUT/T_CHECK_TIME_LT use SKIP() on failure
[folly.git] / folly / Traits.h
index 132e0f2b4a0b9aa73365886f1b5098591bc169f8..16aab8d6ef1e9011b195e44f35d04e69b00144cc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -240,8 +240,10 @@ FOLLY_NAMESPACE_STD_BEGIN
 template <class T, class U>
   struct pair;
 #ifndef _GLIBCXX_USE_FB
+FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
 template <class T, class R, class A>
   class basic_string;
+FOLLY_GLIBCXX_NAMESPACE_CXX11_END
 #else
 template <class T, class R, class A, class S>
   class basic_string;
@@ -250,8 +252,10 @@ template <class T, class A>
   class vector;
 template <class T, class A>
   class deque;
+FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
 template <class T, class A>
   class list;
+FOLLY_GLIBCXX_NAMESPACE_CXX11_END
 template <class T, class C, class A>
   class set;
 template <class K, class V, class C, class A>
@@ -290,34 +294,6 @@ struct IsOneOf<T, T1, Ts...> {
   enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
 };
 
-/**
- * A traits class to check for incomplete types.
- *
- * Example:
- *
- *  struct FullyDeclared {}; // complete type
- *  struct ForwardDeclared; // incomplete type
- *
- *  is_complete<int>::value // evaluates to true
- *  is_complete<FullyDeclared>::value // evaluates to true
- *  is_complete<ForwardDeclared>::value // evaluates to false
- *
- *  struct ForwardDeclared {}; // declared, at last
- *
- *  is_complete<ForwardDeclared>::value // now it evaluates to true
- *
- * @author: Marcelo Juchem <marcelo@fb.com>
- */
-template <typename T>
-class is_complete {
-  template <unsigned long long> struct sfinae {};
-  template <typename U>
-  constexpr static bool test(sfinae<sizeof(U)>*) { return true; }
-  template <typename> constexpr static bool test(...) { return false; }
-public:
-  constexpr static bool value = test<T>(nullptr);
-};
-
 /*
  * Complementary type traits for integral comparisons.
  *
@@ -337,69 +313,32 @@ struct is_negative_impl {
 
 template <typename T>
 struct is_negative_impl<T, false> {
-  constexpr static bool check(T x) { return false; }
+  constexpr static bool check(T) { return false; }
 };
 
-template <typename RHS, RHS rhs, typename LHS>
-bool less_than_impl(
-  typename std::enable_if<
-    (rhs <= std::numeric_limits<LHS>::max()
-      && rhs > std::numeric_limits<LHS>::min()),
-    LHS
-  >::type const lhs
-) {
-  return lhs < rhs;
-}
+// folly::to integral specializations can end up generating code
+// inside what are really static ifs (not executed because of the templated
+// types) that violate -Wsign-compare so suppress them in order to not prevent
+// all calling code from using it.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wsign-compare"
 
 template <typename RHS, RHS rhs, typename LHS>
-bool less_than_impl(
-  typename std::enable_if<
-    (rhs > std::numeric_limits<LHS>::max()),
-    LHS
-  >::type const
-) {
-  return true;
+bool less_than_impl(LHS const lhs) {
+  return
+    rhs > std::numeric_limits<LHS>::max() ? true :
+    rhs <= std::numeric_limits<LHS>::min() ? false :
+    lhs < rhs;
 }
 
-template <typename RHS, RHS rhs, typename LHS>
-bool less_than_impl(
-  typename std::enable_if<
-    (rhs <= std::numeric_limits<LHS>::min()),
-    LHS
-  >::type const
-) {
-  return false;
-}
-
-template <typename RHS, RHS rhs, typename LHS>
-bool greater_than_impl(
-  typename std::enable_if<
-    (rhs <= std::numeric_limits<LHS>::max()
-      && rhs >= std::numeric_limits<LHS>::min()),
-    LHS
-  >::type const lhs
-) {
-  return lhs > rhs;
-}
-
-template <typename RHS, RHS rhs, typename LHS>
-bool greater_than_impl(
-  typename std::enable_if<
-    (rhs > std::numeric_limits<LHS>::max()),
-    LHS
-  >::type const
-) {
-  return false;
-}
+#pragma GCC diagnostic pop
 
 template <typename RHS, RHS rhs, typename LHS>
-bool greater_than_impl(
-  typename std::enable_if<
-    (rhs < std::numeric_limits<LHS>::min()),
-    LHS
-  >::type const
-) {
-  return true;
+bool greater_than_impl(LHS const lhs) {
+  return
+    rhs > std::numeric_limits<LHS>::max() ? false :
+    rhs < std::numeric_limits<LHS>::min() ? true :
+    lhs > rhs;
 }
 
 } // namespace detail {
@@ -438,6 +377,13 @@ bool greater_than(LHS const lhs) {
   >(lhs);
 }
 
+/**
+ * Like std::piecewise_construct, a tag type & instance used for in-place
+ * construction of non-movable contained types, e.g. by Synchronized.
+ */
+struct construct_in_place_t {};
+constexpr construct_in_place_t construct_in_place{};
+
 } // namespace folly
 
 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string);
@@ -451,6 +397,16 @@ FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function);
 // Boost
 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr);
 
+#define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \
+  template <typename T> \
+  struct classname { \
+    template <typename C> \
+    constexpr static bool test(typename C::type_name*) { return true; } \
+    template <typename> \
+    constexpr static bool test(...) { return false; } \
+    constexpr static bool value = test<T>(nullptr); \
+  }
+
 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
   template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
   class classname<TTheClass_, RTheReturn_(TTheArgs_...) cv_qual> { \