Back to index

lightning-sunbird  0.9+nobinonly
ipcList.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef ipcList_h__
00039 #define ipcList_h__
00040 
00041 #include "prtypes.h"
00042 
00043 //-----------------------------------------------------------------------------
00044 // simple list of singly-linked objects.  class T must have the following
00045 // structure:
00046 //
00047 //   class T {
00048 //   ...
00049 //   public:
00050 //     T *mNext;
00051 //   };
00052 //
00053 // objects added to the list must be allocated with operator new.  class T may
00054 // optionally inherit from ipcListNode<T> if it doesn't wish to define mNext
00055 // explicitly.
00056 //-----------------------------------------------------------------------------
00057 
00058 template<class T>
00059 class ipcList
00060 {
00061 public:
00062     ipcList()
00063         : mHead(NULL)
00064         , mTail(NULL)
00065         { }
00066    ~ipcList() { DeleteAll(); }
00067 
00068     //
00069     // prepends obj at the beginning of the list.
00070     //
00071     void Prepend(T *obj)
00072     {
00073         obj->mNext = mHead;
00074         mHead = obj;
00075         if (!mTail)
00076             mTail = mHead;
00077     }
00078 
00079     //
00080     // appends obj to the end of the list.
00081     //
00082     void Append(T *obj)
00083     {
00084         obj->mNext = NULL;
00085         if (mTail) {
00086             mTail->mNext = obj;
00087             mTail = obj;
00088         }
00089         else
00090             mTail = mHead = obj;
00091     }
00092 
00093     //
00094     // inserts b into the list after a.
00095     //
00096     void InsertAfter(T *a, T *b)
00097     {
00098         b->mNext = a->mNext;
00099         a->mNext = b;
00100         if (mTail == a)
00101             mTail = b;
00102     }
00103 
00104     // 
00105     // removes first element w/o deleting it
00106     //
00107     void RemoveFirst()
00108     {
00109         if (mHead)
00110             AdvanceHead();
00111     }
00112 
00113     //
00114     // removes element after the given element w/o deleting it
00115     //
00116     void RemoveAfter(T *obj)
00117     {
00118         T *rej = obj->mNext;
00119         if (rej) {
00120             obj->mNext = rej->mNext;
00121             if (rej == mTail)
00122                 mTail = obj;
00123         }
00124     }
00125 
00126     //
00127     // deletes first element
00128     //
00129     void DeleteFirst()
00130     {
00131         T *first = mHead;
00132         if (first) {
00133             AdvanceHead();
00134             delete first;
00135         }
00136     }
00137 
00138     //
00139     // deletes element after the given element
00140     //
00141     void DeleteAfter(T *obj)
00142     {
00143         T *rej = obj->mNext;
00144         if (rej) {
00145             RemoveAfter(obj);
00146             delete rej;
00147         }
00148     }
00149 
00150     //
00151     // deletes all elements
00152     //
00153     void DeleteAll()
00154     {
00155         while (mHead)
00156             DeleteFirst();
00157     }
00158 
00159     const T *First() const { return mHead; }
00160     T       *First()       { return mHead; }
00161     const T *Last() const  { return mTail; }
00162     T       *Last()        { return mTail; }
00163 
00164     PRBool  IsEmpty() const { return mHead == NULL; }
00165 
00166     //
00167     // moves contents of list to another list
00168     //
00169     void MoveTo(ipcList<T> &other)
00170     {
00171         other.mHead = mHead;
00172         other.mTail = mTail;
00173         mHead = NULL;
00174         mTail = NULL;
00175     }
00176 
00177 protected:
00178     void AdvanceHead()
00179     {
00180         mHead = mHead->mNext;
00181         if (!mHead)
00182             mTail = NULL;
00183     }
00184 
00185     T *mHead;
00186     T *mTail;
00187 };
00188 
00189 template<class T>
00190 class ipcListNode
00191 {
00192 public:
00193     ipcListNode() : mNext(nsnull) {}
00194 
00195     T *mNext;
00196 };
00197 
00198 #endif // !ipcList_h__