summaryrefslogtreecommitdiff
path: root/dom/base/nsAttrValueOrString.h
blob: df0dac17de58eec53db0bc5398c4cb37a579f367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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.
 *
 * This treats nsAttrValueOrString(nullptr) as the empty string,
 * to help with contexts where a null pointer denotes an empty value.
 *
 * 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"

class MOZ_STACK_CLASS nsAttrValueOrString
{
public:
  explicit nsAttrValueOrString(const nsAString& aValue)
    : mAttrValue(nullptr)
    , mStringPtr(&aValue)
    , mCheapString(nullptr)
  { }

  explicit nsAttrValueOrString(const nsAString* aValue)
    : mAttrValue(nullptr)
    , mStringPtr(aValue)
    , mCheapString(nullptr)
  { }

  explicit nsAttrValueOrString(const nsAttrValue& aValue)
    : mAttrValue(&aValue)
    , mStringPtr(nullptr)
    , mCheapString(nullptr)
  { }

  explicit nsAttrValueOrString(const nsAttrValue* aValue)
    : mAttrValue(aValue)
    , mStringPtr(nullptr)
    , mCheapString(nullptr)
  { }

  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;
  }
  const nsAttrValue* GetAttrValue() { return mAttrValue; }

  /**
   * 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;
  nsAttrValue              mStoredAttrValue;
};

#endif // nsAttrValueOrString_h___