Bug 1076587 - Allow nsAttrValueOrString helper to accept potentially-null pointers r=bz

This commit is contained in:
John Schoenick 2014-07-01 13:35:21 -07:00
parent cfe73894a6
commit 7638c8af36
2 changed files with 21 additions and 0 deletions

View File

@ -12,6 +12,11 @@ nsAttrValueOrString::String() const
return *mStringPtr;
}
if (!mAttrValue) {
mStringPtr = &mCheapString;
return *mStringPtr;
}
if (mAttrValue->Type() == nsAttrValue::eString) {
mCheapString = mAttrValue->GetStringValue();
mStringPtr = &mCheapString;

View File

@ -8,6 +8,9 @@
* 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.
*/
@ -26,12 +29,25 @@ public:
, 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.
*