1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 :
39 : #ifndef nsCommandParams_h__
40 : #define nsCommandParams_h__
41 :
42 : #include "nsString.h"
43 :
44 : #include "nsICommandParams.h"
45 :
46 : #include "nsCOMPtr.h"
47 : #include "nsHashtable.h"
48 :
49 : #include "pldhash.h"
50 :
51 :
52 :
53 : class nsCommandParams : public nsICommandParams
54 : {
55 : public:
56 :
57 : nsCommandParams();
58 : virtual ~nsCommandParams();
59 :
60 :
61 : NS_DECL_ISUPPORTS
62 : NS_DECL_NSICOMMANDPARAMS
63 :
64 : nsresult Init();
65 :
66 : protected:
67 :
68 : struct HashEntry : public PLDHashEntryHdr
69 0 : {
70 : nsCString mEntryName;
71 :
72 : PRUint8 mEntryType;
73 : union {
74 :
75 : bool mBoolean;
76 : PRInt32 mLong;
77 : double mDouble;
78 : nsString* mString;
79 : nsCString* mCString;
80 : } mData;
81 :
82 : nsCOMPtr<nsISupports> mISupports;
83 :
84 0 : HashEntry(PRUint8 inType, const char * inEntryName)
85 : : mEntryName(inEntryName)
86 0 : , mEntryType(inType)
87 : {
88 0 : memset(&mData, 0, sizeof(mData));
89 0 : Reset(mEntryType);
90 0 : }
91 :
92 : HashEntry(const HashEntry& inRHS)
93 : : mEntryType(inRHS.mEntryType)
94 : {
95 : Reset(mEntryType);
96 : switch (mEntryType)
97 : {
98 : case eBooleanType: mData.mBoolean = inRHS.mData.mBoolean; break;
99 : case eLongType: mData.mLong = inRHS.mData.mLong; break;
100 : case eDoubleType: mData.mDouble = inRHS.mData.mDouble; break;
101 : case eWStringType:
102 : NS_ASSERTION(inRHS.mData.mString, "Source entry has no string");
103 : mData.mString = new nsString(*inRHS.mData.mString);
104 : break;
105 : case eStringType:
106 : NS_ASSERTION(inRHS.mData.mCString, "Source entry has no string");
107 : mData.mCString = new nsCString(*inRHS.mData.mCString);
108 : break;
109 : case eISupportsType:
110 : mISupports = inRHS.mISupports.get(); // additional addref
111 : break;
112 : default:
113 : NS_ERROR("Unknown type");
114 : }
115 : }
116 :
117 0 : ~HashEntry()
118 0 : {
119 0 : if (mEntryType == eWStringType)
120 0 : delete mData.mString;
121 0 : else if (mEntryType == eStringType)
122 0 : delete mData.mCString;
123 0 : }
124 :
125 0 : void Reset(PRUint8 inNewType)
126 : {
127 0 : switch (mEntryType)
128 : {
129 0 : case eNoType: break;
130 0 : case eBooleanType: mData.mBoolean = false; break;
131 0 : case eLongType: mData.mLong = 0; break;
132 0 : case eDoubleType: mData.mDouble = 0.0; break;
133 0 : case eWStringType: delete mData.mString; mData.mString = nsnull; break;
134 0 : case eISupportsType: mISupports = nsnull; break; // clear the nsCOMPtr
135 0 : case eStringType: delete mData.mCString; mData.mCString = nsnull; break;
136 : default:
137 0 : NS_ERROR("Unknown type");
138 : }
139 :
140 0 : mEntryType = inNewType;
141 0 : }
142 :
143 : };
144 :
145 :
146 : HashEntry* GetNamedEntry(const char * name);
147 : HashEntry* GetIndexedEntry(PRInt32 index);
148 : PRUint32 GetNumEntries();
149 :
150 : nsresult GetOrMakeEntry(const char * name, PRUint8 entryType, HashEntry*& outEntry);
151 :
152 : protected:
153 :
154 : static PLDHashNumber HashKey(PLDHashTable *table, const void *key);
155 :
156 : static bool HashMatchEntry(PLDHashTable *table,
157 : const PLDHashEntryHdr *entry, const void *key);
158 :
159 : static void HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from,
160 : PLDHashEntryHdr *to);
161 :
162 : static void HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry);
163 :
164 :
165 : protected:
166 :
167 : enum {
168 : eNumEntriesUnknown = -1
169 : };
170 :
171 : // this is going to have to use a pldhash, because we need to
172 : // be able to iterate through entries, passing names back
173 : // to the caller, which means that we need to store the names
174 : // internally.
175 :
176 : PLDHashTable mValuesHash;
177 :
178 : // enumerator data
179 : PRInt32 mCurEntry;
180 : PRInt32 mNumEntries; // number of entries at start of enumeration (-1 indicates not known)
181 :
182 : static PLDHashTableOps sHashOps;
183 : };
184 :
185 :
186 : #endif // nsCommandParams_h__
|