dom/base/nsAttrValueOrString.h
author Mike Hommey <mh+mozilla@glandium.org>
Thu, 26 Mar 2015 12:07:17 +0900
changeset 258314 fc1e894eec2fbd34b745cd94f505080427d24705
parent 236672 2db29c0ae60b6eb0e196165631127d195fe2ef0b
child 272011 bd079aadd3feeee3f9b9f73c5e0bc4bd6a870722
permissions -rw-r--r--
Bug 1147207 - Add a ComposedFinder class that acts like a FileFinder proxy over multiple FileFinders. r=gps, a=sledru

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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)
  { }

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

#endif // nsAttrValueOrString_h___