Bug 616262 - Add mozilla/TypeTraits.h with mozilla::Conditional and mozilla::IsBaseOf traits, similar to resp. C++11 std::conditional and std::is_base_of. r=Waldo
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 14 Jun 2012 08:20:47 +0200
changeset 101474 6a4feddc241b00239656e8fb67bdef2cf22637d3
parent 101473 da8c6039c25ec00aeb3e90d02957ab7d711b9cb6
child 101475 560c492f81ad60d02f2adf78beb7813bae428b33
push idunknown
push userunknown
push dateunknown
reviewersWaldo
bugs616262
milestone16.0a1
Bug 616262 - Add mozilla/TypeTraits.h with mozilla::Conditional and mozilla::IsBaseOf traits, similar to resp. C++11 std::conditional and std::is_base_of. r=Waldo
mfbt/TypeTraits.h
mfbt/exported_headers.mk
new file mode 100644
--- /dev/null
+++ b/mfbt/TypeTraits.h
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_TypeTraits_h_
+#define mozilla_TypeTraits_h_
+
+/* Template-based metaprogramming and type-testing facilities. */
+
+namespace mozilla {
+
+/*
+ * IsBaseOf allows to know whether a given class is derived from another.
+ *
+ * Consider the following class definitions:
+ *
+ *   class A {};
+ *   class B : public A {};
+ *   class C {};
+ *
+ * mozilla::IsBaseOf<A, B>::value is true;
+ * mozilla::IsBaseOf<A, C>::value is false;
+ */
+template <class Base, class Derived>
+class IsBaseOf
+{
+  private:
+    static char Test(Base *);
+    static int Test(...);
+  public:
+    static const bool value = (sizeof(Test(static_cast<Derived *>(0))) == sizeof(char));
+};
+
+/*
+ * Conditional selects a class between two, depending on a given boolean value.
+ *
+ * mozilla::Conditional<true, A, B>::Type is A;
+ * mozilla::Conditional<false, A, B>::Type is B;
+ */
+template <bool condition, class A, class B>
+struct Conditional
+{
+    typedef A Type;
+};
+
+template <class A, class B>
+struct Conditional<false, A, B>
+{
+    typedef B Type;
+};
+
+} /* namespace mozilla */
+
+#endif  /* mozilla_TypeTraits_h_ */
--- a/mfbt/exported_headers.mk
+++ b/mfbt/exported_headers.mk
@@ -19,11 +19,12 @@ EXPORTS_mozilla += \
   Likely.h \
   LinkedList.h \
   MSStdInt.h \
   RangedPtr.h \
   RefPtr.h \
   Scoped.h \
   StandardInteger.h \
   ThreadLocal.h \
+  TypeTraits.h \
   Types.h \
   Util.h \
   $(NULL)