/src/mozilla-central/dom/base/nsAttrValueOrString.h
Line | Count | Source (jump to first uncovered line) |
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 | | class MOZ_STACK_CLASS nsAttrValueOrString |
26 | | { |
27 | | public: |
28 | | explicit nsAttrValueOrString(const nsAString& aValue) |
29 | | : mAttrValue(nullptr) |
30 | | , mStringPtr(&aValue) |
31 | | , mCheapString(nullptr) |
32 | 0 | { } |
33 | | |
34 | | explicit nsAttrValueOrString(const nsAString* aValue) |
35 | | : mAttrValue(nullptr) |
36 | | , mStringPtr(aValue) |
37 | | , mCheapString(nullptr) |
38 | 0 | { } |
39 | | |
40 | | explicit nsAttrValueOrString(const nsAttrValue& aValue) |
41 | | : mAttrValue(&aValue) |
42 | | , mStringPtr(nullptr) |
43 | | , mCheapString(nullptr) |
44 | 0 | { } |
45 | | |
46 | | explicit nsAttrValueOrString(const nsAttrValue* aValue) |
47 | | : mAttrValue(aValue) |
48 | | , mStringPtr(nullptr) |
49 | | , mCheapString(nullptr) |
50 | 0 | { } |
51 | | |
52 | | void ResetToAttrValue(const nsAttrValue& aValue) |
53 | 0 | { |
54 | 0 | mAttrValue = &aValue; |
55 | 0 | mStringPtr = nullptr; |
56 | 0 | // No need to touch mCheapString here. If we need to use it, we will reset |
57 | 0 | // it to the rigthe value anyway. |
58 | 0 | } |
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 | | bool EqualsAsStrings(const nsAttrValue& aOther) const |
74 | 0 | { |
75 | 0 | if (mStringPtr) { |
76 | 0 | return aOther.Equals(*mStringPtr, eCaseMatters); |
77 | 0 | } |
78 | 0 | return aOther.EqualsAsStrings(*mAttrValue); |
79 | 0 | } |
80 | | |
81 | | /* |
82 | | * Returns true if the value stored is empty |
83 | | */ |
84 | | bool IsEmpty() const |
85 | 0 | { |
86 | 0 | if (mStringPtr) { |
87 | 0 | return mStringPtr->IsEmpty(); |
88 | 0 | } |
89 | 0 | if (mAttrValue) { |
90 | 0 | return mAttrValue->IsEmptyString(); |
91 | 0 | } |
92 | 0 | return true; |
93 | 0 | } |
94 | | |
95 | | protected: |
96 | | const nsAttrValue* mAttrValue; |
97 | | mutable const nsAString* mStringPtr; |
98 | | mutable nsCheapString mCheapString; |
99 | | }; |
100 | | |
101 | | #endif // nsAttrValueOrString_h___ |