Back to index

lightning-sunbird  0.9+nobinonly
nsClusterKeySet.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Chris Waterson <waterson@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsClusterKeySet_h__
00040 #define nsClusterKeySet_h__
00041 
00042 #include "nsClusterKey.h"
00043 #include "nsFixedSizeAllocator.h"
00044 
00048 class nsClusterKeySet {
00049 public:
00050     class ConstIterator;
00051     friend class ConstIterator;
00052 
00053 protected:
00054     class Entry {
00055     private:
00056         // Hide so that only Create() and Destroy() can be used to
00057         // allocate and deallocate from the heap
00058         static void* operator new(size_t) CPP_THROW_NEW { return 0; }
00059         static void operator delete(void*, size_t) {}
00060 
00061     public:
00062         Entry() { MOZ_COUNT_CTOR(nsClusterKeySet::Entry); }
00063 
00064         Entry(const nsClusterKey& aKey) : mKey(aKey) {
00065             MOZ_COUNT_CTOR(nsClusterKeySet::Entry); }
00066 
00067         ~Entry() { MOZ_COUNT_DTOR(nsClusterKeySet::Entry); }
00068 
00069         static Entry*
00070         Create(nsFixedSizeAllocator& aPool, const nsClusterKey& aKey) {
00071             void* place = aPool.Alloc(sizeof(Entry));
00072             return place ? ::new (place) Entry(aKey) : nsnull; }
00073 
00074         static void
00075         Destroy(nsFixedSizeAllocator& aPool, Entry* aEntry) {
00076             aEntry->~Entry();
00077             aPool.Free(aEntry, sizeof(*aEntry)); }
00078 
00079         PLHashEntry  mHashEntry;
00080         nsClusterKey mKey;
00081         Entry*       mPrev;
00082         Entry*       mNext;
00083     };
00084 
00085     PLHashTable* mTable;
00086     Entry mHead;
00087 
00088     nsFixedSizeAllocator mPool;
00089 
00090 public:
00091     nsClusterKeySet();
00092     ~nsClusterKeySet();
00093 
00094     class ConstIterator {
00095     protected:
00096         Entry* mCurrent;
00097 
00098     public:
00099         ConstIterator(Entry* aEntry) : mCurrent(aEntry) {}
00100 
00101         ConstIterator(const ConstIterator& aConstIterator)
00102             : mCurrent(aConstIterator.mCurrent) {}
00103 
00104         ConstIterator& operator=(const ConstIterator& aConstIterator) {
00105             mCurrent = aConstIterator.mCurrent;
00106             return *this; }
00107 
00108         ConstIterator& operator++() {
00109             mCurrent = mCurrent->mNext;
00110             return *this; }
00111 
00112         ConstIterator operator++(int) {
00113             ConstIterator result(*this);
00114             mCurrent = mCurrent->mNext;
00115             return result; }
00116 
00117         const nsClusterKey& operator*() const {
00118             return mCurrent->mKey; }
00119 
00120         const nsClusterKey* operator->() const {
00121             return &mCurrent->mKey; }
00122 
00123         PRBool operator==(const ConstIterator& aConstIterator) const {
00124             return mCurrent == aConstIterator.mCurrent; }
00125 
00126         PRBool operator!=(const ConstIterator& aConstIterator) const {
00127             return mCurrent != aConstIterator.mCurrent; }
00128     };
00129 
00130     ConstIterator First() const { return ConstIterator(mHead.mNext); }
00131     ConstIterator Last() const { return ConstIterator(NS_CONST_CAST(Entry*, &mHead)); }
00132 
00133     PRBool Contains(const nsClusterKey& aKey);
00134     nsresult Add(const nsClusterKey& aKey);
00135 
00136 protected:
00137     static PLHashAllocOps gAllocOps;
00138 
00139     static void* PR_CALLBACK AllocTable(void* aPool, PRSize aSize) {
00140         return new char[aSize]; }
00141 
00142     static void PR_CALLBACK FreeTable(void* aPool, void* aItem) {
00143         delete[] NS_STATIC_CAST(char*, aItem); }
00144 
00145     static PLHashEntry* PR_CALLBACK AllocEntry(void* aPool, const void* aKey) {
00146         nsFixedSizeAllocator* pool = NS_STATIC_CAST(nsFixedSizeAllocator*, aPool);
00147         const nsClusterKey* key = NS_STATIC_CAST(const nsClusterKey*, aKey);
00148         Entry* entry = Entry::Create(*pool, *key);
00149         return NS_REINTERPRET_CAST(PLHashEntry*, entry); }
00150 
00151     static void PR_CALLBACK FreeEntry(void* aPool, PLHashEntry* aEntry, PRUintn aFlag) {
00152         nsFixedSizeAllocator* pool = NS_STATIC_CAST(nsFixedSizeAllocator*, aPool);
00153         if (aFlag == HT_FREE_ENTRY)
00154             Entry::Destroy(*pool, NS_REINTERPRET_CAST(Entry*, aEntry)); }
00155 };
00156 
00157 #endif // nsClusterKeySet_h__