Bug 1076587 - Allow nsAttrValueOrString helper to accept potentially-null pointers r=bz
authorJohn Schoenick <jschoenick@mozilla.com>
Tue, 01 Jul 2014 13:35:21 -0700
changeset 212303 03b6a747eac115e2df717486586e502e364a8edf
parent 212302 1b4e8921ba1659f3233e24b856cf6f51c85a095f
child 212304 6345f1767ed7c7a28d204bebccffdef133ba4072
push id27704
push userkwierso@gmail.com
push dateSat, 25 Oct 2014 01:25:30 +0000
treeherdermozilla-central@e37231060eb4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1076587
milestone36.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 1076587 - Allow nsAttrValueOrString helper to accept potentially-null pointers r=bz
content/base/src/nsAttrValueOrString.cpp
content/base/src/nsAttrValueOrString.h
--- a/content/base/src/nsAttrValueOrString.cpp
+++ b/content/base/src/nsAttrValueOrString.cpp
@@ -7,16 +7,21 @@
 
 const nsAString&
 nsAttrValueOrString::String() const
 {
   if (mStringPtr) {
     return *mStringPtr;
   }
 
+  if (!mAttrValue) {
+    mStringPtr = &mCheapString;
+    return *mStringPtr;
+  }
+
   if (mAttrValue->Type() == nsAttrValue::eString) {
     mCheapString = mAttrValue->GetStringValue();
     mStringPtr = &mCheapString;
     return *mStringPtr;
   }
 
   mAttrValue->ToString(mCheapString);
   mStringPtr = &mCheapString;
--- a/content/base/src/nsAttrValueOrString.h
+++ b/content/base/src/nsAttrValueOrString.h
@@ -3,16 +3,19 @@
  * 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/. */
 
 /*
  * A wrapper to contain either an nsAttrValue or an nsAString. This is useful
  * because constructing an nsAttrValue from an nsAString can be expensive when
  * the buffer of the string is not shared.
  *
+ * This treats nsAttrValueOrString(nullptr) as the empty string,
+ * to help with contexts where a null pointer denotes an empty value.
+ *
  * Since a raw pointer to the passed-in string is kept, this class should only
  * be used on the stack.
  */
 
 #ifndef nsAttrValueOrString_h___
 #define nsAttrValueOrString_h___
 
 #include "nsString.h"
@@ -21,22 +24,35 @@
 class MOZ_STACK_CLASS nsAttrValueOrString
 {
 public:
   explicit nsAttrValueOrString(const nsAString& aValue)
     : mAttrValue(nullptr)
     , mStringPtr(&aValue)
     , mCheapString(nullptr)
   { }
+
+  explicit nsAttrValueOrString(const nsAString* aValue)
+    : mAttrValue(nullptr)
+    , mStringPtr(aValue)
+    , mCheapString(nullptr)
+  { }
+
   explicit nsAttrValueOrString(const nsAttrValue& aValue)
     : mAttrValue(&aValue)
     , mStringPtr(nullptr)
     , mCheapString(nullptr)
   { }
 
+  explicit nsAttrValueOrString(const nsAttrValue* aValue)
+    : mAttrValue(aValue)
+    , mStringPtr(nullptr)
+    , mCheapString(nullptr)
+  { }
+
   /**
    * Returns a reference to the string value of the contents of this object.
    *
    * When this object points to a string or an nsAttrValue of string or atom
    * type this should be fairly cheap. Other nsAttrValue types will be
    * serialized the first time this is called and cached from thereon.
    */
   const nsAString& String() const;