|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 #ifndef nsCommandParams_h__ |
|
8 #define nsCommandParams_h__ |
|
9 |
|
10 #include "nsString.h" |
|
11 #include "nsICommandParams.h" |
|
12 #include "nsCOMPtr.h" |
|
13 #include "pldhash.h" |
|
14 |
|
15 |
|
16 |
|
17 class nsCommandParams : public nsICommandParams |
|
18 { |
|
19 public: |
|
20 |
|
21 nsCommandParams(); |
|
22 virtual ~nsCommandParams(); |
|
23 |
|
24 |
|
25 NS_DECL_ISUPPORTS |
|
26 NS_DECL_NSICOMMANDPARAMS |
|
27 |
|
28 nsresult Init(); |
|
29 |
|
30 protected: |
|
31 |
|
32 struct HashEntry : public PLDHashEntryHdr |
|
33 { |
|
34 nsCString mEntryName; |
|
35 |
|
36 uint8_t mEntryType; |
|
37 union { |
|
38 |
|
39 bool mBoolean; |
|
40 int32_t mLong; |
|
41 double mDouble; |
|
42 nsString* mString; |
|
43 nsCString* mCString; |
|
44 } mData; |
|
45 |
|
46 nsCOMPtr<nsISupports> mISupports; |
|
47 |
|
48 HashEntry(uint8_t inType, const char * inEntryName) |
|
49 : mEntryName(inEntryName) |
|
50 , mEntryType(inType) |
|
51 { |
|
52 memset(&mData, 0, sizeof(mData)); |
|
53 Reset(mEntryType); |
|
54 } |
|
55 |
|
56 HashEntry(const HashEntry& inRHS) |
|
57 : mEntryType(inRHS.mEntryType) |
|
58 { |
|
59 Reset(mEntryType); |
|
60 switch (mEntryType) |
|
61 { |
|
62 case eBooleanType: mData.mBoolean = inRHS.mData.mBoolean; break; |
|
63 case eLongType: mData.mLong = inRHS.mData.mLong; break; |
|
64 case eDoubleType: mData.mDouble = inRHS.mData.mDouble; break; |
|
65 case eWStringType: |
|
66 NS_ASSERTION(inRHS.mData.mString, "Source entry has no string"); |
|
67 mData.mString = new nsString(*inRHS.mData.mString); |
|
68 break; |
|
69 case eStringType: |
|
70 NS_ASSERTION(inRHS.mData.mCString, "Source entry has no string"); |
|
71 mData.mCString = new nsCString(*inRHS.mData.mCString); |
|
72 break; |
|
73 case eISupportsType: |
|
74 mISupports = inRHS.mISupports.get(); // additional addref |
|
75 break; |
|
76 default: |
|
77 NS_ERROR("Unknown type"); |
|
78 } |
|
79 } |
|
80 |
|
81 ~HashEntry() |
|
82 { |
|
83 if (mEntryType == eWStringType) |
|
84 delete mData.mString; |
|
85 else if (mEntryType == eStringType) |
|
86 delete mData.mCString; |
|
87 } |
|
88 |
|
89 void Reset(uint8_t inNewType) |
|
90 { |
|
91 switch (mEntryType) |
|
92 { |
|
93 case eNoType: break; |
|
94 case eBooleanType: mData.mBoolean = false; break; |
|
95 case eLongType: mData.mLong = 0; break; |
|
96 case eDoubleType: mData.mDouble = 0.0; break; |
|
97 case eWStringType: delete mData.mString; mData.mString = nullptr; break; |
|
98 case eISupportsType: mISupports = nullptr; break; // clear the nsCOMPtr |
|
99 case eStringType: delete mData.mCString; mData.mCString = nullptr; break; |
|
100 default: |
|
101 NS_ERROR("Unknown type"); |
|
102 } |
|
103 |
|
104 mEntryType = inNewType; |
|
105 } |
|
106 |
|
107 }; |
|
108 |
|
109 |
|
110 HashEntry* GetNamedEntry(const char * name); |
|
111 HashEntry* GetIndexedEntry(int32_t index); |
|
112 uint32_t GetNumEntries(); |
|
113 |
|
114 nsresult GetOrMakeEntry(const char * name, uint8_t entryType, HashEntry*& outEntry); |
|
115 |
|
116 protected: |
|
117 |
|
118 static PLDHashNumber HashKey(PLDHashTable *table, const void *key); |
|
119 |
|
120 static bool HashMatchEntry(PLDHashTable *table, |
|
121 const PLDHashEntryHdr *entry, const void *key); |
|
122 |
|
123 static void HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from, |
|
124 PLDHashEntryHdr *to); |
|
125 |
|
126 static void HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry); |
|
127 |
|
128 |
|
129 protected: |
|
130 |
|
131 enum { |
|
132 eNumEntriesUnknown = -1 |
|
133 }; |
|
134 |
|
135 // this is going to have to use a pldhash, because we need to |
|
136 // be able to iterate through entries, passing names back |
|
137 // to the caller, which means that we need to store the names |
|
138 // internally. |
|
139 |
|
140 PLDHashTable mValuesHash; |
|
141 |
|
142 // enumerator data |
|
143 int32_t mCurEntry; |
|
144 int32_t mNumEntries; // number of entries at start of enumeration (-1 indicates not known) |
|
145 |
|
146 static const PLDHashTableOps sHashOps; |
|
147 }; |
|
148 |
|
149 |
|
150 #endif // nsCommandParams_h__ |