2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-02-14 06:00:56 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2014-07-02 00:35:21 +04:00
|
|
|
* This treats nsAttrValueOrString(nullptr) as the empty string,
|
|
|
|
* to help with contexts where a null pointer denotes an empty value.
|
|
|
|
*
|
2012-02-14 06:00:56 +04:00
|
|
|
* 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"
|
|
|
|
#include "nsAttrValue.h"
|
|
|
|
|
2013-04-12 07:20:09 +04:00
|
|
|
class MOZ_STACK_CLASS nsAttrValueOrString
|
2012-02-14 06:00:56 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsAttrValueOrString(const nsAString& aValue)
|
2012-07-30 18:20:58 +04:00
|
|
|
: mAttrValue(nullptr)
|
2012-02-14 06:00:56 +04:00
|
|
|
, mStringPtr(&aValue)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mCheapString(nullptr)
|
2012-02-14 06:00:56 +04:00
|
|
|
{ }
|
2014-07-02 00:35:21 +04:00
|
|
|
|
|
|
|
explicit nsAttrValueOrString(const nsAString* aValue)
|
|
|
|
: mAttrValue(nullptr)
|
|
|
|
, mStringPtr(aValue)
|
|
|
|
, mCheapString(nullptr)
|
|
|
|
{ }
|
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsAttrValueOrString(const nsAttrValue& aValue)
|
2012-02-14 06:00:56 +04:00
|
|
|
: mAttrValue(&aValue)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mStringPtr(nullptr)
|
|
|
|
, mCheapString(nullptr)
|
2012-02-14 06:00:56 +04:00
|
|
|
{ }
|
|
|
|
|
2014-07-02 00:35:21 +04:00
|
|
|
explicit nsAttrValueOrString(const nsAttrValue* aValue)
|
|
|
|
: mAttrValue(aValue)
|
|
|
|
, mStringPtr(nullptr)
|
|
|
|
, mCheapString(nullptr)
|
|
|
|
{ }
|
|
|
|
|
2015-08-01 08:14:06 +03:00
|
|
|
void TakeParsedValue(nsAttrValue& aValue)
|
|
|
|
{
|
|
|
|
mStoredAttrValue.SwapValueWith(aValue);
|
|
|
|
mAttrValue = &mStoredAttrValue;
|
|
|
|
mStringPtr = nullptr;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If TakeParsedValue has been called, returns the value that it set.
|
|
|
|
*/
|
|
|
|
nsAttrValue* GetStoredAttrValue()
|
|
|
|
{
|
|
|
|
return mAttrValue == &mStoredAttrValue ? &mStoredAttrValue : nullptr;
|
|
|
|
}
|
2015-08-01 08:46:15 +03:00
|
|
|
const nsAttrValue* GetAttrValue() { return mAttrValue; }
|
2015-08-01 08:14:06 +03:00
|
|
|
|
2012-02-14 06:00:56 +04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the string representation of this object with the string
|
|
|
|
* representation of an nsAttrValue.
|
|
|
|
*/
|
|
|
|
bool EqualsAsStrings(const nsAttrValue& aOther) const
|
|
|
|
{
|
|
|
|
if (mStringPtr) {
|
|
|
|
return aOther.Equals(*mStringPtr, eCaseMatters);
|
|
|
|
}
|
|
|
|
return aOther.EqualsAsStrings(*mAttrValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const nsAttrValue* mAttrValue;
|
|
|
|
mutable const nsAString* mStringPtr;
|
|
|
|
mutable nsCheapString mCheapString;
|
2015-08-01 08:14:06 +03:00
|
|
|
nsAttrValue mStoredAttrValue;
|
2012-02-14 06:00:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsAttrValueOrString_h___
|