Bumping manifests a=b2g-bump
[gecko.git] / xpcom / ds / nsHashPropertyBag.cpp
blob54ad4bab1332086db07473be9ac780cafa60a150
1 /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 sts=4: */
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 "nsHashPropertyBag.h"
8 #include "nsArray.h"
9 #include "nsArrayEnumerator.h"
10 #include "nsIVariant.h"
11 #include "nsIProperty.h"
12 #include "nsVariant.h"
13 #include "mozilla/Attributes.h"
15 nsresult
16 NS_NewHashPropertyBag(nsIWritablePropertyBag** aResult)
18 nsRefPtr<nsHashPropertyBag> hpb = new nsHashPropertyBag();
19 hpb.forget(aResult);
20 return NS_OK;
24 * nsHashPropertyBag impl
27 NS_IMPL_ADDREF(nsHashPropertyBag)
28 NS_IMPL_RELEASE(nsHashPropertyBag)
29 NS_INTERFACE_MAP_BEGIN(nsHashPropertyBag)
30 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
31 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
32 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
33 NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
34 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
35 NS_INTERFACE_MAP_END
37 NS_IMETHODIMP
38 nsHashPropertyBag::HasKey(const nsAString& aName, bool* aResult)
40 *aResult = mPropertyHash.Get(aName, nullptr);
41 return NS_OK;
44 NS_IMETHODIMP
45 nsHashPropertyBag::Get(const nsAString& aName, nsIVariant** aResult)
47 if (!mPropertyHash.Get(aName, aResult)) {
48 *aResult = nullptr;
51 return NS_OK;
54 NS_IMETHODIMP
55 nsHashPropertyBag::GetProperty(const nsAString& aName, nsIVariant** aResult)
57 bool isFound = mPropertyHash.Get(aName, aResult);
58 if (!isFound) {
59 return NS_ERROR_FAILURE;
62 return NS_OK;
65 NS_IMETHODIMP
66 nsHashPropertyBag::SetProperty(const nsAString& aName, nsIVariant* aValue)
68 if (NS_WARN_IF(!aValue)) {
69 return NS_ERROR_INVALID_ARG;
72 mPropertyHash.Put(aName, aValue);
74 return NS_OK;
77 NS_IMETHODIMP
78 nsHashPropertyBag::DeleteProperty(const nsAString& aName)
80 // is it too much to ask for ns*Hashtable to return
81 // a boolean indicating whether RemoveEntry succeeded
82 // or not?!?!
83 bool isFound = mPropertyHash.Get(aName, nullptr);
84 if (!isFound) {
85 return NS_ERROR_FAILURE;
88 // then from the hash
89 mPropertyHash.Remove(aName);
91 return NS_OK;
96 // nsSimpleProperty class and impl; used for GetEnumerator
99 class nsSimpleProperty MOZ_FINAL : public nsIProperty
101 ~nsSimpleProperty() {}
103 public:
104 nsSimpleProperty(const nsAString& aName, nsIVariant* aValue)
105 : mName(aName)
106 , mValue(aValue)
110 NS_DECL_ISUPPORTS
111 NS_DECL_NSIPROPERTY
112 protected:
113 nsString mName;
114 nsCOMPtr<nsIVariant> mValue;
117 NS_IMPL_ISUPPORTS(nsSimpleProperty, nsIProperty)
119 NS_IMETHODIMP
120 nsSimpleProperty::GetName(nsAString& aName)
122 aName.Assign(mName);
123 return NS_OK;
126 NS_IMETHODIMP
127 nsSimpleProperty::GetValue(nsIVariant** aValue)
129 NS_IF_ADDREF(*aValue = mValue);
130 return NS_OK;
133 // end nsSimpleProperty
135 static PLDHashOperator
136 PropertyHashToArrayFunc(const nsAString& aKey,
137 nsIVariant* aData,
138 void* aUserArg)
140 nsIMutableArray* propertyArray = static_cast<nsIMutableArray*>(aUserArg);
141 nsSimpleProperty* sprop = new nsSimpleProperty(aKey, aData);
142 propertyArray->AppendElement(sprop, false);
143 return PL_DHASH_NEXT;
147 NS_IMETHODIMP
148 nsHashPropertyBag::GetEnumerator(nsISimpleEnumerator** aResult)
150 nsCOMPtr<nsIMutableArray> propertyArray = nsArray::Create();
151 if (!propertyArray) {
152 return NS_ERROR_OUT_OF_MEMORY;
155 mPropertyHash.EnumerateRead(PropertyHashToArrayFunc, propertyArray.get());
157 return NS_NewArrayEnumerator(aResult, propertyArray);
160 #define IMPL_GETSETPROPERTY_AS(Name, Type) \
161 NS_IMETHODIMP \
162 nsHashPropertyBag::GetPropertyAs ## Name (const nsAString & prop, Type *_retval) \
164 nsIVariant* v = mPropertyHash.GetWeak(prop); \
165 if (!v) \
166 return NS_ERROR_NOT_AVAILABLE; \
167 return v->GetAs ## Name(_retval); \
170 NS_IMETHODIMP \
171 nsHashPropertyBag::SetPropertyAs ## Name (const nsAString & prop, Type value) \
173 nsCOMPtr<nsIWritableVariant> var = new nsVariant(); \
174 var->SetAs ## Name(value); \
175 return SetProperty(prop, var); \
178 IMPL_GETSETPROPERTY_AS(Int32, int32_t)
179 IMPL_GETSETPROPERTY_AS(Uint32, uint32_t)
180 IMPL_GETSETPROPERTY_AS(Int64, int64_t)
181 IMPL_GETSETPROPERTY_AS(Uint64, uint64_t)
182 IMPL_GETSETPROPERTY_AS(Double, double)
183 IMPL_GETSETPROPERTY_AS(Bool, bool)
186 NS_IMETHODIMP
187 nsHashPropertyBag::GetPropertyAsAString(const nsAString& aProp,
188 nsAString& aResult)
190 nsIVariant* v = mPropertyHash.GetWeak(aProp);
191 if (!v) {
192 return NS_ERROR_NOT_AVAILABLE;
194 return v->GetAsAString(aResult);
197 NS_IMETHODIMP
198 nsHashPropertyBag::GetPropertyAsACString(const nsAString& aProp,
199 nsACString& aResult)
201 nsIVariant* v = mPropertyHash.GetWeak(aProp);
202 if (!v) {
203 return NS_ERROR_NOT_AVAILABLE;
205 return v->GetAsACString(aResult);
208 NS_IMETHODIMP
209 nsHashPropertyBag::GetPropertyAsAUTF8String(const nsAString& aProp,
210 nsACString& aResult)
212 nsIVariant* v = mPropertyHash.GetWeak(aProp);
213 if (!v) {
214 return NS_ERROR_NOT_AVAILABLE;
216 return v->GetAsAUTF8String(aResult);
219 NS_IMETHODIMP
220 nsHashPropertyBag::GetPropertyAsInterface(const nsAString& aProp,
221 const nsIID& aIID,
222 void** aResult)
224 nsIVariant* v = mPropertyHash.GetWeak(aProp);
225 if (!v) {
226 return NS_ERROR_NOT_AVAILABLE;
228 nsCOMPtr<nsISupports> val;
229 nsresult rv = v->GetAsISupports(getter_AddRefs(val));
230 if (NS_FAILED(rv)) {
231 return rv;
233 if (!val) {
234 // We have a value, but it's null
235 *aResult = nullptr;
236 return NS_OK;
238 return val->QueryInterface(aIID, aResult);
241 NS_IMETHODIMP
242 nsHashPropertyBag::SetPropertyAsAString(const nsAString& aProp,
243 const nsAString& aValue)
245 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
246 var->SetAsAString(aValue);
247 return SetProperty(aProp, var);
250 NS_IMETHODIMP
251 nsHashPropertyBag::SetPropertyAsACString(const nsAString& aProp,
252 const nsACString& aValue)
254 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
255 var->SetAsACString(aValue);
256 return SetProperty(aProp, var);
259 NS_IMETHODIMP
260 nsHashPropertyBag::SetPropertyAsAUTF8String(const nsAString& aProp,
261 const nsACString& aValue)
263 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
264 var->SetAsAUTF8String(aValue);
265 return SetProperty(aProp, var);
268 NS_IMETHODIMP
269 nsHashPropertyBag::SetPropertyAsInterface(const nsAString& aProp,
270 nsISupports* aValue)
272 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
273 var->SetAsISupports(aValue);
274 return SetProperty(aProp, var);