dom/xslt/base/txExpandedNameMap.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/base/txExpandedNameMap.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     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
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "txExpandedNameMap.h"
    1.10 +#include "txCore.h"
    1.11 +
    1.12 +class txMapItemComparator
    1.13 +{
    1.14 +  public:
    1.15 +    bool Equals(const txExpandedNameMap_base::MapItem& aItem,
    1.16 +                  const txExpandedName& aKey) const {
    1.17 +      return aItem.mNamespaceID == aKey.mNamespaceID &&
    1.18 +             aItem.mLocalName == aKey.mLocalName;
    1.19 +    }
    1.20 +};
    1.21 +
    1.22 +/**
    1.23 + * Adds an item, if an item with this key already exists an error is
    1.24 + * returned
    1.25 + * @param  aKey   key for item to add
    1.26 + * @param  aValue value of item to add
    1.27 + * @return errorcode
    1.28 + */
    1.29 +nsresult txExpandedNameMap_base::addItem(const txExpandedName& aKey,
    1.30 +                                         void* aValue)
    1.31 +{
    1.32 +    uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
    1.33 +    if (pos != mItems.NoIndex) {
    1.34 +        return NS_ERROR_XSLT_ALREADY_SET;
    1.35 +    }
    1.36 +
    1.37 +    MapItem* item = mItems.AppendElement();
    1.38 +    NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
    1.39 +
    1.40 +    item->mNamespaceID = aKey.mNamespaceID;
    1.41 +    item->mLocalName = aKey.mLocalName;
    1.42 +    item->mValue = aValue;
    1.43 +
    1.44 +    return NS_OK;
    1.45 +}
    1.46 +
    1.47 +/**
    1.48 + * Sets an item, if an item with this key already exists it is overwritten
    1.49 + * with the new value
    1.50 + * @param  aKey   key for item to set
    1.51 + * @param  aValue value of item to set
    1.52 + * @return errorcode
    1.53 + */
    1.54 +nsresult txExpandedNameMap_base::setItem(const txExpandedName& aKey,
    1.55 +                                         void* aValue,
    1.56 +                                         void** aOldValue)
    1.57 +{
    1.58 +    *aOldValue = nullptr;
    1.59 +    uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
    1.60 +    if (pos != mItems.NoIndex) {
    1.61 +        *aOldValue = mItems[pos].mValue;
    1.62 +        mItems[pos].mValue = aValue;
    1.63 +        
    1.64 +        return NS_OK;
    1.65 +    }
    1.66 +
    1.67 +    MapItem* item = mItems.AppendElement();
    1.68 +    NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
    1.69 +
    1.70 +    item->mNamespaceID = aKey.mNamespaceID;
    1.71 +    item->mLocalName = aKey.mLocalName;
    1.72 +    item->mValue = aValue;
    1.73 +
    1.74 +    return NS_OK;
    1.75 +}
    1.76 +
    1.77 +/**
    1.78 + * Gets an item
    1.79 + * @param  aKey  key for item to get
    1.80 + * @return item with specified key, or null if no such item exists
    1.81 + */
    1.82 +void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const
    1.83 +{
    1.84 +    uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
    1.85 +    if (pos != mItems.NoIndex) {
    1.86 +        return mItems[pos].mValue;
    1.87 +    }
    1.88 +
    1.89 +    return nullptr;
    1.90 +}
    1.91 +
    1.92 +/**
    1.93 + * Removes an item, deleting it if the map owns the values
    1.94 + * @param  aKey  key for item to remove
    1.95 + * @return item with specified key, or null if it has been deleted
    1.96 + *         or no such item exists
    1.97 + */
    1.98 +void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey)
    1.99 +{
   1.100 +    void* value = nullptr;
   1.101 +    uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
   1.102 +    if (pos != mItems.NoIndex) {
   1.103 +        value = mItems[pos].mValue;
   1.104 +        mItems.RemoveElementAt(pos);
   1.105 +    }
   1.106 +
   1.107 +    return value;
   1.108 +}

mercurial