xpcom/ds/nsHashPropertyBag.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim:set ts=4 sw=4 sts=4: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsHashPropertyBag.h"
michael@0 8 #include "nsArray.h"
michael@0 9 #include "nsArrayEnumerator.h"
michael@0 10 #include "nsIVariant.h"
michael@0 11 #include "nsIProperty.h"
michael@0 12 #include "nsVariant.h"
michael@0 13 #include "mozilla/Attributes.h"
michael@0 14
michael@0 15 nsresult
michael@0 16 NS_NewHashPropertyBag(nsIWritablePropertyBag* *_retval)
michael@0 17 {
michael@0 18 nsRefPtr<nsHashPropertyBag> hpb = new nsHashPropertyBag();
michael@0 19 hpb.forget(_retval);
michael@0 20 return NS_OK;
michael@0 21 }
michael@0 22
michael@0 23 /*
michael@0 24 * nsHashPropertyBag impl
michael@0 25 */
michael@0 26
michael@0 27 NS_IMPL_ADDREF(nsHashPropertyBag)
michael@0 28 NS_IMPL_RELEASE(nsHashPropertyBag)
michael@0 29 NS_INTERFACE_MAP_BEGIN(nsHashPropertyBag)
michael@0 30 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
michael@0 31 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
michael@0 32 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
michael@0 33 NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
michael@0 34 NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
michael@0 35 NS_INTERFACE_MAP_END
michael@0 36
michael@0 37 NS_IMETHODIMP
michael@0 38 nsHashPropertyBag::HasKey(const nsAString& name, bool *aResult)
michael@0 39 {
michael@0 40 *aResult = mPropertyHash.Get(name, nullptr);
michael@0 41
michael@0 42 return NS_OK;
michael@0 43 }
michael@0 44
michael@0 45 NS_IMETHODIMP
michael@0 46 nsHashPropertyBag::Get(const nsAString& name, nsIVariant* *_retval)
michael@0 47 {
michael@0 48 if (!mPropertyHash.Get(name, _retval))
michael@0 49 *_retval = nullptr;
michael@0 50
michael@0 51 return NS_OK;
michael@0 52 }
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 nsHashPropertyBag::GetProperty(const nsAString& name, nsIVariant* *_retval)
michael@0 56 {
michael@0 57 bool isFound = mPropertyHash.Get(name, _retval);
michael@0 58 if (!isFound)
michael@0 59 return NS_ERROR_FAILURE;
michael@0 60
michael@0 61 return NS_OK;
michael@0 62 }
michael@0 63
michael@0 64 NS_IMETHODIMP
michael@0 65 nsHashPropertyBag::SetProperty(const nsAString& name, nsIVariant *value)
michael@0 66 {
michael@0 67 if (NS_WARN_IF(!value))
michael@0 68 return NS_ERROR_INVALID_ARG;
michael@0 69
michael@0 70 mPropertyHash.Put(name, value);
michael@0 71
michael@0 72 return NS_OK;
michael@0 73 }
michael@0 74
michael@0 75 NS_IMETHODIMP
michael@0 76 nsHashPropertyBag::DeleteProperty(const nsAString& name)
michael@0 77 {
michael@0 78 // is it too much to ask for ns*Hashtable to return
michael@0 79 // a boolean indicating whether RemoveEntry succeeded
michael@0 80 // or not?!?!
michael@0 81 bool isFound = mPropertyHash.Get(name, nullptr);
michael@0 82 if (!isFound)
michael@0 83 return NS_ERROR_FAILURE;
michael@0 84
michael@0 85 // then from the hash
michael@0 86 mPropertyHash.Remove(name);
michael@0 87
michael@0 88 return NS_OK;
michael@0 89 }
michael@0 90
michael@0 91
michael@0 92 //
michael@0 93 // nsSimpleProperty class and impl; used for GetEnumerator
michael@0 94 //
michael@0 95
michael@0 96 class nsSimpleProperty MOZ_FINAL : public nsIProperty {
michael@0 97 public:
michael@0 98 nsSimpleProperty(const nsAString& aName, nsIVariant* aValue)
michael@0 99 : mName(aName), mValue(aValue)
michael@0 100 {
michael@0 101 }
michael@0 102
michael@0 103 NS_DECL_ISUPPORTS
michael@0 104 NS_DECL_NSIPROPERTY
michael@0 105 protected:
michael@0 106 nsString mName;
michael@0 107 nsCOMPtr<nsIVariant> mValue;
michael@0 108 };
michael@0 109
michael@0 110 NS_IMPL_ISUPPORTS(nsSimpleProperty, nsIProperty)
michael@0 111
michael@0 112 NS_IMETHODIMP
michael@0 113 nsSimpleProperty::GetName(nsAString& aName)
michael@0 114 {
michael@0 115 aName.Assign(mName);
michael@0 116 return NS_OK;
michael@0 117 }
michael@0 118
michael@0 119 NS_IMETHODIMP
michael@0 120 nsSimpleProperty::GetValue(nsIVariant* *aValue)
michael@0 121 {
michael@0 122 NS_IF_ADDREF(*aValue = mValue);
michael@0 123 return NS_OK;
michael@0 124 }
michael@0 125
michael@0 126 // end nsSimpleProperty
michael@0 127
michael@0 128 static PLDHashOperator
michael@0 129 PropertyHashToArrayFunc (const nsAString &aKey,
michael@0 130 nsIVariant* aData,
michael@0 131 void *userArg)
michael@0 132 {
michael@0 133 nsIMutableArray *propertyArray =
michael@0 134 static_cast<nsIMutableArray *>(userArg);
michael@0 135 nsSimpleProperty *sprop = new nsSimpleProperty(aKey, aData);
michael@0 136 propertyArray->AppendElement(sprop, false);
michael@0 137 return PL_DHASH_NEXT;
michael@0 138 }
michael@0 139
michael@0 140
michael@0 141 NS_IMETHODIMP
michael@0 142 nsHashPropertyBag::GetEnumerator(nsISimpleEnumerator* *_retval)
michael@0 143 {
michael@0 144 nsCOMPtr<nsIMutableArray> propertyArray = nsArray::Create();
michael@0 145 if (!propertyArray)
michael@0 146 return NS_ERROR_OUT_OF_MEMORY;
michael@0 147
michael@0 148 mPropertyHash.EnumerateRead(PropertyHashToArrayFunc, propertyArray.get());
michael@0 149
michael@0 150 return NS_NewArrayEnumerator(_retval, propertyArray);
michael@0 151 }
michael@0 152
michael@0 153 #define IMPL_GETSETPROPERTY_AS(Name, Type) \
michael@0 154 NS_IMETHODIMP \
michael@0 155 nsHashPropertyBag::GetPropertyAs ## Name (const nsAString & prop, Type *_retval) \
michael@0 156 { \
michael@0 157 nsIVariant* v = mPropertyHash.GetWeak(prop); \
michael@0 158 if (!v) \
michael@0 159 return NS_ERROR_NOT_AVAILABLE; \
michael@0 160 return v->GetAs ## Name(_retval); \
michael@0 161 } \
michael@0 162 \
michael@0 163 NS_IMETHODIMP \
michael@0 164 nsHashPropertyBag::SetPropertyAs ## Name (const nsAString & prop, Type value) \
michael@0 165 { \
michael@0 166 nsCOMPtr<nsIWritableVariant> var = new nsVariant(); \
michael@0 167 var->SetAs ## Name(value); \
michael@0 168 return SetProperty(prop, var); \
michael@0 169 }
michael@0 170
michael@0 171 IMPL_GETSETPROPERTY_AS(Int32, int32_t)
michael@0 172 IMPL_GETSETPROPERTY_AS(Uint32, uint32_t)
michael@0 173 IMPL_GETSETPROPERTY_AS(Int64, int64_t)
michael@0 174 IMPL_GETSETPROPERTY_AS(Uint64, uint64_t)
michael@0 175 IMPL_GETSETPROPERTY_AS(Double, double)
michael@0 176 IMPL_GETSETPROPERTY_AS(Bool, bool)
michael@0 177
michael@0 178
michael@0 179 NS_IMETHODIMP
michael@0 180 nsHashPropertyBag::GetPropertyAsAString(const nsAString & prop, nsAString & _retval)
michael@0 181 {
michael@0 182 nsIVariant* v = mPropertyHash.GetWeak(prop);
michael@0 183 if (!v)
michael@0 184 return NS_ERROR_NOT_AVAILABLE;
michael@0 185 return v->GetAsAString(_retval);
michael@0 186 }
michael@0 187
michael@0 188 NS_IMETHODIMP
michael@0 189 nsHashPropertyBag::GetPropertyAsACString(const nsAString & prop, nsACString & _retval)
michael@0 190 {
michael@0 191 nsIVariant* v = mPropertyHash.GetWeak(prop);
michael@0 192 if (!v)
michael@0 193 return NS_ERROR_NOT_AVAILABLE;
michael@0 194 return v->GetAsACString(_retval);
michael@0 195 }
michael@0 196
michael@0 197 NS_IMETHODIMP
michael@0 198 nsHashPropertyBag::GetPropertyAsAUTF8String(const nsAString & prop, nsACString & _retval)
michael@0 199 {
michael@0 200 nsIVariant* v = mPropertyHash.GetWeak(prop);
michael@0 201 if (!v)
michael@0 202 return NS_ERROR_NOT_AVAILABLE;
michael@0 203 return v->GetAsAUTF8String(_retval);
michael@0 204 }
michael@0 205
michael@0 206 NS_IMETHODIMP
michael@0 207 nsHashPropertyBag::GetPropertyAsInterface(const nsAString & prop,
michael@0 208 const nsIID & aIID,
michael@0 209 void** _retval)
michael@0 210 {
michael@0 211 nsIVariant* v = mPropertyHash.GetWeak(prop);
michael@0 212 if (!v)
michael@0 213 return NS_ERROR_NOT_AVAILABLE;
michael@0 214 nsCOMPtr<nsISupports> val;
michael@0 215 nsresult rv = v->GetAsISupports(getter_AddRefs(val));
michael@0 216 if (NS_FAILED(rv))
michael@0 217 return rv;
michael@0 218 if (!val) {
michael@0 219 // We have a value, but it's null
michael@0 220 *_retval = nullptr;
michael@0 221 return NS_OK;
michael@0 222 }
michael@0 223 return val->QueryInterface(aIID, _retval);
michael@0 224 }
michael@0 225
michael@0 226 NS_IMETHODIMP
michael@0 227 nsHashPropertyBag::SetPropertyAsAString(const nsAString & prop, const nsAString & value)
michael@0 228 {
michael@0 229 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
michael@0 230 var->SetAsAString(value);
michael@0 231 return SetProperty(prop, var);
michael@0 232 }
michael@0 233
michael@0 234 NS_IMETHODIMP
michael@0 235 nsHashPropertyBag::SetPropertyAsACString(const nsAString & prop, const nsACString & value)
michael@0 236 {
michael@0 237 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
michael@0 238 var->SetAsACString(value);
michael@0 239 return SetProperty(prop, var);
michael@0 240 }
michael@0 241
michael@0 242 NS_IMETHODIMP
michael@0 243 nsHashPropertyBag::SetPropertyAsAUTF8String(const nsAString & prop, const nsACString & value)
michael@0 244 {
michael@0 245 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
michael@0 246 var->SetAsAUTF8String(value);
michael@0 247 return SetProperty(prop, var);
michael@0 248 }
michael@0 249
michael@0 250 NS_IMETHODIMP
michael@0 251 nsHashPropertyBag::SetPropertyAsInterface(const nsAString & prop, nsISupports* value)
michael@0 252 {
michael@0 253 nsCOMPtr<nsIWritableVariant> var = new nsVariant();
michael@0 254 var->SetAsISupports(value);
michael@0 255 return SetProperty(prop, var);
michael@0 256 }
michael@0 257

mercurial