1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : /*
7 : * A wrapper to contain either an nsAttrValue or an nsAString. This is useful
8 : * because constructing an nsAttrValue from an nsAString can be expensive when
9 : * the buffer of the string is not shared.
10 : *
11 : * Since a raw pointer to the passed-in string is kept, this class should only
12 : * be used on the stack.
13 : */
14 :
15 : #ifndef nsAttrValueOrString_h___
16 : #define nsAttrValueOrString_h___
17 :
18 : #include "nsString.h"
19 : #include "nsAttrValue.h"
20 :
21 : class NS_STACK_CLASS nsAttrValueOrString
22 11455 : {
23 : public:
24 11455 : nsAttrValueOrString(const nsAString& aValue)
25 : : mAttrValue(nsnull)
26 : , mStringPtr(&aValue)
27 11455 : , mCheapString(nsnull)
28 11455 : { }
29 0 : nsAttrValueOrString(const nsAttrValue& aValue)
30 : : mAttrValue(&aValue)
31 : , mStringPtr(nsnull)
32 0 : , mCheapString(nsnull)
33 0 : { }
34 :
35 : /**
36 : * Returns a reference to the string value of the contents of this object.
37 : *
38 : * When this object points to a string or an nsAttrValue of string or atom
39 : * type this should be fairly cheap. Other nsAttrValue types will be
40 : * serialized the first time this is called and cached from thereon.
41 : */
42 : const nsAString& String() const;
43 :
44 : /**
45 : * Compares the string representation of this object with the string
46 : * representation of an nsAttrValue.
47 : */
48 7 : bool EqualsAsStrings(const nsAttrValue& aOther) const
49 : {
50 7 : if (mStringPtr) {
51 7 : return aOther.Equals(*mStringPtr, eCaseMatters);
52 : }
53 0 : return aOther.EqualsAsStrings(*mAttrValue);
54 : }
55 :
56 : protected:
57 : const nsAttrValue* mAttrValue;
58 : mutable const nsAString* mStringPtr;
59 : mutable nsCheapString mCheapString;
60 : };
61 :
62 : #endif // nsAttrValueOrString_h___
|