Back to index

lightning-sunbird  0.9+nobinonly
nsCommandParams.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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.org 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  *
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 
00039 #ifndef nsCommandParams_h__
00040 #define nsCommandParams_h__
00041 
00042 #include "nsString.h"
00043 
00044 #include "nsICommandParams.h"
00045 
00046 #include "nsCOMPtr.h"
00047 #include "nsHashtable.h"
00048 
00049 #include "pldhash.h"
00050 
00051 
00052 
00053 class nsCommandParams : public nsICommandParams
00054 {
00055 public:
00056 
00057                       nsCommandParams();
00058   virtual             ~nsCommandParams();
00059   
00060   
00061   NS_DECL_ISUPPORTS
00062   NS_DECL_NSICOMMANDPARAMS
00063 
00064   nsresult            Init();
00065   
00066 protected:
00067 
00068   struct HashEntry : public PLDHashEntryHdr
00069   {
00070     nsCString      mEntryName;
00071 
00072     PRUint8       mEntryType;
00073     union {
00074     
00075       PRBool                  mBoolean;
00076       PRInt32                 mLong;
00077       double                  mDouble;
00078       nsString*               mString;
00079       nsCString*              mCString;
00080     } mData;
00081 
00082     nsCOMPtr<nsISupports>   mISupports;    
00083     
00084     HashEntry(PRUint8 inType, const char * inEntryName)
00085     : mEntryName(inEntryName)
00086     , mEntryType(inType)
00087     {
00088       memset(&mData, 0, sizeof(mData));
00089       Reset(mEntryType);
00090     }
00091 
00092     HashEntry(const HashEntry& inRHS)
00093     : mEntryType(inRHS.mEntryType)
00094     {
00095       Reset(mEntryType);
00096       switch (mEntryType)
00097       {
00098         case eBooleanType:      mData.mBoolean = inRHS.mData.mBoolean;  break;
00099         case eLongType:         mData.mLong    = inRHS.mData.mLong;     break;
00100         case eDoubleType:       mData.mDouble  = inRHS.mData.mDouble;   break;
00101         case eWStringType:
00102           NS_ASSERTION(inRHS.mData.mString, "Source entry has no string");
00103           mData.mString = new nsString(*inRHS.mData.mString);
00104           break;      
00105         case eStringType:
00106           NS_ASSERTION(inRHS.mData.mCString, "Source entry has no string");
00107           mData.mCString = new nsCString(*inRHS.mData.mCString);
00108           break;      
00109         case eISupportsType:
00110           mISupports = inRHS.mISupports.get();    // additional addref
00111           break;
00112         default:
00113           NS_ASSERTION(0, "Unknown type");
00114       }
00115     }
00116     
00117     ~HashEntry()
00118     {
00119       if (mEntryType == eWStringType)
00120         delete mData.mString;
00121       else if (mEntryType == eStringType)
00122         delete mData.mCString;
00123     }
00124     
00125     void Reset(PRUint8 inNewType)
00126     {
00127       switch (mEntryType)
00128       {
00129         case eNoType:                                       break;
00130         case eBooleanType:      mData.mBoolean = PR_FALSE;  break;
00131         case eLongType:         mData.mLong = 0;            break;
00132         case eDoubleType:       mData.mDouble = 0.0;        break;
00133         case eWStringType:      delete mData.mString; mData.mString = nsnull;     break;
00134         case eISupportsType:    mISupports = nsnull;        break;    // clear the nsCOMPtr
00135         case eStringType:       delete mData.mCString; mData.mCString = nsnull;   break;
00136         default:
00137           NS_ASSERTION(0, "Unknown type");
00138       }
00139       
00140       mEntryType = inNewType;
00141     }
00142     
00143   };
00144 
00145 
00146   HashEntry*          GetNamedEntry(const char * name);
00147   HashEntry*          GetIndexedEntry(PRInt32 index);
00148   PRUint32            GetNumEntries();
00149   
00150   nsresult            GetOrMakeEntry(const char * name, PRUint8 entryType, HashEntry*& outEntry);
00151   
00152 protected:
00153 
00154   static const void * PR_CALLBACK  HashGetKey(PLDHashTable *table, PLDHashEntryHdr *entry);
00155 
00156   static PLDHashNumber PR_CALLBACK HashKey(PLDHashTable *table, const void *key);
00157 
00158   static PRBool PR_CALLBACK        HashMatchEntry(PLDHashTable *table,
00159                                                   const PLDHashEntryHdr *entry, const void *key);
00160   
00161   static void PR_CALLBACK          HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from,
00162                                                  PLDHashEntryHdr *to);
00163   
00164   static void PR_CALLBACK          HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry);
00165   
00166                                   
00167 protected:
00168   
00169   enum {
00170     eNumEntriesUnknown      = -1
00171   };
00172 
00173   // this is going to have to use a pldhash, because we need to
00174   // be able to iterate through entries, passing names back
00175   // to the caller, which means that we need to store the names
00176   // internally.
00177   
00178   PLDHashTable    mValuesHash;
00179   
00180   // enumerator data
00181   PRInt32         mCurEntry;
00182   PRInt32         mNumEntries;      // number of entries at start of enumeration (-1 indicates not known)
00183     
00184   static PLDHashTableOps    sHashOps;
00185 };
00186 
00187 
00188 #endif // nsCommandParams_h__