[meta] Add hb_is_base_of
authorBehdad Esfahbod <behdad@behdad.org>
Fri, 10 May 2019 18:26:39 +0000 (11:26 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Fri, 10 May 2019 18:26:49 +0000 (11:26 -0700)
src/hb-meta.hh
src/test-meta.cc

index 88b7f2f..645eb54 100644 (file)
@@ -147,6 +147,13 @@ struct hb_is_convertible
 };
 #define hb_is_convertible(From,To) hb_is_convertible<From, To>::value
 
+template <typename Base, typename Derived>
+struct hb_is_base_of
+{
+  static constexpr bool value = hb_is_convertible (hb_decay<Derived> *, hb_decay<Base> *);
+};
+#define hb_is_base_of(Base,Derived) hb_is_base_of<Base, Derived>::value
+
 template <typename From, typename To>
 struct hb_is_cr_convertible
 {
index 9037e9c..716e99c 100644 (file)
@@ -78,5 +78,21 @@ main (int argc, char **argv)
   static_assert (hb_is_convertible (int *, void *), "");
   static_assert (!hb_is_convertible (void *, int *), "");
 
+  struct Y : X {};
+
+  static_assert (hb_is_base_of (void, void));
+  static_assert (hb_is_base_of (void, int));
+  static_assert (!hb_is_base_of (int, void));
+
+  static_assert (hb_is_base_of (int, int));
+  static_assert (hb_is_base_of (const int, int));
+  static_assert (hb_is_base_of (int, const int));
+
+  static_assert (hb_is_base_of (X, X));
+  static_assert (hb_is_base_of (X, Y));
+  static_assert (hb_is_base_of (const X, Y));
+  static_assert (hb_is_base_of (X, const Y));
+  static_assert (!hb_is_base_of (Y, X));
+
   return 0;
 }