Bug 1163328 - Add an And<...> class to TemplateLib.h which performs logical and on a variadic number of booleans known at compile time. r=froydnj
authorBotond Ballo <botond@mozilla.com>
Thu, 21 May 2015 22:33:39 -0400
changeset 245179 0fe6e8656fc9553f26d61f3ac81ddcc7cc67f930
parent 245178 59599cdabdae5143701ced71c658715d27c63cdb
child 245180 1e588075e7eef1c12ee2854e41520de993b4c99c
push id28799
push userphilringnalda@gmail.com
push dateSat, 23 May 2015 20:31:44 +0000
treeherdermozilla-central@c3c0928c3cde [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1163328
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1163328 - Add an And<...> class to TemplateLib.h which performs logical and on a variadic number of booleans known at compile time. r=froydnj
mfbt/TemplateLib.h
mfbt/tests/TestTemplateLib.cpp
mfbt/tests/moz.build
--- a/mfbt/TemplateLib.h
+++ b/mfbt/TemplateLib.h
@@ -15,16 +15,18 @@
  */
 
 #ifndef mozilla_TemplateLib_h
 #define mozilla_TemplateLib_h
 
 #include <limits.h>
 #include <stddef.h>
 
+#include "mozilla/TypeTraits.h"
+
 namespace mozilla {
 
 namespace tl {
 
 /** Compute min/max. */
 template<size_t I, size_t J>
 struct Min
 {
@@ -100,13 +102,32 @@ template<size_t N>
 struct MulOverflowMask
 {
   static const size_t value =
     ~NBitMask<BitSize<size_t>::value - CeilingLog2<N>::value>::value;
 };
 template<> struct MulOverflowMask<0> { /* Error */ };
 template<> struct MulOverflowMask<1> { static const size_t value = 0; };
 
+/**
+ * And<bool...> computes the logical 'and' of its argument booleans.
+ *
+ * Examples:
+ *   mozilla::t1::And<true, true>::value is true.
+ *   mozilla::t1::And<true, false>::value is false.
+ *   mozilla::t1::And<>::value is true.
+ */
+
+template<bool...>
+struct And;
+
+template<>
+struct And<> : public TrueType { };
+
+template<bool C1, bool... Cn>
+struct And<C1, Cn...>
+  : public Conditional<C1, And<Cn...>, FalseType>::Type { };
+
 } // namespace tl
 
 } // namespace mozilla
 
 #endif /* mozilla_TemplateLib_h */
new file mode 100644
--- /dev/null
+++ b/mfbt/tests/TestTemplateLib.cpp
@@ -0,0 +1,35 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#include "mozilla/TemplateLib.h"
+
+using mozilla::tl::And;
+
+static_assert(And<>::value == true,
+              "And<>::value should be true");
+static_assert(And<true>::value == true,
+              "And<true>::value should be true");
+static_assert(And<false>::value == false,
+              "And<false>::value should be false");
+static_assert(And<false, true>::value == false,
+              "And<false, true>::value should be false");
+static_assert(And<false, false>::value == false,
+              "And<false, false>::value should be false");
+static_assert(And<true, false>::value == false,
+              "And<true, false>::value should be false");
+static_assert(And<true, true>::value == true,
+              "And<true, true>::value should be true");
+static_assert(And<true, true, true>::value == true,
+              "And<true, true, true>::value should be true");
+static_assert(And<true, false, true>::value == false,
+              "And<true, false, true>::value should be false");
+
+int
+main()
+{
+  // Nothing to do here.
+  return 0;
+}
--- a/mfbt/tests/moz.build
+++ b/mfbt/tests/moz.build
@@ -23,16 +23,17 @@ CppUnitTests([
     'TestMacroForEach',
     'TestMaybe',
     'TestPair',
     'TestRefPtr',
     'TestRollingMean',
     'TestSegmentedVector',
     'TestSHA1',
     'TestSplayTree',
+    'TestTemplateLib',
     'TestTypedEnum',
     'TestTypeTraits',
     'TestUniquePtr',
     'TestVector',
     'TestWeakPtr',
 ])
 
 if not CONFIG['MOZ_ASAN']: