content/base/src/nsAttrValueOrString.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsAttrValueOrString.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * A wrapper to contain either an nsAttrValue or an nsAString. This is useful
    1.11 + * because constructing an nsAttrValue from an nsAString can be expensive when
    1.12 + * the buffer of the string is not shared.
    1.13 + *
    1.14 + * Since a raw pointer to the passed-in string is kept, this class should only
    1.15 + * be used on the stack.
    1.16 + */
    1.17 +
    1.18 +#ifndef nsAttrValueOrString_h___
    1.19 +#define nsAttrValueOrString_h___
    1.20 +
    1.21 +#include "nsString.h"
    1.22 +#include "nsAttrValue.h"
    1.23 +
    1.24 +class MOZ_STACK_CLASS nsAttrValueOrString
    1.25 +{
    1.26 +public:
    1.27 +  nsAttrValueOrString(const nsAString& aValue)
    1.28 +    : mAttrValue(nullptr)
    1.29 +    , mStringPtr(&aValue)
    1.30 +    , mCheapString(nullptr)
    1.31 +  { }
    1.32 +  nsAttrValueOrString(const nsAttrValue& aValue)
    1.33 +    : mAttrValue(&aValue)
    1.34 +    , mStringPtr(nullptr)
    1.35 +    , mCheapString(nullptr)
    1.36 +  { }
    1.37 +
    1.38 +  /**
    1.39 +   * Returns a reference to the string value of the contents of this object.
    1.40 +   *
    1.41 +   * When this object points to a string or an nsAttrValue of string or atom
    1.42 +   * type this should be fairly cheap. Other nsAttrValue types will be
    1.43 +   * serialized the first time this is called and cached from thereon.
    1.44 +   */
    1.45 +  const nsAString& String() const;
    1.46 +
    1.47 +  /**
    1.48 +   * Compares the string representation of this object with the string
    1.49 +   * representation of an nsAttrValue.
    1.50 +   */
    1.51 +  bool EqualsAsStrings(const nsAttrValue& aOther) const
    1.52 +  {
    1.53 +    if (mStringPtr) {
    1.54 +      return aOther.Equals(*mStringPtr, eCaseMatters);
    1.55 +    }
    1.56 +    return aOther.EqualsAsStrings(*mAttrValue);
    1.57 +  }
    1.58 +
    1.59 +protected:
    1.60 +  const nsAttrValue*       mAttrValue;
    1.61 +  mutable const nsAString* mStringPtr;
    1.62 +  mutable nsCheapString    mCheapString;
    1.63 +};
    1.64 +
    1.65 +#endif // nsAttrValueOrString_h___

mercurial