Move the traits factories up, and remove <boost/mpl/has_xxx.hpp>
authorYedidya Feldblum <yfeldblum@fb.com>
Mon, 2 Jan 2017 06:25:37 +0000 (22:25 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 2 Jan 2017 06:33:12 +0000 (22:33 -0800)
Summary:
[Folly] Move the traits factories up, and remove `<boost/mpl/has_xxx.hpp>`.

Also, they now generate aliases to the types aliased by `std::true_type` and `std::false_type`. So now the API is entirely compatible with the requirements of `std::integral_constant` (because it is `std::integral_constant`).

Reviewed By: wqfish

Differential Revision: D4375944

fbshipit-source-id: dfd41283f13d793f7fc1f173590cd93cdee39a10

folly/Traits.h
folly/test/TraitsTest.cpp

index 999a7a257d5238f721d1322ea5bc45b5f030c1f8..a038b18c80e6bd2563f28afdf475775c2be56da9 100644 (file)
 #endif
 
 #include <boost/type_traits.hpp>
-#include <boost/mpl/has_xxx.hpp>
+
+#define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name)              \
+  template <typename TTheClass_>                                               \
+  struct classname##__folly_traits_impl__ {                                    \
+    template <typename UTheClass_>                                             \
+    static std::true_type test(typename UTheClass_::type_name*);               \
+    template <typename>                                                        \
+    static std::false_type test(...);                                          \
+  };                                                                           \
+  template <typename TTheClass_>                                               \
+  using classname = decltype(                                                  \
+      classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
+          nullptr))
+
+#define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
+  template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
+  struct classname##__folly_traits_impl__<                                    \
+      TTheClass_,                                                             \
+      RTheReturn_(TTheArgs_...) cv_qual> {                                    \
+    template <                                                                \
+        typename UTheClass_,                                                  \
+        RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual>                    \
+    struct sfinae {};                                                         \
+    template <typename UTheClass_>                                            \
+    static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*);  \
+    template <typename>                                                       \
+    static std::false_type test(...);                                         \
+  }
+
+/*
+ * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
+ * classes that check for the existence of a member function with
+ * a given name and signature. It currently does not support
+ * checking for inherited members.
+ *
+ * Such classes receive two template parameters: the class to be checked
+ * and the signature of the member function. A static boolean field
+ * named `value` (which is also constexpr) tells whether such member
+ * function exists.
+ *
+ * Each traits class created is bound only to the member name, not to
+ * its signature nor to the type of the class containing it.
+ *
+ * Say you need to know if a given class has a member function named
+ * `test` with the following signature:
+ *
+ *    int test() const;
+ *
+ * You'd need this macro to create a traits class to check for a member
+ * named `test`, and then use this traits class to check for the signature:
+ *
+ * namespace {
+ *
+ * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
+ *
+ * } // unnamed-namespace
+ *
+ * void some_func() {
+ *   cout << "Does class Foo have a member int test() const? "
+ *     << boolalpha << has_test_traits<Foo, int() const>::value;
+ * }
+ *
+ * You can use the same traits class to test for a completely different
+ * signature, on a completely different class, as long as the member name
+ * is the same:
+ *
+ * void some_func() {
+ *   cout << "Does class Foo have a member int test()? "
+ *     << boolalpha << has_test_traits<Foo, int()>::value;
+ *   cout << "Does class Foo have a member int test() const? "
+ *     << boolalpha << has_test_traits<Foo, int() const>::value;
+ *   cout << "Does class Bar have a member double test(const string&, long)? "
+ *     << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
+ * }
+ *
+ * @author: Marcelo Juchem <marcelo@fb.com>
+ */
+#define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name)               \
+  template <typename, typename>                                               \
+  struct classname##__folly_traits_impl__;                                    \
+  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, );             \
+  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const);        \
+  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(                                     \
+      classname, func_name, /* nolint */ volatile);                           \
+  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(                                     \
+      classname, func_name, /* nolint */ volatile const);                     \
+  template <typename TTheClass_, typename TTheSignature_>                     \
+  using classname =                                                           \
+      decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
+                   template test<TTheClass_>(nullptr))
 
 namespace folly {
 
@@ -91,7 +180,7 @@ namespace folly {
 namespace traits_detail {
 
 #define FOLLY_HAS_TRUE_XXX(name)                                             \
-  BOOST_MPL_HAS_XXX_TRAIT_DEF(name)                                          \
+  FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name);                     \
   template <class T>                                                         \
   struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
   template <class T>                                                         \
@@ -597,88 +686,6 @@ FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr)
 #endif
 
-#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> { \
-    template < \
-      typename UTheClass_, RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual \
-    > struct sfinae {}; \
-    template <typename UTheClass_> \
-    constexpr static bool test(sfinae<UTheClass_, &UTheClass_::func_name>*) \
-    { return true; } \
-    template <typename> \
-    constexpr static bool test(...) { return false; } \
-  public: \
-    constexpr static bool value = test<TTheClass_>(nullptr); \
-  }
-
-/*
- * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
- * classes that check for the existence of a member function with
- * a given name and signature. It currently does not support
- * checking for inherited members.
- *
- * Such classes receive two template parameters: the class to be checked
- * and the signature of the member function. A static boolean field
- * named `value` (which is also constexpr) tells whether such member
- * function exists.
- *
- * Each traits class created is bound only to the member name, not to
- * its signature nor to the type of the class containing it.
- *
- * Say you need to know if a given class has a member function named
- * `test` with the following signature:
- *
- *    int test() const;
- *
- * You'd need this macro to create a traits class to check for a member
- * named `test`, and then use this traits class to check for the signature:
- *
- * namespace {
- *
- * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
- *
- * } // unnamed-namespace
- *
- * void some_func() {
- *   cout << "Does class Foo have a member int test() const? "
- *     << boolalpha << has_test_traits<Foo, int() const>::value;
- * }
- *
- * You can use the same traits class to test for a completely different
- * signature, on a completely different class, as long as the member name
- * is the same:
- *
- * void some_func() {
- *   cout << "Does class Foo have a member int test()? "
- *     << boolalpha << has_test_traits<Foo, int()>::value;
- *   cout << "Does class Foo have a member int test() const? "
- *     << boolalpha << has_test_traits<Foo, int() const>::value;
- *   cout << "Does class Bar have a member double test(const string&, long)? "
- *     << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
- * }
- *
- * @author: Marcelo Juchem <marcelo@fb.com>
- */
-#define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
-  template <typename, typename> class classname; \
-  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
-  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
-  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
-      classname, func_name, /* nolint */ volatile); \
-  FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
-      classname, func_name, /* nolint */ volatile const)
-
 /* Some combinations of compilers and C++ libraries make __int128 and
  * unsigned __int128 available but do not correctly define their standard type
  * traits.
index a5f1a052822bb259c7996bc9b6d66b97a7fe7837..8658a58a2a4f69a60cd35ee8092d07ebd23de9b2 100644 (file)
 using namespace folly;
 using namespace std;
 
+namespace {
+
+FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x);
+}
+
+TEST(Traits, has_member_type) {
+  struct membership_no {};
+  struct membership_yes {
+    using x = void;
+  };
+
+  EXPECT_TRUE((is_same<false_type, has_member_type_x<membership_no>>::value));
+  EXPECT_TRUE((is_same<true_type, has_member_type_x<membership_yes>>::value));
+}
+
+//  Note: FOLLY_CREATE_HAS_MEMBER_FN_TRAITS tests are in
+//  folly/test/HasMemberFnTraitsTest.cpp.
+
 struct T1 {}; // old-style IsRelocatable, below
 struct T2 {}; // old-style IsRelocatable, below
 struct T3 { typedef std::true_type IsRelocatable; };
@@ -198,12 +216,3 @@ TEST(Traits, actuallyRelocatable) {
 
   testIsRelocatable<std::vector<char>>(5, 'g');
 }
-
-struct membership_no {};
-struct membership_yes { using x = void; };
-FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x);
-
-TEST(Traits, has_member_type) {
-  EXPECT_FALSE(bool(has_member_type_x<membership_no>::value));
-  EXPECT_TRUE(bool(has_member_type_x<membership_yes>::value));
-}