Bug 1331495 - Add an operator<< overload for Maybe. r=froydnj
authorBotond Ballo <botond@mozilla.com>
Wed, 18 Jan 2017 13:53:35 -0500
changeset 330737 9ef29560c30a3d6bc9ce238ab0b1f4ff5ca6ef3e
parent 330736 97795bcd29b111657e66948641d61f1126029b9f
child 330738 5c6e50cade7914bc1d011cd7be43723d13f1c84d
push id86062
push userphilringnalda@gmail.com
push dateTue, 24 Jan 2017 02:57:28 +0000
treeherdermozilla-inbound@cdeb2bdf61e3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1331495
milestone53.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 1331495 - Add an operator<< overload for Maybe. r=froydnj MozReview-Commit-ID: 8TcYoikeCvR
mfbt/Maybe.h
--- a/mfbt/Maybe.h
+++ b/mfbt/Maybe.h
@@ -11,16 +11,17 @@
 
 #include "mozilla/Alignment.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Move.h"
 #include "mozilla/TypeTraits.h"
 
 #include <new>  // for placement new
+#include <ostream>
 #include <type_traits>
 
 namespace mozilla {
 
 struct Nothing { };
 
 /*
  * Maybe is a container class which contains either zero or one elements. It
@@ -447,16 +448,27 @@ public:
    */
   template<typename... Args>
   void emplace(Args&&... aArgs)
   {
     MOZ_ASSERT(!mIsSome);
     ::new (mStorage.addr()) T(Forward<Args>(aArgs)...);
     mIsSome = true;
   }
+
+  friend std::ostream&
+  operator<<(std::ostream& aStream, const Maybe<T>& aMaybe)
+  {
+    if (aMaybe) {
+      aStream << aMaybe.ref();
+    } else {
+      aStream << "<Nothing>";
+    }
+    return aStream;
+  }
 };
 
 /*
  * Some() creates a Maybe<T> value containing the provided T value. If T has a
  * move constructor, it's used to make this as efficient as possible.
  *
  * Some() selects the type of Maybe it returns by removing any const, volatile,
  * or reference qualifiers from the type of the value you pass to it. This gives