1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "xpcom-config.h"
12 #include "nsCommandParams.h"
13 #include "mozilla/HashFunctions.h"
15 using namespace mozilla
;
17 const PLDHashTableOps
nsCommandParams::sHashOps
=
25 NS_IMPL_ISUPPORTS(nsCommandParams
, nsICommandParams
)
27 nsCommandParams::nsCommandParams()
28 : mValuesHash(&sHashOps
, sizeof(HashEntry
), 2)
32 nsCommandParams::~nsCommandParams()
37 nsCommandParams::GetValueType(const char* aName
, int16_t* aRetVal
)
39 NS_ENSURE_ARG_POINTER(aRetVal
);
41 HashEntry
* foundEntry
= GetNamedEntry(aName
);
43 *aRetVal
= foundEntry
->mEntryType
;
47 return NS_ERROR_FAILURE
;
51 nsCommandParams::GetBooleanValue(const char* aName
, bool* aRetVal
)
53 NS_ENSURE_ARG_POINTER(aRetVal
);
55 HashEntry
* foundEntry
= GetNamedEntry(aName
);
56 if (foundEntry
&& foundEntry
->mEntryType
== eBooleanType
) {
57 *aRetVal
= foundEntry
->mData
.mBoolean
;
61 return NS_ERROR_FAILURE
;
65 nsCommandParams::GetLongValue(const char* aName
, int32_t* aRetVal
)
67 NS_ENSURE_ARG_POINTER(aRetVal
);
69 HashEntry
* foundEntry
= GetNamedEntry(aName
);
70 if (foundEntry
&& foundEntry
->mEntryType
== eLongType
) {
71 *aRetVal
= foundEntry
->mData
.mLong
;
75 return NS_ERROR_FAILURE
;
79 nsCommandParams::GetDoubleValue(const char* aName
, double* aRetVal
)
81 NS_ENSURE_ARG_POINTER(aRetVal
);
83 HashEntry
* foundEntry
= GetNamedEntry(aName
);
84 if (foundEntry
&& foundEntry
->mEntryType
== eDoubleType
) {
85 *aRetVal
= foundEntry
->mData
.mDouble
;
89 return NS_ERROR_FAILURE
;
93 nsCommandParams::GetStringValue(const char* aName
, nsAString
& aRetVal
)
95 HashEntry
* foundEntry
= GetNamedEntry(aName
);
96 if (foundEntry
&& foundEntry
->mEntryType
== eWStringType
) {
97 NS_ASSERTION(foundEntry
->mData
.mString
, "Null string");
98 aRetVal
.Assign(*foundEntry
->mData
.mString
);
102 return NS_ERROR_FAILURE
;
106 nsCommandParams::GetCStringValue(const char* aName
, char** aRetVal
)
108 NS_ENSURE_ARG_POINTER(aRetVal
);
110 HashEntry
* foundEntry
= GetNamedEntry(aName
);
111 if (foundEntry
&& foundEntry
->mEntryType
== eStringType
) {
112 NS_ASSERTION(foundEntry
->mData
.mCString
, "Null string");
113 *aRetVal
= ToNewCString(*foundEntry
->mData
.mCString
);
117 return NS_ERROR_FAILURE
;
121 nsCommandParams::GetISupportsValue(const char* aName
, nsISupports
** aRetVal
)
123 NS_ENSURE_ARG_POINTER(aRetVal
);
125 HashEntry
* foundEntry
= GetNamedEntry(aName
);
126 if (foundEntry
&& foundEntry
->mEntryType
== eISupportsType
) {
127 NS_IF_ADDREF(*aRetVal
= foundEntry
->mISupports
.get());
131 return NS_ERROR_FAILURE
;
135 nsCommandParams::SetBooleanValue(const char* aName
, bool aValue
)
137 HashEntry
* foundEntry
= GetOrMakeEntry(aName
, eBooleanType
);
139 return NS_ERROR_OUT_OF_MEMORY
;
141 foundEntry
->mData
.mBoolean
= aValue
;
146 nsCommandParams::SetLongValue(const char* aName
, int32_t aValue
)
148 HashEntry
* foundEntry
= GetOrMakeEntry(aName
, eLongType
);
150 return NS_ERROR_OUT_OF_MEMORY
;
152 foundEntry
->mData
.mLong
= aValue
;
157 nsCommandParams::SetDoubleValue(const char* aName
, double aValue
)
159 HashEntry
* foundEntry
= GetOrMakeEntry(aName
, eDoubleType
);
161 return NS_ERROR_OUT_OF_MEMORY
;
163 foundEntry
->mData
.mDouble
= aValue
;
168 nsCommandParams::SetStringValue(const char* aName
, const nsAString
& aValue
)
170 HashEntry
* foundEntry
= GetOrMakeEntry(aName
, eWStringType
);
172 return NS_ERROR_OUT_OF_MEMORY
;
174 foundEntry
->mData
.mString
= new nsString(aValue
);
179 nsCommandParams::SetCStringValue(const char* aName
, const char* aValue
)
181 HashEntry
* foundEntry
= GetOrMakeEntry(aName
, eStringType
);
183 return NS_ERROR_OUT_OF_MEMORY
;
185 foundEntry
->mData
.mCString
= new nsCString(aValue
);
190 nsCommandParams::SetISupportsValue(const char* aName
, nsISupports
* aValue
)
192 HashEntry
* foundEntry
= GetOrMakeEntry(aName
, eISupportsType
);
194 return NS_ERROR_OUT_OF_MEMORY
;
196 foundEntry
->mISupports
= aValue
; // addrefs
201 nsCommandParams::RemoveValue(const char* aName
)
203 mValuesHash
.Remove((void*)aName
);
207 nsCommandParams::HashEntry
*
208 nsCommandParams::GetNamedEntry(const char* aName
)
210 return static_cast<HashEntry
*>(mValuesHash
.Search((void*)aName
));
213 nsCommandParams::HashEntry
*
214 nsCommandParams::GetOrMakeEntry(const char* aName
, uint8_t aEntryType
)
216 auto foundEntry
= static_cast<HashEntry
*>(mValuesHash
.Search((void*)aName
));
217 if (foundEntry
) { // reuse existing entry
218 foundEntry
->Reset(aEntryType
);
222 foundEntry
= static_cast<HashEntry
*>(mValuesHash
.Add((void*)aName
, fallible
));
227 // Use placement new. Our ctor does not clobber keyHash, which is important.
228 new (foundEntry
) HashEntry(aEntryType
, aName
);
233 nsCommandParams::HashKey(const void* aKey
)
235 return HashString((const char*)aKey
);
239 nsCommandParams::HashMatchEntry(const PLDHashEntryHdr
* aEntry
, const void* aKey
)
241 const char* keyString
= (const char*)aKey
;
242 const HashEntry
* thisEntry
= static_cast<const HashEntry
*>(aEntry
);
243 return thisEntry
->mEntryName
.Equals(keyString
);
247 nsCommandParams::HashMoveEntry(PLDHashTable
* aTable
,
248 const PLDHashEntryHdr
* aFrom
,
249 PLDHashEntryHdr
* aTo
)
251 const HashEntry
* fromEntry
= static_cast<const HashEntry
*>(aFrom
);
252 HashEntry
* toEntry
= static_cast<HashEntry
*>(aTo
);
254 new (toEntry
) HashEntry(*fromEntry
);
256 fromEntry
->~HashEntry();
260 nsCommandParams::HashClearEntry(PLDHashTable
* aTable
, PLDHashEntryHdr
* aEntry
)
262 HashEntry
* thisEntry
= static_cast<HashEntry
*>(aEntry
);
263 thisEntry
->~HashEntry();