Line data Source code
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/. */
6 :
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.
11 : *
12 : * This treats nsAttrValueOrString(nullptr) as the empty string,
13 : * to help with contexts where a null pointer denotes an empty value.
14 : *
15 : * Since a raw pointer to the passed-in string is kept, this class should only
16 : * be used on the stack.
17 : */
18 :
19 : #ifndef nsAttrValueOrString_h___
20 : #define nsAttrValueOrString_h___
21 :
22 : #include "nsString.h"
23 : #include "nsAttrValue.h"
24 :
25 1925 : class MOZ_STACK_CLASS nsAttrValueOrString
26 : {
27 : public:
28 1917 : explicit nsAttrValueOrString(const nsAString& aValue)
29 1917 : : mAttrValue(nullptr)
30 : , mStringPtr(&aValue)
31 1917 : , mCheapString(nullptr)
32 1917 : { }
33 :
34 : explicit nsAttrValueOrString(const nsAString* aValue)
35 : : mAttrValue(nullptr)
36 : , mStringPtr(aValue)
37 : , mCheapString(nullptr)
38 : { }
39 :
40 0 : explicit nsAttrValueOrString(const nsAttrValue& aValue)
41 0 : : mAttrValue(&aValue)
42 : , mStringPtr(nullptr)
43 0 : , mCheapString(nullptr)
44 0 : { }
45 :
46 8 : explicit nsAttrValueOrString(const nsAttrValue* aValue)
47 8 : : mAttrValue(aValue)
48 : , mStringPtr(nullptr)
49 8 : , mCheapString(nullptr)
50 8 : { }
51 :
52 90 : void ResetToAttrValue(const nsAttrValue& aValue)
53 : {
54 90 : mAttrValue = &aValue;
55 90 : mStringPtr = nullptr;
56 : // No need to touch mCheapString here. If we need to use it, we will reset
57 : // it to the rigthe value anyway.
58 90 : }
59 :
60 : /**
61 : * Returns a reference to the string value of the contents of this object.
62 : *
63 : * When this object points to a string or an nsAttrValue of string or atom
64 : * type this should be fairly cheap. Other nsAttrValue types will be
65 : * serialized the first time this is called and cached from thereon.
66 : */
67 : const nsAString& String() const;
68 :
69 : /**
70 : * Compares the string representation of this object with the string
71 : * representation of an nsAttrValue.
72 : */
73 134 : bool EqualsAsStrings(const nsAttrValue& aOther) const
74 : {
75 134 : if (mStringPtr) {
76 134 : return aOther.Equals(*mStringPtr, eCaseMatters);
77 : }
78 0 : return aOther.EqualsAsStrings(*mAttrValue);
79 : }
80 :
81 : /*
82 : * Returns true if the value stored is empty
83 : */
84 0 : bool IsEmpty() const
85 : {
86 0 : if (mStringPtr) {
87 0 : return mStringPtr->IsEmpty();
88 : }
89 0 : if (mAttrValue) {
90 0 : return mAttrValue->IsEmptyString();
91 : }
92 0 : return true;
93 : }
94 :
95 : protected:
96 : const nsAttrValue* mAttrValue;
97 : mutable const nsAString* mStringPtr;
98 : mutable nsCheapString mCheapString;
99 : };
100 :
101 : #endif // nsAttrValueOrString_h___
|