Bug 1835710 - Cancel off-thread JIT compilation before changing nursery allocation...
[gecko.git] / dom / base / nsAttrValueOrString.h
blob9cb3db33c554c239ad1e723cd58b730f384671d2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * A wrapper to contain either an nsAttrValue or an nsAString. This is useful
9 * because constructing an nsAttrValue from an nsAString can be expensive when
10 * the buffer of the string is not shared.
12 * This treats nsAttrValueOrString(nullptr) as the empty string,
13 * to help with contexts where a null pointer denotes an empty value.
15 * Since a raw pointer to the passed-in string is kept, this class should only
16 * be used on the stack.
19 #ifndef nsAttrValueOrString_h___
20 #define nsAttrValueOrString_h___
22 #include "nsString.h"
23 #include "nsAttrValue.h"
25 class MOZ_STACK_CLASS nsAttrValueOrString {
26 public:
27 explicit nsAttrValueOrString(const nsAString& aValue)
28 : mAttrValue(nullptr), mStringPtr(&aValue), mCheapString(nullptr) {}
30 explicit nsAttrValueOrString(const nsAString* aValue)
31 : mAttrValue(nullptr), mStringPtr(aValue), mCheapString(nullptr) {}
33 explicit nsAttrValueOrString(const nsAttrValue& aValue)
34 : mAttrValue(&aValue), mStringPtr(nullptr), mCheapString(nullptr) {}
36 explicit nsAttrValueOrString(const nsAttrValue* aValue)
37 : mAttrValue(aValue), mStringPtr(nullptr), mCheapString(nullptr) {}
39 void ResetToAttrValue(const nsAttrValue& aValue) {
40 mAttrValue = &aValue;
41 mStringPtr = nullptr;
42 // No need to touch mCheapString here. If we need to use it, we will reset
43 // it to the rigthe value anyway.
46 /**
47 * Returns a reference to the string value of the contents of this object.
49 * When this object points to a string or an nsAttrValue of string or atom
50 * type this should be fairly cheap. Other nsAttrValue types will be
51 * serialized the first time this is called and cached from thereon.
53 const nsAString& String() const;
55 /**
56 * Compares the string representation of this object with the string
57 * representation of an nsAttrValue.
59 bool EqualsAsStrings(const nsAttrValue& aOther) const {
60 if (mStringPtr) {
61 return aOther.Equals(*mStringPtr, eCaseMatters);
63 return aOther.EqualsAsStrings(*mAttrValue);
67 * Returns true if the value stored is empty
69 bool IsEmpty() const {
70 if (mStringPtr) {
71 return mStringPtr->IsEmpty();
73 if (mAttrValue) {
74 return mAttrValue->IsEmptyString();
76 return true;
79 protected:
80 const nsAttrValue* mAttrValue;
81 mutable const nsAString* mStringPtr;
82 mutable nsCheapString mCheapString;
85 #endif // nsAttrValueOrString_h___