michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A wrapper to contain either an nsAttrValue or an nsAString. This is useful michael@0: * because constructing an nsAttrValue from an nsAString can be expensive when michael@0: * the buffer of the string is not shared. michael@0: * michael@0: * Since a raw pointer to the passed-in string is kept, this class should only michael@0: * be used on the stack. michael@0: */ michael@0: michael@0: #ifndef nsAttrValueOrString_h___ michael@0: #define nsAttrValueOrString_h___ michael@0: michael@0: #include "nsString.h" michael@0: #include "nsAttrValue.h" michael@0: michael@0: class MOZ_STACK_CLASS nsAttrValueOrString michael@0: { michael@0: public: michael@0: nsAttrValueOrString(const nsAString& aValue) michael@0: : mAttrValue(nullptr) michael@0: , mStringPtr(&aValue) michael@0: , mCheapString(nullptr) michael@0: { } michael@0: nsAttrValueOrString(const nsAttrValue& aValue) michael@0: : mAttrValue(&aValue) michael@0: , mStringPtr(nullptr) michael@0: , mCheapString(nullptr) michael@0: { } michael@0: michael@0: /** michael@0: * Returns a reference to the string value of the contents of this object. michael@0: * michael@0: * When this object points to a string or an nsAttrValue of string or atom michael@0: * type this should be fairly cheap. Other nsAttrValue types will be michael@0: * serialized the first time this is called and cached from thereon. michael@0: */ michael@0: const nsAString& String() const; michael@0: michael@0: /** michael@0: * Compares the string representation of this object with the string michael@0: * representation of an nsAttrValue. michael@0: */ michael@0: bool EqualsAsStrings(const nsAttrValue& aOther) const michael@0: { michael@0: if (mStringPtr) { michael@0: return aOther.Equals(*mStringPtr, eCaseMatters); michael@0: } michael@0: return aOther.EqualsAsStrings(*mAttrValue); michael@0: } michael@0: michael@0: protected: michael@0: const nsAttrValue* mAttrValue; michael@0: mutable const nsAString* mStringPtr; michael@0: mutable nsCheapString mCheapString; michael@0: }; michael@0: michael@0: #endif // nsAttrValueOrString_h___