Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / xpcom / ds / nsHashPropertyBag.cpp
blobf18119cb393767d98d2411fdfcf48b0448487331
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 "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"
16 * nsHashPropertyBagBase implementation.
19 NS_IMETHODIMP
20 nsHashPropertyBagBase::HasKey(const nsAString& aName, bool* aResult)
22 *aResult = mPropertyHash.Get(aName, nullptr);
23 return NS_OK;
26 NS_IMETHODIMP
27 nsHashPropertyBagBase::Get(const nsAString& aName, nsIVariant** aResult)
29 if (!mPropertyHash.Get(aName, aResult)) {
30 *aResult = nullptr;
33 return NS_OK;
36 NS_IMETHODIMP
37 nsHashPropertyBagBase::GetProperty(const nsAString& aName, nsIVariant** aResult)
39 bool isFound = mPropertyHash.Get(aName, aResult);
40 if (!isFound) {
41 return NS_ERROR_FAILURE;
44 return NS_OK;
47 NS_IMETHODIMP
48 nsHashPropertyBagBase::SetProperty(const nsAString& aName, nsIVariant* aValue)
50 if (NS_WARN_IF(!aValue)) {
51 return NS_ERROR_INVALID_ARG;
54 mPropertyHash.Put(aName, aValue);
56 return NS_OK;
59 NS_IMETHODIMP
60 nsHashPropertyBagBase::DeleteProperty(const nsAString& aName)
62 // is it too much to ask for ns*Hashtable to return
63 // a boolean indicating whether RemoveEntry succeeded
64 // or not?!?!
65 bool isFound = mPropertyHash.Get(aName, nullptr);
66 if (!isFound) {
67 return NS_ERROR_FAILURE;
70 // then from the hash
71 mPropertyHash.Remove(aName);
73 return NS_OK;
78 // nsSimpleProperty class and impl; used for GetEnumerator
81 class nsSimpleProperty MOZ_FINAL : public nsIProperty
83 ~nsSimpleProperty() {}
85 public:
86 nsSimpleProperty(const nsAString& aName, nsIVariant* aValue)
87 : mName(aName)
88 , mValue(aValue)
92 NS_DECL_ISUPPORTS
93 NS_DECL_NSIPROPERTY
94 protected:
95 nsString mName;
96 nsCOMPtr<nsIVariant> mValue;
99 NS_IMPL_ISUPPORTS(nsSimpleProperty, nsIProperty)
101 NS_IMETHODIMP
102 nsSimpleProperty::GetName(nsAString& aName)
104 aName.Assign(mName);
105 return NS_OK;
108 NS_IMETHODIMP
109 nsSimpleProperty::GetValue(nsIVariant** aValue)
111 NS_IF_ADDREF(*aValue = mValue);
112 return NS_OK;
115 // end nsSimpleProperty
117 static PLDHashOperator
118 PropertyHashToArrayFunc(const nsAString& aKey,
119 nsIVariant* aData,
120 void* aUserArg)
122 nsIMutableArray* propertyArray = static_cast<nsIMutableArray*>(aUserArg);
123 nsSimpleProperty* sprop = new nsSimpleProperty(aKey, aData);
124 propertyArray->AppendElement(sprop, false);
125 return PL_DHASH_NEXT;
129 NS_IMETHODIMP
130 nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult)
132 nsCOMPtr<nsIMutableArray> propertyArray = nsArray::Create();
133 if (!propertyArray) {
134 return NS_ERROR_OUT_OF_MEMORY;
137 mPropertyHash.EnumerateRead(PropertyHashToArrayFunc, propertyArray.get());
139 return NS_NewArrayEnumerator(aResult, propertyArray);
142 #define IMPL_GETSETPROPERTY_AS(Name, Type) \
143 NS_IMETHODIMP \
144 nsHashPropertyBagBase::GetPropertyAs ## Name (const nsAString & prop, Type *_retval) \
146 nsIVariant* v = mPropertyHash.GetWeak(prop); \
147 if (!v) \
148 return NS_ERROR_NOT_AVAILABLE; \
149 return v->GetAs ## Name(_retval); \
152 NS_IMETHODIMP \
153 nsHashPropertyBagBase::SetPropertyAs ## Name (const nsAString & prop, Type value) \
155 nsCOMPtr<nsIWritableVariant> var = new nsVariant(); \
156 var->SetAs ## Name(value); \
157 return SetProperty(prop, var); \
160 IMPL_GETSETPROPERTY_AS(Int32, int32_t)
161 IMPL_GETSETPROPERTY_AS(Uint32, uint32_t)
162 IMPL_GETSETPROPERTY_AS(Int64, int64_t)
163 IMPL_GETSETPROPERTY_AS(Uint64, uint64_t)
164 IMPL_GETSETPROPERTY_AS(Double, double)
165 IMPL_GETSETPROPERTY_AS(Bool, bool)
168 NS_IMETHODIMP
169 nsHashPropertyBagBase::GetPropertyAsAString(const nsAString& aProp,
170 nsAString& aResult)
172 nsIVariant* v = mPropertyHash.GetWeak(aProp);
173 if (!v) {
174 return NS_ERROR_NOT_AVAILABLE;
176 return v->GetAsAString(aResult);
179 NS_IMETHODIMP
180 nsHashPropertyBagBase::GetPropertyAsACString(const nsAString& aProp,
181 nsACString& aResult)
183 nsIVariant* v = mPropertyHash.GetWeak(aProp);
184 if (!v) {
185 return NS_ERROR_NOT_AVAILABLE;
187 return v->GetAsACString(aResult);
190 NS_IMETHODIMP
191 nsHashPropertyBagBase::GetPropertyAsAUTF8String(const nsAString& aProp,
192 nsACString& aResult)
194 nsIVariant* v = mPropertyHash.GetWeak(aProp);
195 if (!v) {
196 return NS_ERROR_NOT_AVAILABLE;
198 return v->GetAsAUTF8String(aResult);
201 NS_IMETHODIMP
202 nsHashPropertyBagBase::GetPropertyAsInterface(const nsAString& aProp,
203 const nsIID& aIID,
204 void** aResult)
206 nsIVariant* v = mPropertyHash.GetWeak(aProp);
207 if (!v) {
208 return NS_ERROR_NOT_AVAILABLE;
210 nsCOMPtr<nsISupports> val;
211 nsresult rv = v->GetAsISupports(getter_AddRefs(val));
212 if (NS_FAILED(rv)) {
213 return rv;
215 if (!val) {
216 // We have a value, but it's null
217 *aResult = nullptr;
218 return NS_OK;
220 return val->QueryInterface(aIID, aResult);
223 NS_IMETHODIMP
224 nsHashPropertyBagBase::SetPropertyAsAString(const nsAString& aProp,
225 const nsAString& aValue)
227 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
228 var->SetAsAString(aValue);
229 return SetProperty(aProp, var);
232 NS_IMETHODIMP
233 nsHashPropertyBagBase::SetPropertyAsACString(const nsAString& aProp,
234 const nsACString& aValue)
236 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
237 var->SetAsACString(aValue);
238 return SetProperty(aProp, var);
241 NS_IMETHODIMP
242 nsHashPropertyBagBase::SetPropertyAsAUTF8String(const nsAString& aProp,
243 const nsACString& aValue)
245 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
246 var->SetAsAUTF8String(aValue);
247 return SetProperty(aProp, var);
250 NS_IMETHODIMP
251 nsHashPropertyBagBase::SetPropertyAsInterface(const nsAString& aProp,
252 nsISupports* aValue)
254 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
255 var->SetAsISupports(aValue);
256 return SetProperty(aProp, var);
261 * nsHashPropertyBag implementation.
264 NS_IMPL_ADDREF(nsHashPropertyBag)
265 NS_IMPL_RELEASE(nsHashPropertyBag)
267 NS_INTERFACE_MAP_BEGIN(nsHashPropertyBag)
268 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
269 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
270 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
271 NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
272 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
273 NS_INTERFACE_MAP_END
277 * nsHashPropertyBagCC implementation.
280 NS_IMPL_CYCLE_COLLECTION(nsHashPropertyBagCC, mPropertyHash)
282 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHashPropertyBagCC)
283 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHashPropertyBagCC)
285 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHashPropertyBagCC)
286 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
287 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
288 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
289 NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
290 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
291 NS_INTERFACE_MAP_END