xpcom/ds/nsHashtable.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/nsHashtable.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,217 @@
     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 + * This Original Code has been modified by IBM Corporation.
     1.9 + * Modifications made by IBM described herein are
    1.10 + * Copyright (c) International Business Machines
    1.11 + * Corporation, 2000
    1.12 + *
    1.13 + * Modifications to Mozilla code or documentation
    1.14 + * identified per MPL Section 3.3
    1.15 + *
    1.16 + * Date         Modified by     Description of modification
    1.17 + * 04/20/2000   IBM Corp.       Added PR_CALLBACK for Optlink use in OS2
    1.18 + */
    1.19 +
    1.20 +/**
    1.21 + * nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
    1.22 + */
    1.23 +
    1.24 +#ifndef nsHashtable_h__
    1.25 +#define nsHashtable_h__
    1.26 +
    1.27 +#include "pldhash.h"
    1.28 +#include "nscore.h"
    1.29 +#include "nsISupports.h"
    1.30 +#include "nsISupportsImpl.h"
    1.31 +#include "nsStringFwd.h"
    1.32 +
    1.33 +class nsIObjectInputStream;
    1.34 +class nsIObjectOutputStream;
    1.35 +
    1.36 +struct PRLock;
    1.37 +
    1.38 +class nsHashKey {
    1.39 +  protected:
    1.40 +    nsHashKey(void) {
    1.41 +#ifdef DEBUG
    1.42 +        mKeyType = UnknownKey;
    1.43 +#endif
    1.44 +        MOZ_COUNT_CTOR(nsHashKey);
    1.45 +    }
    1.46 +
    1.47 +
    1.48 +  public:
    1.49 +    // Virtual destructor because all hash keys are |delete|d via a
    1.50 +    // nsHashKey pointer.
    1.51 +
    1.52 +    virtual ~nsHashKey(void);
    1.53 +    virtual uint32_t HashCode(void) const = 0;
    1.54 +    virtual bool Equals(const nsHashKey *aKey) const = 0;
    1.55 +    virtual nsHashKey *Clone() const = 0;
    1.56 +    virtual nsresult Write(nsIObjectOutputStream* aStream) const;
    1.57 +
    1.58 +#ifdef DEBUG
    1.59 +  public:
    1.60 +    // used for verification that we're casting to the correct key type
    1.61 +    enum nsHashKeyType {
    1.62 +        UnknownKey,
    1.63 +        SupportsKey,
    1.64 +        PRUint32Key,
    1.65 +        VoidKey,
    1.66 +        IDKey,
    1.67 +        CStringKey,
    1.68 +        StringKey
    1.69 +    };
    1.70 +    nsHashKeyType GetKeyType() const { return mKeyType; }
    1.71 +  protected:
    1.72 +    nsHashKeyType mKeyType;
    1.73 +#endif
    1.74 +};
    1.75 +
    1.76 +// Enumerator and Read/Write callback functions.
    1.77 +
    1.78 +// Return values for nsHashtableEnumFunc
    1.79 +enum {
    1.80 +    kHashEnumerateStop      = false,
    1.81 +    kHashEnumerateNext      = true
    1.82 +};
    1.83 +
    1.84 +typedef bool
    1.85 +(* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
    1.86 +
    1.87 +typedef nsresult
    1.88 +(* nsHashtableReadEntryFunc)(nsIObjectInputStream *aStream, nsHashKey **aKey,
    1.89 +                             void **aData);
    1.90 +
    1.91 +// NB: may be called with null aKey or aData, to free just one of the two.
    1.92 +typedef void
    1.93 +(* nsHashtableFreeEntryFunc)(nsIObjectInputStream *aStream, nsHashKey *aKey,
    1.94 +                             void *aData);
    1.95 +
    1.96 +typedef nsresult
    1.97 +(* nsHashtableWriteDataFunc)(nsIObjectOutputStream *aStream, void *aData);
    1.98 +
    1.99 +class nsHashtable {
   1.100 +  protected:
   1.101 +    // members
   1.102 +    PRLock*         mLock;
   1.103 +    PLDHashTable    mHashtable;
   1.104 +    bool            mEnumerating;
   1.105 +
   1.106 +  public:
   1.107 +    nsHashtable(uint32_t aSize = 16, bool aThreadSafe = false);
   1.108 +    virtual ~nsHashtable();
   1.109 +
   1.110 +    int32_t Count(void) { return mHashtable.entryCount; }
   1.111 +    bool Exists(nsHashKey *aKey);
   1.112 +    void *Put(nsHashKey *aKey, void *aData);
   1.113 +    void *Get(nsHashKey *aKey);
   1.114 +    void *Remove(nsHashKey *aKey);
   1.115 +    nsHashtable *Clone();
   1.116 +    void Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure = nullptr);
   1.117 +    void Reset();
   1.118 +    void Reset(nsHashtableEnumFunc destroyFunc, void* aClosure = nullptr);
   1.119 +
   1.120 +    nsHashtable(nsIObjectInputStream* aStream,
   1.121 +                nsHashtableReadEntryFunc aReadEntryFunc,
   1.122 +                nsHashtableFreeEntryFunc aFreeEntryFunc,
   1.123 +                nsresult *aRetVal);
   1.124 +    nsresult Write(nsIObjectOutputStream* aStream,
   1.125 +                   nsHashtableWriteDataFunc aWriteDataFunc) const;
   1.126 +};
   1.127 +
   1.128 +////////////////////////////////////////////////////////////////////////////////
   1.129 +// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
   1.130 +// deleted
   1.131 +
   1.132 +typedef void* (* nsHashtableCloneElementFunc)(nsHashKey *aKey, void *aData, void* aClosure);
   1.133 +
   1.134 +class nsObjectHashtable : public nsHashtable {
   1.135 +  public:
   1.136 +    nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
   1.137 +                      void* cloneElementClosure,
   1.138 +                      nsHashtableEnumFunc destroyElementFun,
   1.139 +                      void* destroyElementClosure,
   1.140 +                      uint32_t aSize = 16, bool threadSafe = false);
   1.141 +    ~nsObjectHashtable();
   1.142 +
   1.143 +    nsHashtable *Clone();
   1.144 +    void Reset();
   1.145 +    bool RemoveAndDelete(nsHashKey *aKey);
   1.146 +
   1.147 +  protected:
   1.148 +    static PLDHashOperator CopyElement(PLDHashTable* table,
   1.149 +                                       PLDHashEntryHdr* hdr,
   1.150 +                                       uint32_t i, void *arg);
   1.151 +
   1.152 +    nsHashtableCloneElementFunc mCloneElementFun;
   1.153 +    void*                       mCloneElementClosure;
   1.154 +    nsHashtableEnumFunc         mDestroyElementFun;
   1.155 +    void*                       mDestroyElementClosure;
   1.156 +};
   1.157 +
   1.158 +////////////////////////////////////////////////////////////////////////////////
   1.159 +
   1.160 +class nsPRUint32Key : public nsHashKey {
   1.161 +protected:
   1.162 +    uint32_t mKey;
   1.163 +public:
   1.164 +    nsPRUint32Key(uint32_t key) {
   1.165 +#ifdef DEBUG
   1.166 +        mKeyType = PRUint32Key;
   1.167 +#endif
   1.168 +        mKey = key;
   1.169 +    }
   1.170 +
   1.171 +    uint32_t HashCode(void) const {
   1.172 +        return mKey;
   1.173 +    }
   1.174 +
   1.175 +    bool Equals(const nsHashKey *aKey) const {
   1.176 +        return mKey == ((const nsPRUint32Key *) aKey)->mKey;
   1.177 +    }
   1.178 +    nsHashKey *Clone() const {
   1.179 +        return new nsPRUint32Key(mKey);
   1.180 +    }
   1.181 +    uint32_t GetValue() { return mKey; }
   1.182 +};
   1.183 +
   1.184 +// for null-terminated c-strings
   1.185 +class nsCStringKey : public nsHashKey {
   1.186 +  public:
   1.187 +
   1.188 +    // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
   1.189 +    enum Ownership {
   1.190 +        NEVER_OWN,  // very long lived, even clones don't need to copy it.
   1.191 +        OWN_CLONE,  // as long lived as this key. But clones make a copy.
   1.192 +        OWN         // to be free'd in key dtor. Clones make their own copy.
   1.193 +    };
   1.194 +
   1.195 +    nsCStringKey(const nsCStringKey& aStrKey);
   1.196 +    nsCStringKey(const char* str, int32_t strLen = -1, Ownership own = OWN_CLONE);
   1.197 +    nsCStringKey(const nsAFlatCString& str);
   1.198 +    nsCStringKey(const nsACString& str);
   1.199 +    ~nsCStringKey(void);
   1.200 +
   1.201 +    uint32_t HashCode(void) const;
   1.202 +    bool Equals(const nsHashKey* aKey) const;
   1.203 +    nsHashKey* Clone() const;
   1.204 +    nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
   1.205 +    nsresult Write(nsIObjectOutputStream* aStream) const;
   1.206 +
   1.207 +    // For when the owner of the hashtable wants to peek at the actual
   1.208 +    // string in the key. No copy is made, so be careful.
   1.209 +    const char* GetString() const { return mStr; }
   1.210 +    uint32_t GetStringLength() const { return mStrLen; }
   1.211 +
   1.212 +  protected:
   1.213 +    char*       mStr;
   1.214 +    uint32_t    mStrLen;
   1.215 +    Ownership   mOwnership;
   1.216 +};
   1.217 +
   1.218 +////////////////////////////////////////////////////////////////////////////////
   1.219 +
   1.220 +#endif // nsHashtable_h__

mercurial